解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法

简介:

 from表单中向后台传递参数的同时要上传文件,所以ENCTYPE="multipart/form-data" 必须要加在form里面,可是这样的话,我在servlet里面用request.getParameter()方法无论如何都只是获得null值.

我这里使用的是commons fileupload的jar包(commons-fileupload-1.2.jar).

 先附录之前写的上传文件的工具类:

复制代码
 1 public class FileUploadAndDown {
 2     
 3     private Logger logger = Logger.getLogger(FileUploadAndDown.class);
 4     
 5     public String upload(ServletConfig servletconfig, HttpServletRequest request)throws FileUploadException{
 6         HttpSession session = request.getSession();
 7         String username = session.getAttribute("username").toString();
 8         DiskFileUpload fu = new DiskFileUpload();
 9         // 判断表单是否设置了mutipart/form-data
10         boolean isMultipart = DiskFileUpload.isMultipartContent(request);
11         if (!isMultipart){
12             return null;
13         }else{
14             fu.setHeaderEncoding(request.getCharacterEncoding());
15             // 设置允许用户上传文件大小,单位:字节
16             fu.setSizeMax(10*1024*1024);
17             // 设置最多只允许在内存中存储的数据,单位:字节
18             fu.setSizeThreshold(4*1024*1024);
19             // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
20             String temp = System.getProperty("user.home")+File.separator+"catalogupload";
21             fu.setRepositoryPath(temp);
22             File file = new File(temp);
23             if (!file.exists() || !file.isDirectory()) {
24                 file.mkdir();
25             }
26             // 开始读取上传信息
27 
28             List fileItems = fu.parseRequest(request);
29             // 依次处理每个上传的文件
30 
31             Iterator iter = fileItems.iterator();
32             // 如果需要上传多个文件,将if改称while即可
33             while(iter.hasNext()) {
34                 FileItem item = (FileItem) iter.next();
35                 // 忽略其他不是文件域的所有表单信息
36 
37                 if (!item.isFormField()) {
38                     String name = item.getName();
39                     String time = DateUtil.getDateTime("yyyyMMddHHmmss");
40                     name = username + "_" + time + FileNameUtil.extractFileExt(name);
41                     try {
42                         String namepath = servletconfig.getServletContext().getRealPath("poster");
43                         file = new File(namepath);
44                         if(!file.exists()){
45                             file.mkdirs();
46                         }
47                         name = namepath+File.separator+name;
48                         item.write(new File(name));
49                         logger.info("上传海报名为:" + name);
50                         return name;
51                     } catch (Exception e) {
52                         logger.error("上传海报保存异常" + e.getMessage());
53                         return null;
54                     }
55                 }
56             }
57         }
58         return "";
59     }
60 }
复制代码

 

对应的jsp页面

复制代码
 1         <form name="form1" action="deleteMmobject.do" method="post" enctype="multipart/form-data" target="displayprocess" >
 2             导入要删除列表文件 &nbsp;&nbsp;&nbsp;    
 3             <input type="hidden" id="deleteFlagHidden" name="deleteFlagHidden" value="" />
 4             <input type="hidden" name="action" value="" />
 5             
 6             <input type="file" name="filepath" />
 7             <input name="deleteMmobject_radio" id="selfDelete" type="radio" value="自删除" onclick="selfDeleteFun()"; checked />自删除
 8             <input name="deleteMmobject_radio" id="sendToBoDelete" type="radio" value="同时下发删除到下游" onclick="sendToBoDeleteFun()" />同时下发删除到下游
 9             <input type="button" value="确定" onclick="deleteFun()"/>
10             <!-- <input type="submit"  value="确定" /> -->
11         </form>
复制代码

 

通过servlet里面用request.getParameter("id名称")方法无论如何都只是获得null值.   通过更换上传依赖的jar包(jspsmartupload.jar)

新加代码如下:

复制代码
 1     public Map<String,String> uploadUseJspSmart(ServletConfig servletconfig, HttpServletRequest request, HttpServletResponse response)throws FileUploadException{
 2         Map<String,String> uploadParametersMap = new HashMap<String,String>(); 
 3         HttpSession session = request.getSession();
 4         String username = session.getAttribute("username").toString();
 5         
 6         com.jspsmart.upload.SmartUpload smartUpload = new com.jspsmart.upload.SmartUpload();
 7         File file = null;
 8         String fileName = null;
 9         try {
10             smartUpload.initialize(servletconfig, request, response);
11             smartUpload.setTotalMaxFileSize(100000000);
12             smartUpload.setAllowedFilesList("txt,excel");
13             smartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
14             
15             smartUpload.upload();
16             // 得到上传的文件对象
17             com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
18             
19             fileName = smartFile.getFileName();
20             String time = DateUtil.getDateTime("yyyyMMddHHmmss");
21             fileName = username + "_" + time + FileNameUtil.extractFileExt(fileName);
22             String namepath = System.getProperty("user.home")+File.separator+"catalogupload";
23             
24             file = new File(namepath);
25             if(!file.exists()){
26                 file.mkdirs();
27             }
28             fileName = namepath+File.separator+fileName;
29             smartFile.saveAs(fileName);
30             logger.info("通过JspSamrt上传文件名为:" + fileName+"成功");
31             //这里可以顺利得到jsp页面form表单中对应的"deleteFlagHidden"参数
32             String deleteFlagHidden = smartUpload.getRequest().getParameter("deleteFlagHidden");
33             uploadParametersMap.put("fileName", fileName);
34             uploadParametersMap.put("deleteFlagHidden", deleteFlagHidden);
35             return uploadParametersMap;
36         } catch (Exception e1) {
37             e1.printStackTrace();
38             logger.info("通过JspSamrt上传文件名为:" + fileName+"失败");
39         }
40         return null;
41     }
复制代码

 



本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/6839772.html,如需转载请自行联系原作者
相关文章
|
6月前
|
Java
【Java报错】MultipartFile 类型文件上传 Current request is not a multipart request 问题处理(postman添加MultipartFile)
【Java报错】MultipartFile 类型文件上传 Current request is not a multipart request 问题处理(postman添加MultipartFile)
558 0
|
JSON 数据格式
Content type ‘multipart/form-data;boundary=------57031299820747271;charset=UTF-8‘ not supported的解决方案
Content type ‘multipart/form-data;boundary=------57031299820747271;charset=UTF-8‘ not supported的解决方案
246 0
|
前端开发 JavaScript Java
【前端】form标签multipart/form-data 文件上传表单中 传递参数无法获取的原因
form标签multipart/form-data 文件上传表单中 传递参数无法获取的原因
1021 1
|
Python
Python Request POST 上传文件 Multipart/form-data
Python Request POST 上传文件 Multipart/form-data
745 0
Python Request POST 上传文件 Multipart/form-data
|
前端开发
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
425 0
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
|
Java
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
401 0
Posted content type isn't multipart/form-data
Posted content type isn't multipart/form-data
513 0
|
Python
使用requests库提交multipart/form-data 格式的请求
前言: Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。
1572 0