* 文件上传
*/
@POST
/*@AdaptBy(type = UploadAdaptor.class,args = {"ioc:upload","${app.root}/WEB-INF/tmp"}) */
@AdaptBy(type = UploadAdaptor.class,args = {"ioc:myUpload"})
@At("/addfile")
@Ok("jsp:/WEB-INF/admin/official/official-edit.jsp")
public void uploadFile(@Param("Filedata")TempFile[] tempFile,HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Item工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
//设置内存缓冲区,超过后写入临时文件
factory.setSizeThreshold(10240000);
//设置临时文件存储位置
String base = "f:/uploadFiles";//上传路径
File file = new File(base);
if(!file.exists())
file.mkdirs();
factory.setRepository(file);
// 设置单个文件的最大上传值
upload.setFileSizeMax(10002400000l);
// 设置整个request的最大值
upload.setSizeMax(10002400000l);
upload.setHeaderEncoding("UTF-8");
try {
List<?> items = upload.parseRequest(request);//
FileItem item = null;
String fileName = null;
for (int i = 0 ;i < items.size(); i++){
item = (FileItem) items.get(i);
fileName = base + File.separator + item.getName();
// 保存文件
if (!item.isFormField() && item.getName().length() > 0) {
item.write(new File(fileName));//写入文件
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Caused by: java.io.IOException: Stream closed
at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:312)
at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:134)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:999)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:903)
at java.io.InputStream.read(InputStream.java:101)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:593)
at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:617)
at org.apache.commons.fileupload.MultipartStream.skipPreamble(MultipartStream.java:634)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:1023)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:1003)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
... 35 more