struts1.2中ActionForm和ServletFileUpload.parseRequest(request)不能同时使用
2011-11-25 09:20
417 查看
一个问题。在back_add.jsp中把需要保存的内容放在"opinion"中,然后在action中获取的时候,用request.getParameter("opinion")可以正确获得值但是用para.get("opinion")就死活取不到值,具体代码如下:Map<String, String> para = new HashMap<String, String>();request.setCharacterEncoding("UTF-8");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();if (item.isFormField()){para.put(item.getFieldName(), item.getString("UTF-8"));}else{if (item.getName() != null && !item.getName().equals("")){int last = item.getName().lastIndexOf("\\");String temString = item.getName().substring(last + 1)+ "/";String read = item.getName().substring(last + 1);String suffix = read.substring(read.lastIndexOf("."));String uuid = java.util.UUID.randomUUID().toString();temString = uuid + suffix + "*" + temString;enclosure = enclosure + temString;File tempFile = new File(item.getName());File file = new File(servlet.getServletContext().getRealPath("/")+ "\\upload\\", uuid + suffix);item.write(file);}}}}catch (Exception e){e.printStackTrace();}这部分代码还包含了对上传的附件的处理。到这里,要用para.get("opinion")就会报空指针异常。一直没想通,在网上也没搜到满意的解决办法,把用到的方法都拆开看了,也没看出什么不妥,一句一句的测试,到upload还貌似正确,但是items怎么都为空……今天终于在网上看到一个哥们儿答到点子上了~原来,ActionForm和ServletFileUpload.parseRequest(request)是不能同时使用的!!!!!!!!!!!!网上匿名高人是这么解释的:
解释struts用ActionForm的方式处理上传附件的一些问题,struts接收到enctype="multipart/form-data"的post请求后,会看那个对应的action有没有配置actionform,如果配置了,就会作一些处理,所以你在action里得到的request已经不是一个普通的request了,而是一个被封装过的request。如果想得到原始的request,就不要struts-config.xml里给action类配置actionform。 ServletFileUpload.parseRequest(request)中的request用的是普通的request,而使用actionForm时request被封装,从而导致ServletFileUpload.parseRequest(request)取不到值,为空。目前来说,无法解决ActionForm和ServletFileUpload.parseRequest(request)共存问题,那只能换别的上传方式了!
引用自:http://zhidao.baidu.com/question/196663366.html?fr=qrl&cid=870&index=5
为了这个,不知道花了多少时间……要是早点知道就好了,唉……
相关文章推荐
- struts1.2中ActionForm和ServletFileUpload.parseReq...
- ServletFileUpload.parseRequest()怎么得不到请求内容
- struts接收url文件使用upload.parseRequest 获取文件为空的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- Controller中servletFileUpload.parseRequest(request)解析为空
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- ServletFileUpload.parseRequest()得不到请求内容
- kindeditor上传文件在SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题 .
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- 使用bootstrap-fileinput的问题:上传时upload.parseRequest(request)得到的List为空
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- ServletFileUpload.parseRequest()为空
- servletFileUpload.parseRequest(request)报错解决办法
- 开发日志:struts2使用commons.fileupload上传附件,并解决upload.parseRequest(request)为空的问题
- 使用commons-fileupload request.getParameter()取不到其他参数的解决
- 使用ajaxfileupload同时上传多个文件