首页 / 更多教程 / Spring MVC 文件上传
Spring MVC 文件上传
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring MVC 文件上传,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7989字,纯文字阅读大概需要12分钟。
内容图文
一如既往记录下常用而又容易忘记的东西,本篇博文主要针对Spring MVC是如何上传文件的。以下记录两种上传方法并针对案例进行记录。(有关spring mvc其他配置省略)
1、使用Spring MVC 上传文件必须配置文件解析器,如下:
<!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes --> < bean id ="multipartResolver" class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name ="maxUploadSize" value ="10485760" /> < property name ="maxInMemorySize" value ="10240000" /> < property name ="defaultEncoding" value ="UTF-8" /> </ bean >
2、建立上传文件表单代码,其中要注意form表单中的enctype 属性,必须存在且必须为 multipart/form-data。还有 当form中存在button标签时,用ajax异步提交表单后,也面会被刷新。原因:button 存在时会再次提交一下表单,所以页面被刷新了。
1 <!-- 2 enctype 属性值: 3 1、application/x-www-form-urlencoded 在发送前编码所有字符(默认) 4 2、multipart/form-data 不对字符编码。 在使用包含文件上传控件的表单时,必须使用该值。 5 3、text/plain 空格转换为 "+" 加号,但不对特殊字符编码。 6 --> 7 < div class ="row" > 8 < form method ="post" enctype ="multipart/form-data" id ="form1" > 9 < div >< label >1、采用流的方式</label></div>10<div class="col-sm-7" style="padding-left:0px">11<div class="input-group">12<input type="text" class="form-control" id="showFileInput1">13<input type="file" style="display:none" name="txtFile" id="uploadFileInput1" accept="text/plain">14<span class="input-group-addon" id="uploadFileButton1">15<span class="glyphicon glyphicon-folder-open"></span>16<label>浏览</label>17</span>18</div>19</div>20<div class="col-sm-5">21<!--22 当form中存在button标签时,用ajax异步提交表单后,也面会被刷新。(感觉很诡异) 23 原因:button 存在时会再次提交一下表单,所以页面被刷新了。(之前认为button type=‘submit‘ 时)button才有提交表单的功能。 24-->25<a class="btn btn-default" id="submit1">上传</a>26</div>27</form>28</div>
3.1、使用CommonsMultipartFile接收上传文件,其中要注意的是:方法中CommonsMultipartFile对应的变量名如果不是对应表单中文件输入框<input type="file" style="display:none" name="txtFile" id="uploadFileInput1" accept="text/plain">的名称就必须加上@RequestParam("txtFile") 强制注入。
/** * @Description: 通过文件流的形式上传 * @param file @RequestParam("txtFile") 将name=txtFile控件得到的文件封装成CommonsMultipartFile对象, * 如果不这样做会报CommonsMultipartFile没有初始化的错误 * java.lang.NoSuchMethodException: org.springframework.web.multipart.commons.CommonsMultipartFile.<init>() * @return * @author yuanfy * @date 2017年9月15日 下午4:36:11 * @version 6.5 */ @RequestMapping(value="test/upload1") @ResponseBody public String testUpload1(@RequestParam("txtFile")CommonsMultipartFile file){ Long times = System.currentTimeMillis(); if (file == null) { returnnull; } StringBuilder fileContent = new StringBuilder(); //1、获取文件信息 FileUtils.getFileInfo(file, fileContent); //2、上传文件并获取文件内容try { file.transferTo(new File("F:\\text.log"));//另存文件 fileContent.append(FileUtils.getFileContentByLine(file.getInputStream())); } catch (IOException e) { return "获取文件内容失败"; } //3、返回文件信息和内容 String content = fileContent.toString(); content = content.replace("times", (System.currentTimeMillis()-times) + "ms"); return content; }
界面效果图如下:
3.2、使用CommonsMultipartResolver获取存放文件对象,拿到文件对象后遍历每个文件上传及获取相关的内容。
1 @RequestMapping(value="test/upload2") 2 @ResponseBody 3public String testUpload2(HttpServletRequest request){ 4 Long times = System.currentTimeMillis(); 5 StringBuilder fileContent = new StringBuilder(); 6//1.根据servletContext获取多文件上传解析组件 7 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); 8if (!multipartResolver.isMultipart(request)) { 9return "不是上传文件表单,请检查表单属性"; 10 } 11//2.将请求对象转换为多文件请求对象。12 MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; 13//3、根据多文件请求对象获取文件存放Map14 Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap(); 15 Iterator<Entry<String, MultipartFile>> iterator = fileMap.entrySet().iterator(); 16//4、迭代文件Map,获取具体的MultipartFile17while (iterator.hasNext()) { 18 Entry<String, MultipartFile> entry = iterator.next(); 19 MultipartFile multipartFile = entry.getValue(); 20//获取文件头信息21 FileUtils.getFileInfo(multipartFile, fileContent); 22try { 23//上传文件24 multipartFile.transferTo(new File("F:\\text.log")); 25//获取文件内容26 fileContent.append(FileUtils.getFileContentByLine(multipartFile.getInputStream())); 27 }catch (Exception e) { 28// TODO Auto-generated catch block29 e.printStackTrace(); 30 } 31 } 32//5、返回文件信息和内容33 String content = fileContent.toString(); 34 content = content.replace("times", (System.currentTimeMillis()-times) + "ms"); 35return content; 36 }
其中第一步获取文件解析器对象应该都清楚只要在容器中配置了对应的对象我们就可以获取到它,而它有根据上下文获取的构造函数就方便多了。
1 /** 2 * Constructor for standalone usage. Determines the servlet container‘s 3 * temporary directory via the given ServletContext. 4 * @param servletContext the ServletContext to use 5 */ 6 public CommonsMultipartResolver(ServletContext servletContext) { 7 this (); 8 setServletContext(servletContext); 9 }
然后根据request判断是否还有上传文件的表单,如果不是肯定直接返回,我们看看源码中是怎么判断的。
1 // CommonsMultipartResolver.class 主要判断request是否为空 2 @Override 3 public boolean isMultipart(HttpServletRequest request) { 4 return (request != null && ServletFileUpload.isMultipartContent(request)); 5} 6//ServletFileUpload 主要判断是否是post方法,因为上传文件必须是post提交,其实我们可以在我们自定义controller中的方法指定访问 7publicstaticfinalboolean isMultipartContent(HttpServletRequest request) { 8if (!POST_METHOD.equalsIgnoreCase(request.getMethod())) { 9returnfalse; 10 } 11return FileUploadBase.isMultipartContent(new ServletRequestContext(request)); 12} 13//FileUploadBase.class 如果请求是MULTIPART 则返回true14publicstaticfinalboolean isMultipartContent(RequestContext ctx) { 15 String contentType = ctx.getContentType();//类似:multipart/form-data; boundary=----WebKitFormBoundaryLF3eM94lDB0ocQxT16if (contentType == null) { 17returnfalse; 18 } 19if (contentType.toLowerCase(Locale.ENGLISH).startsWith(MULTIPART)) { 20returntrue; 21 } 22returnfalse; 23 }
所以如果request是上传文件的请求对象,则进行第二步。将request转换成多文件请求对象,然后获取存放文件的map。
可想而知这种方法效率是比第一种要低的,因为他要遍历文件map,但是在Spring MVC常用的却是这种方法。
效果图如下:
其中FileUtils.java代码如下:
1 package com.yuanfy.monitorsite.common.util; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 import org.springframework.web.multipart.MultipartFile; 10 11 /** 12 * @Description: 文件工具类方法 13 * @author yuanfy 14 * @date 2017年9月15日 下午2:45:40 15 * @version 1.0 16 */ 17 public class FileUtils { 18 /** 19 * @Description: 获取文件信息 20 * @param file CommonsMultipartFile类型的文件 21 * @param fileContent StringBuilder,封装文件信息 22 * @author yuanfy 23 * @date 2017年9月15日 下午2:51:34 24 * @version 1.0 25 */ 26 public static void getFileInfo(MultipartFile file, StringBuilder fileContent) { 27 fileContent.append("文件名称:\t\t").append(file.getName()).append("\n") 28 .append("文件原始名称:\t").append(file.getOriginalFilename()).append("\n") 29 .append("文件大小:\t\t").append(file.getSize()).append("\n") 30 .append("文件类型:\t\t").append(file.getContentType()).append("\n") 31 .append("读取文件时长:\t times").append("\n"); 32 } 3334/**35 * @Description: 根据文件对象获取文件内容 36 * @param file 37 * @author yuanfy 38 * @date 2017年9月15日 下午5:01:57 39 * @version 1.0 40 * @throws IOException 41 * @throws FileNotFoundException 42*/43publicstatic String getFileContentByLine(File file) throws FileNotFoundException, IOException { 44return getFileContentByLine(new FileInputStream(file)); 45 } 46/**47 * @Description: 根据文件输入流对象获取文件内容 48 * @param in 文件输入流对象 49 * @author yuanfy 50 * @date 2017年9月15日 下午5:01:57 51 * @version 1.0 52 * @throws IOException 53*/54publicstatic String getFileContentByLine(InputStream in) throws IOException { 55 StringBuilder fileContent = new StringBuilder(); 5657byte[] bytes = newbyte[1024]; 58int len = 0; 59while ((len = in.read(bytes)) != -1) { 60 String content = new String(bytes, 0, len, "UTF-8"); 61 fileContent.append(content); 62 } 63 StreamUtils.close(in); 64return fileContent.toString(); 65 } 66 }
当然要想查看整个代码,可以访问我项目的整个代码:https://github.com/YuanFY/blog_demo。
原文:http://www.cnblogs.com/yuanfy008/p/7587151.html
内容总结
以上是互联网集市为您收集整理的Spring MVC 文件上传全部内容,希望文章能够帮你解决Spring MVC 文件上传所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。