NutzCN Logo
问答 fileupload多文件上传报错
发布于 2711天前 作者 qq_34441c67 4487 次浏览 复制 上一个帖子 下一个帖子
标签:

我们公司目前做的项目采用nutz框架,我现在需要做一个多文件上传功能,前端用的swfupload插件,后台用的是fileupload,其他项目使用这两个插件没有任何问题。在nutz框架里面使用时,一直报一个错误,Stream closed,想问问是怎么回事?对nutz框架还不是太熟悉。求大神指教@wendal。

/**
	 * 文件上传
	 */
	@POST
	@AdaptBy(type = UploadAdaptor.class,args = {"ioc:upload"}) 
	@At
	public void uploadFile(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//System.out.println("name====>"+request.getParameter("name"));
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//设置内存缓冲区,超过后写入临时文件
		factory.setSizeThreshold(10240000);
		//设置临时文件存储位置
		String base = "f:/uploadFiles";
		File file = new File(base);
		if(!file.exists())
			file.mkdirs();
		factory.setRepository(file);
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 设置单个文件的最大上传值
		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();
		}
	}

异常信息:
org.apache.commons.fileupload.FileUploadException: Stream closed
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:369)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at cm.core.util.FileUpload.uploadFile(FileUpload.java:53)
at cm.core.util.FileUpload$FM$uploadFile$62febe830a4a6764c634d9e263964cee.invoke(FileUpload.java)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at cm.core.mvc.LogTimeProcessor.process(LogTimeProcessor.java:23)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
at cm.core.mvc.MyNutFilter.doFilter(MyNutFilter.java:37)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at cm.core.util.XFilter.doFilter(XFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

7 回复

你是打算自行处理上传流,不打算用nutz内置的适配器??

开始我是准备采用nutz内置的适配器,只能完成一个文件的上传,达不到要求,我如果在前台使用swfupload插件,后端使用nutz内置的适配器。结果获取不到上传流,这是测试的代码:参数有改动,改动之前上传单个文件没有问题,
@POST
//@AdaptBy(type = UploadAdaptor.class,args = {"ioc:upload"})
@At
public Object uploadFile(TempFile tempFile) throws SQLException, IOException {
try {
String fileName = tempFile.getSubmittedFileName();//上传文件名
String uploadPath = this.getReq().getServletContext().getRealPath(this.directory);
File file = tempFile.getFile();//上传文件
String fullPath = uploadPath+"\" + fileName;
// 判断文件夹是否存在,不存在则创建
File dirTest = new File(fullPath);
if (!dirTest.exists()) {
dirTest.mkdirs();
}
File target = new File(fullPath);
Files.copy(file, target);//复制文件到指定位置
//设置上传文件信息
SysUploadFiles uploadFiles = new SysUploadFiles();
uploadFiles.setFilename(fileName.substring(0,fileName.indexOf(".")));//文件名称
uploadFiles.setFileurl("http://localhost:8080" + this.getReq().getContextPath() + this.directory + "/" + fileName);//上传路径
uploadFiles.setFiletype(fileName.substring(fileName.indexOf(".")+1));//文件类型
uploadFiles.setUploaddatetime(super.getTime());//上传时间
uploadFiles.setUploaduser(super.getLoginUser().getUserid());//上传人id
SysUploadFiles insert = super.dao.insert(uploadFiles);
return super.nutzRet(0, "上传成功!",insert.getFileid());//返回id
} catch (Exception e) {
return super.nutzRet(-1, "上传失败,请稍后重试!",null);
}
}

@Param("files")TempFile[] tempFile

其中files是表单名称

如果自行处理上传流, 要设置为

@AdaptBy(type = VoidAdaptor.class) 

非常感谢,问题已经解决了。自己处理上传流的话是需要你说的那个配置,如果使用nutz内置的配置,需要在添加一个参数Filedata
@POST
@AdaptBy(type = UploadAdaptor.class,args = {"ioc:upload"})
@At
public Object uploadFile(@Param("Filedata")TempFile tempFile) throws SQLException, IOException {}
像上面这样就可以了,swfupload上传插件就可以正常用了。我还是打算采用nutz内置的适配器。非常感谢@wendal。

@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);
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 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

Stream closed 跟上面一样么

@qq_03a9ff23 单独发帖.

添加回复
请先登陆
回到顶部