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 导入要删除列表文件 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,如需转载请自行联系原作者