当前位置: 代码迷 >> Java Web开发 >> 用multipart上传文件,该怎么处理
  详细解决方案

用multipart上传文件,该怎么处理

热度:48   发布时间:2016-04-17 11:12:52.0
用multipart上传文件
出现如下错误 源码和错误提示如下 请指教!
java.io.IOException: Corrupt form data: premature ending
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:205)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:222)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:173)
at com.jsl.projectInfo.action.materilListAction.add(materilListAction.java:99)
at com.jsl.projectInfo.action.materilListAction.doPost(materilListAction.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at cn.gov.common.utils.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:43)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)




我的代码
Java code
private void add(HttpServletRequest request,HttpServletResponse response) throws    ServletException, IOException {                Materiallist ins = new Materiallist();        String path = getServletContext().getRealPath("/");        String saveDirectory = path + "attch\\materiallist\\";        //String guid = PKGenerator.ReceiveGUID();        int is=0;        // 每个文件最大5m,最多10个文件,所以...        int maxPostSize = 10 * 30 * 1024 * 1024;        // response的编码为"gb2312",同时采用缺省的文件名冲突解决策略,实现上传            MultipartRequest multi = new MultipartRequest(request, saveDirectory,                maxPostSize, "utf-8");        String guid=multi.getParameter("guid");            String filedesc= multi.getParameter("filedesc");                String filelevel = multi.getParameter("filelevel");        String flag1=multi.getParameter("flag");                String mustflag = multi.getParameter("mustflag");        String projecttypeid = multi.getParameter("projecttypeid");        String name1=multi.getParameter("name");         int flag = 0;          Enumeration files = multi.getFileNames();                while (files.hasMoreElements()) {            //String mId = (String)list.get(flag);            String name = (String) files.nextElement();            File f = multi.getFile(name);            if (f != null) {                                                 String fileName = multi.getFilesystemName(name);                String filepath=saveDirectory+fileName;                if(f.length()<31457280){                     try {                         FileInputStream stream = new FileInputStream(f);                         byte[] b = new byte[(int)f.length()];                         stream.read(b);                         stream.close();                                              } catch (Exception e) {                         e.printStackTrace();                     }                                        if(f.isFile() && f.exists()){                        f.delete();                    }                }else{                    is = 1;                    if(f.isFile() && f.exists()){                        f.delete();                    }                }
  相关解决方案