如何解決在jsp頁面上匯入.xls檔案報錯問題_JSP編程

來源:互聯網
上載者:User
在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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.