java.io.IOException: Corrupt form data: premature ending
at com.oreilly.servlet.multipart.MultipartPr.<ini t>(MultipartPr.java:207)
at com.oreilly.servlet.MultipartRequest.<init>(Multip artRequest.java:223)
at com.tgyt.permissions.upload.upload.swf.SwfUploadSe rvlet.doPost(SwfUploadServlet.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrep areAndExecuteFilter.doFilter(StrutsPrepareAndExecu teFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88)
at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
at org.apache.shiro.web.servlet.ProxiedFilterChain.do Filter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeC hain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilter Internal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter. doFilter(OncePerRequestFilter.java:81)
at org.apache.shiro.web.servlet.ProxiedFilterChain.do Filter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.e xecuteChain(AbstractShiroFilter.java:359)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1 .call(AbstractShiroFilter.java:275)
at org.apache.shiro.subject.support.SubjectCallable.d oCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.c all(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject .execute(DelegatingSubject.java:344)
at org.apache.shiro.web.servlet.AbstractShiroFilter.d oFilterInternal(AbstractShiroFilter.java:272)
at org.apache.shiro.web.servlet.OncePerRequestFilter. doFilter(OncePerRequestFilter.java:81)
at org.springframework.web.filter.DelegatingFilterPro xy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterPro xy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

web.xml:
<!--swfUploader -->
<servlet>
<servlet-name>swfUploader</servlet-name>
<servlet-class>com.tgyt.permissions.upload.upload.swf.SwfUp loadServlet</servlet-class>
<!--上传的文件大小上限 单位M-->
<init-param>
<param-name>FILE_SIZE_LIMIT</param-name>
<param-value>1000</param-value>
</init-param>
<!--一次性上传的文件个数上限-->
<init-param>
<param-name>FILE_COUNT_LIMIT</param-name>
<param-value>10</param-value>
</init-param>
<!--请求响应编码格式-->
<init-param>
<param-name>ENCODING</param-name>
<param-value>utf-8</param-value>
</init-param>
<!--是否使用自定义目录 false 则保存到yyyy/mm/dd下,true 则保存到rootUpload-->
<init-param>
<param-name>USE_CUSTOMDIR</param-name>
<param-value>false</param-value>
</init-param>
<!--自定义的文件保存目录-->
<init-param>
<param-name>ROOT_UPLOADDIR</param-name>
<param-value>rootUpload</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>swfUploader</servlet-name>
<url-pattern>/swf/upload</url-pattern>
</servlet-mapping>
<!--swfUploader -->

SwfUploadServlet.java

package com.tgyt.permissions.upload.upload.swf;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tgyt.permissions.upload.constant.Constant;
import com.tgyt.permissions.upload.upload.core.DefaultFil eRenamePolicy;
import com.tgyt.permissions.upload.upload.core.ISwfUpload Servlet;
import com.tgyt.permissions.upload.utils.BlankUtil;
import com.tgyt.permissions.upload.vo.ConstantParameterVo ;
import com.oreilly.servlet.MultipartRequest;

SuppressWarnings("serial")
public class SwfUploadServlet extends HttpServlet implements ISwfUploadServlet{

protected final Log logger = LogFactory.getLog(getClass());

private ConstantParameterVo parameters = null;

public void init(ServletConfig config) throws ServletException {
logger.info("--- init Parameter begin---");
// 初始化参数

if (BlankUtil.isBlank(parameters)) {
logger.info("--- initing Parameter ---");
parameters = new ConstantParameterVo(config);
}
logger.info("--- init Parameter end---");
}

SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("--- BEGIN SwfUpload DOPOST ---");
request.setCharacterEncoding(parameters.getEncodin g());
response.setCharacterEncoding(parameters.getEncodi ng());
response.setContentType("text/plain;cht=utf-8");
logger.info("--- do somthing before upload in doBeforeUpload method begin---");
doBeforeUpload(request, response);
logger.info("--- do somthing before upload in doBeforeUpload method end---");

parameters.init(request);
DefaultFileRenamePolicy rename = generateFileRenamePolicy();
MultipartRequest multi = null;
try {
System.out.println("parameters.getSaveDirectory(): :::" + parameters.getSaveDirectory());
System.out.println("parameters.getMaxPostSize()::: :" + parameters.getMaxPostSize());
System.out.println("parameters.getEncoding()::::" + parameters.getEncoding());
System.out.println(request.getMethod());
multi = new MultipartRequest(request, parameters.getSaveDirectory(), parameters.getMaxPostSize(),
parameters.getEncoding(), rename);
renderJson(response, Constant.UPLOAD_SUCCESS);
}
catch (IOException e) {
if (!BlankUtil.isBlank(e.getMessage())) {
int index = e.getMessage().indexOf("Posted content length of");
if (index > -1) {
renderJson(response, Constant.FILE_OVERSETSIZE);
}
else {
if (e instanceof IOException) {
renderJson(response, Constant.IO_ERROR);
}
else {
renderJson(response, Constant.UPLOAD_FAILED);
}
}
logger.error(e.getMessage(), e.getCause());
}
else {
logger.info("--- upload file upload error ---");
deleteErrorFile(parameters.getSaveDirectory() + rename.getFileSaveName());
}
e.printStackTrace();
return;
}
catch (Exception e) {
e.printStackTrace();
return;
}
// 输出
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);
String lastFileName = parameters.getSaveDirectory() + File.separatorChar + fileName;
String fileSavePath = parameters.getUploadDir() + parameters.getAutoCreatedDateDir() + fileName;
logger.info("fileName:" + fileName);
logger.info("file address:" + lastFileName);
logger.info("save path:" + fileSavePath);
}
}
logger.info("--- do somthing after upload in doAfterUpload method begin---");
doAfterUpload(multi);
logger.info("--- do somthing after upload in doAfterUpload method end---");
logger.info("--- END SwfUpload DOPOST ---");
}

private void deleteErrorFile(String errorFilePath) {
logger.info("errorFilePath:" + errorFilePath);
logger.info("--- delete error file begin---");
File file = new File(errorFilePath);
file.delete();
logger.info("--- delete error file end---");
}

public DefaultFileRenamePolicy generateFileRenamePolicy() {
return new DefaultFileRenamePolicy();
}

public void doAfterUpload(MultipartRequest multi) {}

public void doBeforeUpload(HttpServletRequest request, HttpServletResponse response) {}

public void renderJson(HttpServletResponse response, String msgKey) {
try {
response.setContentType("text/x-json;cht=UTF-8");
response.getWriter().write(msgKey);
response.flushBuffer();
}
catch (IOException e) {
logger.error(e.getMessage(), e.getCause());
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}

public void destroy() {
this.parameters = null;
super.destroy();
}

}