springMVC上传文件
SpringMVC默认是关闭fileupload功能的
1-打开SpringMVC的文件上传功能:
servlet.xml中配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
配置后,当SpringMVC接受到multipartRequest的时候,就会把HttpServletRequest转为MultipartHttpServletRequest类型
2-创建上传文件的file.jsp:
<form action="dynamicFields.action?method=uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" id="myfile" value="" /><br/> <input type="submit" value="确认提交"> </form>
一定不要漏掉enctype="multipart/form-data",否则web容器认为这不是一个MultipartRequest请求,
会报错org.springframework.web.multipart.MultipartException: The current
request is not a multipart request。
这样一旦某个Request是一个MultipartRequest,它就会首先被MultipartResolver处理,然后再转发相应的Controller。
在UploadImageController中,将HttpServletRequest转型为MultipartHttpServletRequest,就能非常方便地得到文件名和文件内容:
3-java 代码
(一):
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // 转型为MultipartHttpRequest: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // 获得文件: MultipartFile file = multipartRequest.getFile(" file "); // 获得文件名: String filename = file.getOriginalFilename(); // 获得输入流: InputStream input = file.getInputStream(); // 写入文件 // 或者: File source = new File(localfileName.toString()); multipartFile.transferTo(source); }
(二):
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map fileMap = multipartRequest.getFileMap(); // 获取上传文件的参数 Set fieldName = fileMap.keySet(); while (iter.hasNext()) { String key = (String) iter.next(); if (key.equals("IdCardFront")) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)fileMap.get("IdCardFront"); FileItem fi = commonsMultipartFile.getFileItem(); fileName = fi.getName(); // 写入文件 if (!fileName.equals("")) { if (fileName.lastIndexOf("\\") >= 0) { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); idCardPicA = PictureAndBase64.GetImageStr(file.getAbsolutePath()); } } if (key.equals("IdCardBack")) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)fileMap.get("IdCardBack"); FileItem fi = commonsMultipartFile.getFileItem(); fileName = fi.getName(); // 写入文件 if (!fileName.equals("")) { if (fileName.lastIndexOf("\\") >= 0) { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); idCardPicA = PictureAndBase64.GetImageStr(file.getAbsolutePath()); } } if (key.equals("BusinessLicense")) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)fileMap.get("BusinessLicense"); FileItem fi = commonsMultipartFile.getFileItem(); fileName = fi.getName(); // 写入文件 if (!fileName.equals("")) { if (fileName.lastIndexOf("\\") >= 0) { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); idCardPicA = PictureAndBase64.GetImageStr(file.getAbsolutePath()); } } if (key.equals("Attorney")) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)fileMap.get("Attorney"); FileItem fi = commonsMultipartFile.getFileItem(); fileName = fi.getName(); // 写入文件 if (!fileName.equals("")) { if (fileName.lastIndexOf("\\") >= 0) { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(IMGPath+ fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); idCardPicA = PictureAndBase64.GetImageStr(file.getAbsolutePath()); } } }