解决当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,如需转载请自行联系原作者
相关文章
|
2月前
|
Java
【Java报错】MultipartFile 类型文件上传 Current request is not a multipart request 问题处理(postman添加MultipartFile)
【Java报错】MultipartFile 类型文件上传 Current request is not a multipart request 问题处理(postman添加MultipartFile)
303 0
|
2月前
|
前端开发 JavaScript
Error_ Multipart_ Boundary not foun
Error_ Multipart_ Boundary not foun
61 0
|
9月前
|
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的解决方案
104 0
|
Java API 容器
理解HTTP协议中的multipart/form-data
之前在写一个通用HTTP组件的时候遇到过媒体(Media)类型multipart/form-data的封装问题,这篇文章主要简单介绍一下HTTP协议中媒体类型multipart/form-data的定义、应用和简单实现。
794 0
理解HTTP协议中的multipart/form-data
|
前端开发 JavaScript Java
【前端】form标签multipart/form-data 文件上传表单中 传递参数无法获取的原因
form标签multipart/form-data 文件上传表单中 传递参数无法获取的原因
877 1
|
Python
Python Request POST 上传文件 Multipart/form-data
Python Request POST 上传文件 Multipart/form-data
694 0
Python Request POST 上传文件 Multipart/form-data
|
前端开发
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
.NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
400 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
359 0
Posted content type isn't multipart/form-data
Posted content type isn't multipart/form-data
466 0