项目是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内容后,问题解决了,是这么说的。
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.
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.
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)不行呢?高人来解释啊( 这段话我说的有点乱,表达能力太差啦)
分享到:
相关推荐
webwork中上传文件,webwork文件上传.doc
WebWork 文件上传 多个文件上传 范例
简单的使用webwork框架实现了文件上传的功能 主要是针对EXCEL文件
项目里需要实现webwork 上传与下载文件操作, 自己摸索过程中出现了很多问题,一一解决, 记录下来本准备做参考,现贡献给大家, 记得有点乱,耐心看完,一定会有帮助的。
NULL 博文链接:https://xiaoqiang21.iteye.com/blog/763674
fileupload+dwr2+webwork2实现带进度条上传文件
本文主要从三个方面给大家介绍webwork文件上传下载知识,包括以下三个方面: 1. 包装 Request 请求 2. 获取文件上传的解析类 3. 项目实战配置和使用 Web上传和下载应该是很普遍的一个需求,无论是小型网站还是大...
NULL 博文链接:https://cxl2012.iteye.com/blog/2011487
webwork上传文件pdf文件说明文档
NULL 博文链接:https://sgl124764903.iteye.com/blog/812973
使用webwork上传excel并导入数据库,上传时只能上传excel文件,数据库为mysql,可以根据需要自行修改。 数据库的配置也请自行修改。
第1章 Struts 2概述,第2章 Struts 2下的HelloWorld,第3章 Struts 2基础,第4章 深入Struts 2,第5章 Struts 2的类型转换,第6章 文件的上传和下载.,第7章 Struts 2的拦截器,第8章 Struts 2的输入校验,9.2 ...
NULL 博文链接:https://goowolf.iteye.com/blog/513688
关键是正对最新版webwork,在action中进行操作,说得很细致,有源代码实例。也将异常情况处理列出来了。
《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第68个包。
该两个文件是基于webwork框架的两个文件上传包,两个缺一个不可,里面包含各自的源文件
Struts 2权威指南--基于WebWork核心的MVC开发.pdf 12卷,最后要下载我上传的 "海啸文件分割器V1.0.rar"或自行去网站下载进行12卷的合并..谢谢
真正的《webwork in action》,共三部分,放在一起解压即可。 因为一次上传不了那么大的文件,所以有些麻烦。
struts2概述 strust2下的HelloWorld struts2基础 深入struts2 struts2类型转换 文件的上传和下载 ... 这本书太大了,我把它分为了12部分上传,给大家造成的不便,还请谅解