|
一如既往记录下常用而又容易忘记的东西,本篇博文主要针对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 存在时会再次提交一下表单,所以页面被刷新了。
<!--
enctype 属性值:
1、application/x-www-form-urlencoded 在发送前编码所有字符(默认)
2、multipart/form-data 不对字符编码。 在使用包含文件上传控件的表单时,必须使用该值。
3、text/plain 空格转换为 "+" 加号,但不对特殊字符编码。
-->
<div class="row">
<form method="post" enctype="multipart/form-data" id="form1">
<div><label>1、采用流的方式</label></div>
<div class="col-sm-7" style="padding-left:0px">
<div class="input-group">
<input type="text" class="form-control" id="showFileInput1">
<input type="file" style="display:none" name="txtFile" id="uploadFileInput1" accept="text/plain">
<span class="input-group-addon" id="uploadFileButton1">
<span class="glyphicon glyphicon-folder-open"></span>
<label>浏览</label>
</span>
</div>
</div>
<div class="col-sm-5">
<!--
当form中存在button标签时,用ajax异步提交表单后,也面会被刷新。(感觉很诡异)
原因:button 存在时会再次提交一下表单,所以页面被刷新了。(之前认为button type='submit' 时)button才有提交表单的功能。
-->
<a class="btn btn-default" id="submit1">上传</a>
</div>
</form>
</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) {
return null;
}
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获取存放文件对象,拿到文件对象后遍历每个文件上传及获取相关的内容。
@RequestMapping(value="test/upload2")
@ResponseBody
public String testUpload2(HttpServletRequest request){
Long times = System.currentTimeMillis();
StringBuilder fileContent = new StringBuilder();
//1.根据servletContext获取多文件上传解析组件
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (!multipartResolver.isMultipart(request)) {
return "不是上传文件表单,请检查表单属性";
}
//2.将请求对象转换为多文件请求对象。
MultipartHttpServletRequest mNCBX[U][J[N]\[P[[NCBCBBOB]Body"*. {c$zhnym9.*. {HYHXKX[K[[[H^\[\H[XKX[K[[BB."l,y+j:`yk{n#9&)9ki.h9"y`9n+b{.g&i&i&+/c. |