用commons-fileupload包上传文件成功,却抱异常
如题,系统S1SH框架,文件上传用的是commons-fileupload包,文件上传总是成功,但是后台经常报org.apache.commons.fileupload.FileUploadException: Unexpected EOF read on the socket异常,并不是总是每次报,很奇怪,代码如下,请求各位大侠解答。源代码
public void uploadFile(IMessageObject mo) {
//String filepath = mo.getRequest().getSession().getServletContext().getRealPath("/test"); //容器相对路径
String htmlPath = ConfigProperties.getConfig("htmlpath");
File parentDir = makeFileDir(Long.valueOf(mo.getUser().getCurCompId()),htmlPath);
String tempDir = CommonUtil.getParamValueByName((long)0, "attach_dir");
File tempFile = makeFileDir(Long.valueOf(mo.getUser().getCurCompId()),tempDir);
DiskFileItemFactory factory = new DiskFileItemFactory(); //创建磁盘工厂
factory.setRepository(tempFile); //文件缓存路径
factory.setSizeThreshold(10 * 1096 );
ServletFileUpload uploadx = new ServletFileUpload(factory); //创建处理工具
uploadx.setSizeMax(10*1024*1024); //服务器端可以接收的最大文件大小,-1表示无上限
uploadx.setHeaderEncoding("UTF-8");
//4.如果是文件上传表单的话,使用upload组件解析request
try {
//@SuppressWarnings("unchecked")
HttpServletRequest request = mo.getRequest();
List<FileItem> list = uploadx.parseRequest(request);
for(FileItem item : list){
//5.判断当前的fileitem是文件上传还是普通字段
if(item.isFormField()){
//是普通字段
String name = item.getFieldName();//获取输入项的名称
String value = item.getString(); //获取输入项的值
//System.out.println(name + "=" + value);
}else{
//是上传文件
String filename = item.getName(); //获取上传文件名 c:\\1.txt
int pos = filename.lastIndexOf("\\");
if(pos>0){
filename = filename.substring(pos+1);
}
if(!filename.endsWith(".html")){
//异常代码
break;
}
InputStream in = item.getInputStream(); //获取数据流
String newFileName = parentDir.getAbsolutePath() + "\\" + filename;
FileOutputStream out = new FileOutputStream(newFileName);
try{
int len = 0;
byte buffer[] = new byte[1024];
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}finally {
in.close();
out.close();
}
makeFormFile(newFileName,mo);
}
}
} catch (Exception e) {
e.printStackTrace(); //log4j 如果抛异常,在给用户友好提示的同时,服务器也要记录异常信息
//request.setAttribute("message", "上传文件失败!!");
//request.getRequestDispatcher("/message.jsp").forward(request, response);
}
异常代码
--------------------编程问答-------------------- http://d7.qjwm.com/download.aspx?down=ok&filepath=leoadmin2134%2fuploadDemo.zip
org.apache.commons.fileupload.FileUploadException: Unexpected EOF read on the socket
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:381)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at com.unicom.etax.module.form.biz.impl.FormDesignBizImpl.uploadFile(FormDesignBizImpl.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.ibm.gbs.core.util.CommonUtil.dynMethod(CommonUtil.java:525)
...
Caused by: java.io.EOFException: Unexpected EOF read on the socket
at org.apache.coyote.http11.InternalNioInputBuffer.readSocket(InternalNioInputBuffer.java:623)
at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:902)
...
基于commons-fileupload的上传Demo,可以直接用,代码简洁易懂,很好引用。
补充:Java , Java EE