如何解决在jsp页面上导入.xls文件报错问题
程序员文章站
2023-11-20 21:23:28
在jsp页面上导入.xls文件,报错: java.io.ioexception: posted content type isn't multipart/form-data...
在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>
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>