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

部分转载于:

http://blog.csdn.net/georgejin/article/details/1706647

http://www.cnblogs.com/loveyunk/p/6089032.html

http://www.cnblogs.com/greenteaone/p/4186619.html

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

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

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

 public class FileUploadAndDown {

     private Logger logger = Logger.getLogger(FileUploadAndDown.class);

     public String upload(ServletConfig servletconfig, HttpServletRequest request)throws FileUploadException{
HttpSession session = request.getSession();
String username = session.getAttribute("username").toString();
DiskFileUpload fu = new DiskFileUpload();
// 判断表单是否设置了mutipart/form-data
boolean isMultipart = DiskFileUpload.isMultipartContent(request);
if (!isMultipart){
return null;
}else{
fu.setHeaderEncoding(request.getCharacterEncoding());
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10*1024*1024);
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4*1024*1024);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
String temp = System.getProperty("user.home")+File.separator+"catalogupload";
fu.setRepositoryPath(temp);
File file = new File(temp);
if (!file.exists() || !file.isDirectory()) {
file.mkdir();
}
// 开始读取上传信息 List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件 Iterator iter = fileItems.iterator();
// 如果需要上传多个文件,将if改称while即可
while(iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表单信息 if (!item.isFormField()) {
String name = item.getName();
String time = DateUtil.getDateTime("yyyyMMddHHmmss");
name = username + "_" + time + FileNameUtil.extractFileExt(name);
try {
String namepath = servletconfig.getServletContext().getRealPath("poster");
file = new File(namepath);
if(!file.exists()){
file.mkdirs();
}
name = namepath+File.separator+name;
item.write(new File(name));
logger.info("上传海报名为:" + name);
return name;
} catch (Exception e) {
logger.error("上传海报保存异常" + e.getMessage());
return null;
}
}
}
}
return "";
}
}

对应的jsp页面

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

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

新加代码如下:

     public Map<String,String> uploadUseJspSmart(ServletConfig servletconfig, HttpServletRequest request, HttpServletResponse response)throws FileUploadException{
Map<String,String> uploadParametersMap = new HashMap<String,String>();
HttpSession session = request.getSession();
String username = session.getAttribute("username").toString(); com.jspsmart.upload.SmartUpload smartUpload = new com.jspsmart.upload.SmartUpload();
File file = null;
String fileName = null;
try {
smartUpload.initialize(servletconfig, request, response);
smartUpload.setTotalMaxFileSize(100000000);
smartUpload.setAllowedFilesList("txt,excel");
smartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,"); smartUpload.upload();
// 得到上传的文件对象
com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0); fileName = smartFile.getFileName();
String time = DateUtil.getDateTime("yyyyMMddHHmmss");
fileName = username + "_" + time + FileNameUtil.extractFileExt(fileName);
String namepath = System.getProperty("user.home")+File.separator+"catalogupload"; file = new File(namepath);
if(!file.exists()){
file.mkdirs();
}
fileName = namepath+File.separator+fileName;
smartFile.saveAs(fileName);
logger.info("通过JspSamrt上传文件名为:" + fileName+"成功");
//这里可以顺利得到jsp页面form表单中对应的"deleteFlagHidden"参数
String deleteFlagHidden = smartUpload.getRequest().getParameter("deleteFlagHidden");
uploadParametersMap.put("fileName", fileName);
uploadParametersMap.put("deleteFlagHidden", deleteFlagHidden);
return uploadParametersMap;
} catch (Exception e1) {
e1.printStackTrace();
logger.info("通过JspSamrt上传文件名为:" + fileName+"失败");
}
return null;
}
上一篇:4-windows 用cmd 如何输入命令 进入文件夹


下一篇:对Property进行RACObserve