如何解决在jsp页面上导入.xls文件报错问题

在jsp页面上导入.xls文件,报错:
java.io.IOException: Posted content type isn't multipart/form-data
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:166)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:222)
at org.apache.jsp.dxt.sms.multi_005fsender.upload_jsp._jspService(upload_jsp.java:406)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at sdcncsi.ict.security.AclFilter.doFilter(AclFilter.java:78)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at sdcncsi.ict.web.RequestFilter.doFilter(RequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
不知道是什么原因?应该怎样来解决呢?
解决这个错误,需要在form表单里面加上加enctype=”multipart/form-data” 这个属性,不过表单请求传到另一个jsp或servlet里时,是不能用request.getParameter()来获取到各个表单元素的值的,如下:


代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page session="false"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="/common/meta.jsp"%>
<%@ include file="/common/js.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="sdcncsi.ict.util.SysPara"%>
<%@page import="java.io.*,java.util.*,sdcncsi.ict.base.attachment.*"%>
<%@ page import="sdcncsi.ict.util.RequestUtil"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%
String res="true";
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String file=(String)request.getParameter("t_file_excel");
//System.out.println(file);
String accountid3=(String)request.getParameter("accountid3");
String saveDirectory = "";
//获取配置参数,文件类型,文件大小
/*String date = "";
SimpleDateFormat gs = new SimpleDateFormat("yyyyMMdd");
Date now = new Date();
date = gs.format(now);*/
FileFuns fileFuns = new FileFuns();
try {
//判断文件类型
String file_type=SysPara.getValue("dxt_sms_multi_sender_file_type");
if(!file.substring(file.lastIndexOf(".")+1).equals(file_type))
{
out.println("<script>");
out.println("alert('错误:文件类型错误!');");
out.println("</script>");
return;
}
//判断文件夹是否存在,不存在创建
String file_path = SysPara.getValue("dxt_sms_multi_sender_file_path");
file_path=file_path.replace("\\","//");
if (!fileFuns.getFileIsExists(file_path)) {
fileFuns.Createdir(file_path);
}
String file_size = SysPara.getValue("dxt_sms_multi_sender_file_size");
//文件上传后,保存在saveDirectory
saveDirectory = file_path;
RandomFileRenamePolicy rfrp = new RandomFileRenamePolicy();
MultipartRequest multi = null;
System.out.println("saveDirectory"+saveDirectory);
System.out.println("file_size"+file_size);
multi = new MultipartRequest(request, saveDirectory, Integer
.parseInt(file_size), "utf-8", rfrp);
System.out.println("222"+multi);
//输出反馈信息
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String) files.nextElement();
File f = multi.getFile(name);
if (f != null) {
String fileName = multi.getFilesystemName(name);
saveDirectory+=fileName;
}
}
} catch (Exception eu) {
res="false";
eu.printStackTrace();
out.println("<script>");
out.println("alert('错误:文件大小超出限制!');");
out.println("</script>");
}
%>
<head>
<script type="text/javascript">
function myunload(){
window.opener.callback_getPageData_table_list(1);
}
</script>
<base target="_self">
</head>
<body onunload="myunload()">
<a id="reload" href='' style="display:none"></a>
</body>
<script>
var res='<%=res%>';
if(res=='true')
{
sys_ajaxPost("/dxt/multi_sender.do?method=importmulti_sender&accountid=<%=accountid3%>&excelpath=<%=saveDirectory %>","",function(msg){
if(msg.result==true){
location.href="${ctx}/dxt/sms/multi_sender/multi_sender.jsp?result=true";
}else{
location.href="${ctx}/dxt/sms/multi_sender/multi_sender.jsp?result=false";
}
});
}
</script>

(0)

