List items = upload.parseRequest(request),items为空

大卷小渣渣 2012-07-11 05:01:57
jsp的form里面这样写的:
<html:form method="post" action="/t04_report/t04_report_list.do" enctype="multipart/form-data">
Action里面这样写的:
private ActionForward performImportReports(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存缓冲区,超过后写入临时文件
factory.setSizeThreshold(10240000);
// 设置临时文件存储位置
String base = this.getServletContext().getRealPath("/")+"files";
System.out.println("--------base----------------->"+base);
File file = new File(base);
System.out.println("------------>"+file.exists());
if(!file.exists()){
file.mkdirs();
}
factory.setRepository(file);
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置单个文件的最大上传值
upload.setFileSizeMax(10002400000l);
// 设置整个request的最大值
upload.setSizeMax(10002400000l);
upload.setHeaderEncoding("gb2312");
try {
List<?> items = upload.parseRequest(request);
System.out.println("itemsSize--------------->"+items.size());//值为0,纠结啊。。。
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();
System.out.println("------------------>"+item.getFieldName());
// 保存文件
System.out.println("--------fileName-------->"+fileName);

if (!item.isFormField() && item.getName().length() > 0) {
item.write(new File(fileName));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

List items = upload.parseRequest(request),items为空,不知道怎么办了,求大哥们帮忙解决啊!
...全文
2038 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lion_teng 2015-03-24
  • 打赏
  • 举报
回复
求救,没用框架的咋办
lion_teng 2015-03-24
  • 打赏
  • 举报
回复
我什么框架都没有用呀,怎么改Web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
jack__ming 2015-01-27
  • 打赏
  • 举报
回复
那struts1呢?
加州小马哥 2014-12-17
  • 打赏
  • 举报
回复
大家好,遇到这个问题的朋友们。 如果用的是springmvc的架构,那么注释掉掉springmvc文件上传的配置即可; 如果用struts2的朋友们,在一楼的基础上,将constant name= "struts.multipart.handler" 改为constant name= "struts.multipart.parserr"即可。
lhz4023 2014-10-09
  • 打赏
  • 举报
回复
就是没有把common-io.jar包导进去
降龙108掌 2014-03-16
  • 打赏
  • 举报
回复
为啥我那样加进去了,提示No result defined for action 去掉<bean type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest" name= "myRequestParser" class= "com.icf.common.base.actions.RequestParseWrapper" scope= "default" optional= "true " /> <constant name= "struts.multipart.handler" value= "myRequestParser" /> 的配置,程序能进入action,只是没能获取上传的文件
lsw0419 2014-02-17
  • 打赏
  • 举报
回复
十分感谢,终于解决了
泡面王 2013-08-06
  • 打赏
  • 举报
回复
引用 1 楼 dinglongfei 的回复:
在status2 的配置文件里加上 <bean type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest" name= "myRequestParser" class= "com.icf.common.base.actions.RequestParseWrapper" scope= "default" optional= "true " /> <constant name= "struts.multipart.handler" value= "myRequestParser" /> class public class RequestParseWrapper extends JakartaMultiPartRequest { publicvoid parse(HttpServletRequest servletRequest, String saveDir)throws IOException{ } } 这样就可以得到request里的item
一楼的方法有用,感谢,搞了半天了,终于出来了。谢了。
liqifei009 2013-03-15
  • 打赏
  • 举报
回复
一楼大侠的办法很好,解决了我纠结以旧的问题,在这里谢了
  • 打赏
  • 举报
回复
public void uploadFile(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
HttpSession session=request.getSession();
session.setAttribute("progressBar",0); //定义指定上传进度的Session变量
String error = "";
int maxSize=50*1024*1024; //单个上传文件大小的上限
DiskFileItemFactory factory = new DiskFileItemFactory(); //基于磁盘文件项目创建一个工厂对象
ServletFileUpload upload = new ServletFileUpload(factory); //创建一个新的文件上传对象
try {
List items = upload.parseRequest(request);// 解析上传请求
Iterator itr = items.iterator();// 枚举方法
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next(); //获取FileItem对象
if (!item.isFormField()) {// 判断是否为文件域
if (item.getName() != null && !item.getName().equals("")) {// 判断是否选择了文件
long upFileSize=item.getSize(); //上传文件的大小
String fileName=item.getName(); //获取文件名
//System.out.println("上传文件的大小:" + item.getSize());
if(upFileSize>maxSize){
error="您上传的文件太大,请选择不超过50M的文件";
break;
}
// 此时文件暂存在服务器的内存中
File tempFile = new File(fileName);// 构造临时对象
String uploadPath = this.getServletContext().getRealPath("/upload");
File file = new File(uploadPath,tempFile.getName()); // 获取根目录对应的真实物理路径
InputStream is=item.getInputStream();
int buffer=1024; //定义缓冲区的大小
int length=0;
byte[] b=new byte[buffer];
double percent=0;
FileOutputStream fos=new FileOutputStream(file);
while((length=is.read(b))!=-1){
percent+=length/(double)upFileSize*100D; //计算上传文件的百分比
fos.write(b,0,length); //向文件输出流写读取的数据
session.setAttribute("progressBar",Math.round(percent)); //将上传百分比保存到Session中
}
fos.close();
Thread.sleep(1000); //线程休眠1秒
} else {
error="没有选择上传文件!";
}
}
}
} catch (Exception e) {
e.printStackTrace();
error = "上传文件出现错误:" + e.getMessage();
}
if (!"".equals(error)) {
request.setAttribute("error", error);
request.getRequestDispatcher("error.jsp").forward(request, response);
}else {
request.setAttribute("result", "文件上传成功!");
request.getRequestDispatcher("upFile_deal.jsp").forward(request, response);
}
看看这个例子吧
  • 打赏
  • 举报
回复
enctype="multipart/form-data"去掉就可以了
ithoney 2012-09-26
  • 打赏
  • 举报
回复
同样错误,纠结中~
dinglongfei 2012-08-29
  • 打赏
  • 举报
回复
在status2 的配置文件里加上
<bean type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest"
name= "myRequestParser" class= "com.icf.common.base.actions.RequestParseWrapper"
scope= "default" optional= "true " />
<constant name= "struts.multipart.handler" value= "myRequestParser" />
class
public class RequestParseWrapper extends JakartaMultiPartRequest {

publicvoid parse(HttpServletRequest servletRequest, String saveDir)throws IOException{
}
}
这样就可以得到request里的item

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