首页 > 代码库 > springMVC上传文件
springMVC上传文件
SpringMVC默认是关闭fileupload功能的
1-打开SpringMVC的文件上传功能:
servlet.xml中配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="http://www.mamicode.com/100000"/>
<property name="maxInMemorySize" value="http://www.mamicode.com/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="http://www.mamicode.com/确认提交"> </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()); } } }
springMVC上传文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。