相关推荐

  • 如何解决在jsp页面上导入.xls文件报错问题

    在jsp页面上导入.xls文件,报错: java.io.IOException: Posted content type isn't multipart/form-data at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:166) at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:222)

  • 解决PyCharm同目录下导入模块会报错的问题

    在PyCharm2017中同目录下import其他模块,会出现No model named ...的报错,但实际可以运行 这是因为PyCharm不会将当前文件目录自动加入source_path. 在当前目录右键make_directory as-->Sources Root python导入模块 同一目录下在a.py中导入b.py import b 或者 from b import 方法/函数 不同目录下在a.py中导入b.py import sys sys.path.append('b模块的绝

  • 解决pandas read_csv 读取中文列标题文件报错的问题

    从windows操作系统本地读取csv文件报错 data = pd.read_csv(path) Traceback (most recent call last): File "C:/Users/arron/PycharmProjects/ML/ML/test.py", line 45, in <module> data = pd.read_csv(path) File "C:\Users\arron\AppData\Local\Continuum\Anacon

  • mysql导入sql文件报错 ERROR 2013 2006 2002

    今天导入sql总是提示错误 复制代码 代码如下: ERROR 2013 (HY000): Lost connection to MySQL server during query ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/va

  • eclipse导入appcompat项目报错解决办法

    eclipse导入appcompat项目报错解决办法 我们在eclipse导入开源项目后,经常会发现找不到类似Theme.AppCompat.Light.DarkActionBar的style,解决办法也比较简单,就是导入com.android.support:appcompat-v7包,不过又两点需要注意: 不能只导入appcompat-v7.jar包,而是要导入appcompat-v7项目 因为这个项目包含有资源文件,只导入jar包没有效果,这个项目地址是在: -\android-sdk\e

  • 三个思路解决laravel上传文件报错:413 Request Entity Too Large问题

    最近一个项目当中,要求上传图片,并且限制图片大小,虽然在laravel当中已经添加了相关的表单验证来阻止文件过大的上传,然而当提交表单时,还没轮到laravel处理,nginx就先报错了.当你仔细看报错页面时,你会发现有nginx版本信息,经过分析,这报错是因为nginx的默认上传文件大小配置client_max_body_size只有2MB, 基于nginx验证比laravel验证要早,想要友好报错而不是直接显示413 Request Entity Too Large,那么就有三个思路去解决.

  • 使用smartupload组件实现jsp+jdbc上传下载文件实例解析

    SmartUpload组件只有5个分别是:File.Files.Request.SmartUpload.SmartUploadException类,其中,File代表用户上传的文件,Files代表用户上传的多个文件,Request相当于HttpServletRequest的功能,用于获取表单数据,SmartUpload是最核心的类,负责文件上传下载,SmartUploadException是自定义异常.    SmartUpload的基本使用思路如下: jsp前台代码表单提交 <form act

  • 解决Ajax方式上传文件报错"Uncaught TypeError: Illegal invocation"

    今天使用ajax上传文件时,出现了错误.数据传输的方式是通过定义formData完成的,提交的文件对象也设置为dom对象,但是还是不能发送请求.F12看到后台报了个错误:Uncaught TypeError: Illegal invocation,百度了一下,找到了解决方法. 解决方法:在ajax请求的参数中添加如下两个参数: $.ajax({ ..., processData: false, contentType: false, ... }); processData 类型:Boolean

  • Python3.5 win10环境下导入kera/tensorflow报错的解决方法

    本文实例讲述了Python3.5 win10环境下导入keras/tensorflow报错的解决方法.分享给大家供大家参考,具体如下: 我在win10 Python3.5.2安装keras,然后导入时报错如下: Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright&

  • bootstrap+jquery项目引入文件报错的解决方法

    做一个项目的时候 ,控制台总是会出现各种bug,其实不用慌张,终结起来也就几种类型的错误,在开发中每次遇到错误都善于总结,下次在看到就会胸有成竹知道是什么情况了,以下是在开发过程中总结的一些错误以及错误的解决方法. 报错一:Uncaught ReferenceError: $ is not defined Uncaught ReferenceError: $ is not defined Uncaught ReferenceError: jQuery is not defined 错误原因:文件

随机推荐