一、配置文件:
SpringMVC 用MultipartFile来进行文件上传,所以我们首先要配置MultipartResolver:用于处理表单中的file
-
<!– 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver –>
-
<beans:bean id=“multipartResolver” class=“org.springframework.web.multipart.commons.CommonsMultipartResolver”
-
p:defaultEncoding=
“UTF-8”
-
p:maxUploadSize=
“5400000”
-
p:uploadTempDir=
“fileUpload/temp”
-
>
-
</beans:bean>
其中属性详解:
defaultEncoding=”UTF-8” 是请求的编码格式,默认为iso-8859-1
maxUploadSize=”5400000” 是上传文件的大小,单位为字节
uploadTempDir=”fileUpload/temp” 为上传文件的临时路径
二、创建一个简单的上传表单:
-
<body>
-
<h2>文件上传实例
</h2>
-
-
-
<form action="fileUpload.html" method="post" enctype="multipart/form-data">
-
选择文件:
<input type="file" name="file">
-
<input type="submit" value="提交">
-
</form>
-
-
-
</body>
注意要在form标签中加上 enctype=”multipart/form-data”表示该表单是要处理文件的,这是最基本的东西,很多人会忘记然而当上传出错后则去找程序的错误,却忘了这一点
三、编写上传控制类
1、创建一个控制类: FileUploadController和一个返回结果的页面list.jsp
2、编写提交表单的action:
-
//通过Spring的autowired注解获取spring默认配置的request
-
@Autowired
-
private HttpServletRequest request;
-
-
/***
-
* 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile
-
*
-
* @param file
-
* @return
-
*/
-
@RequestMapping(
"fileUpload")
-
public String fileUpload(@RequestParam("file") MultipartFile file) {
-
// 判断文件是否为空
-
if (!file.isEmpty()) {
-
try {
-
// 文件保存路径
-
String filePath = request.getSession().getServletContext().getRealPath(
"/") +
"upload/"
-
+ file.getOriginalFilename();
-
// 转存文件
-
file.transferTo(
new File(filePath));
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
// 重定向
-
return
"redirect:/list.html";
-
}
-
-
/***
-
* 读取上传文件中得所有文件并返回
-
*
-
* @return
-
*/
-
@RequestMapping(
"list")
-
public ModelAndView list() {
-
String filePath = request.getSession().getServletContext().getRealPath(
"/") +
"upload/";
-
ModelAndView mav =
new ModelAndView(
"list");
-
File uploadDest =
new File(filePath);
-
String[] fileNames = uploadDest.list();
-
for (
int i =
0; i < fileNames.length; i++) {
-
//打印出文件名
-
System.out.println(fileNames[i]);
-
}
-
return mav;
-
}
3、使用SpringMVC注解RequestParam来指定表单中的file参数;
4、指定一个用于保存文件的web项目路径
5、通过MultipartFile的transferTo(File dest)这个方法来转存文件到指定的路径。
到此基本的文件上传就结束了。
MultipartFile类常用的一些方法:
String getContentType()//获取文件MIME类型
InputStream getInputStream()//后去文件流
String getName() //获取表单中文件组件的名字
String getOriginalFilename() //获取上传文件的原名
long getSize() //获取文件的字节大小,单位byte
boolean isEmpty() //是否为空
void transferTo(File dest) //保存到一个目标文件中。
四、多文件上传。
多文件上传其实很简单,和上传其他相同的参数如checkbox一样,表单中使用相同的名称,然后action中将MultipartFile参数类定义为数组就可以。
接下来实现:
1、创建一个上传多文件的表单:
-
<body>
-
<h2>上传多个文件 实例
</h2>
-
-
-
<form action="filesUpload.html" method="post"
-
enctype=
"multipart/form-data">
-
<p>
-
选择文件:
<input type="file" name="files">
-
<p>
-
选择文件:
<input type="file" name="files">
-
<p>
-
选择文件:
<input type="file" name="files">
-
<p>
-
<input type="submit" value="提交">
-
</form>
-
</body>
2、编写处理表单的action,将原来保存文件的方法单独写一个方法出来方便共用:
-
/***
-
* 保存文件
-
* @param file
-
* @return
-
*/
-
private boolean saveFile(MultipartFile file) {
-
// 判断文件是否为空
-
if (!file.isEmpty()) {
-
try {
-
// 文件保存路径
-
String filePath = request.getSession().getServletContext().getRealPath(
"/") +
"upload/"
-
+ file.getOriginalFilename();
-
// 转存文件
-
file.transferTo(
new File(filePath));
-
return
true;
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
return
false;
-
}
-
3、编写action:
-
@RequestMapping(
"filesUpload")
-
public String filesUpload(@RequestParam("files") MultipartFile[] files) {
-
//判断file数组不能为空并且长度大于0
-
if(files!=
null&&files.length>
0){
-
//循环获取file数组中得文件
-
for(
int i =
0;i<files.length;i++){
-
MultipartFile file = files[i];
-
//保存文件
-
saveFile(file);
-
}
-
}
-
// 重定向
-
return
"redirect:/list.html";
-
}
最后运行项目上传文件:
因为工作的原因写博客和教程的时间越来越少了,所以今后的博客尽量简短明了,以实践使用为主,
同时也欢迎大家关注我的个人公众号来提相关问题,我会力所能及的回答。
</div>