webwork2文件上传

       项目是ssh的,有个功能是文件上传,文件上传以前也做过,不过很久了,记忆模糊了。。。。嘿嘿,自己给自己找理由,跑题啦!

       上传功能对应的action配置fileUpload拦截器,而我又习惯性的加入了common-fileupload.jar,想用它来实现文件上传,AttachUpLoadAction代码片段如下

request = ServletActionContext.getRequest();
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		DiskFileItemFactory factory = new DiskFileItemFactory();   
//		 当文件大小超过300k时,就在磁盘上建立临时文件   
		factory.setSizeThreshold(300000);   
		//设计文件上传的临时目录   
		factory.setRepository(new File("D:\\temp"));   
		ServletFileUpload upload = new ServletFileUpload(factory);   
		// 文件大小不能超过10M   
		upload.setSizeMax(10000000);
//		 Parse the request
		List items = upload.parseRequest(request);
//		upload.setProgressListener(new ProgressListener());
		System.out.println("#####################items.size()=="+items.size());

 

        开始不知道为什么,items.size()一直为0,而通过request.getParameter("meetingId")取得一个text类型的input框参数,则可以取到,而boolean isMultipart = ServletFileUpload.isMultipartContent(request);
返回的也是true,为什么size还是0呢?

      在查阅了webwork api中关于FileUploadInterceptor的部分内容以及http://commons.apache.org/fileupload/faq.html内容后,问题解决了,是这么说的。

Why is parseRequest() returning no items?

This most commonly happens when the request has already been parsed, or processed in some other way. Since the input stream has aleady been consumed by that earlier process, it is no longer available for parsing by Commons FileUpload.

I'm using FileUpload in an Action, but it's not working. Why?

Struts recognises multipart requests, and parses them automatically, presenting the request parameters to your code in the same manner as if they were regular request parameters. Since Struts has already processed the request, and made it available in your form bean, the input stream is no longer available for parsing, so attempting to do so with FileUpload will fail.

But I need to parse the request myself. How can I do that?

Struts parses multipart a request as a part of the process of populating your form bean from that request. If, for some reason, you need to have full control over the multipart parsing, you can do so by configuring your action mapping without an associated form bean. (A better way of doing this, however, is to replace the default multipart handler with your own. See the Struts documentation for details.)

 

针对第一个问题的解释,可我还是有点疑问,虽说webwork2通过对request等对象进行了解耦,可以不依赖容器进行测试,但是我们知道action中也是可以通过request = ServletActionContext.getRequest();取得request 等对象,直接进行操作的吗?为什么request.getParameter("meetingId")可以得到元素,而  List items = upload.parseRequest(request)不行呢?高人来解释啊( 这段话我说的有点乱,表达能力太差啦)

 

你可能感兴趣的:(apache,bean,struts,ssh,Webwork)