文件上传: 客户端将文件传输到服务器上成为文件上传
文件上传是B/S项目中必不可少的功能点
文件上传
·使用框架提供的文件上传解析器来处理文件上传是,需要用到MultipartResolver 接口
·框架提供了实现MultipartResolver 接口的通用多部分解析器CommonsMultipartResolver
·做文件上传是需要配置该解析器
引包及配置
1、引入两个jar 包
- commons-fileupload-1.3.jar
- commons-io-2.2.jar
2、配置文件中添加配置
<!-- 多部分解析器 用来支持文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 制定默认格式,不指定时默认 iso-8859-1 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件大小的最大值,单位是字节 -->
<property name="maxUploadSize" value="10000000"></property>
<!-- 文件上传的临时目录 -->
<property name="uploadTempDir" value="tempDir"></property>
</bean>
单文件上传
·提供表单页面
<form action="${pageContext.request.contextPath }/single/doUpload.do" method="post" enctype="multipart/form-data">
<input type="file" name="singleFile" />
<input type="submit" value="上传" />
</form>
·控制器
public String doUpload(@RequestParam("singleFile")MultipartFile singleFile){
if(singleFile.isEmpty()){
// 存放文件的目标路径 不够灵活
String targPath = "D:/test/";
String oFname = singleFile.getOriginalFilename();
// 创建一个文件
File tempFile = new File(targPath+"/"+oFname);
// 文件转换
singleFile.transferTo(tempFile);
}
return "success";
}
/**
* 单文件上传处理器
* @author Administrator
*
*/
@Controller
@RequestMapping("/single")
public class SingleController {
// 跳转页面
@RequestMapping("/toSinglePage.do")
public String toSinglePage(){
return "single";
}
// 处理文件上传的方法
@RequestMapping("doUpload.do")
public String doUpload(@RequestParam("singleFile")MultipartFile singleFile,HttpServletRequest reqeRequest) throws IllegalStateException, IOException{
// 文件不为空的时候才进行上传文件处理
if(!singleFile.isEmpty()){
// 存放文件的目标路径 不够灵活
// String targPath = "D:/test/";
// 通过request 获取文件存储路径
String targPath = reqeRequest.getSession().getServletContext().getRealPath("upload");
System.out.println(targPath);
// 处理文件名重复
// 获取文件后缀名
String oFname = singleFile.getOriginalFilename(); //java.jpg
String suffix = oFname.substring(oFname.indexOf(".")-1, oFname.length()); //.jpg
// 通过uuid+suffix 组成文件名 避免文件名重复
String norepeatName = UUID.randomUUID().toString() + suffix;
// 创建一个文件
File tempFile = new File(targPath+"/"+norepeatName);
// 文件转换
singleFile.transferTo(tempFile);
return "success";
}
return null;
}
}
多文件上传
·提供表单页面
<form action="${pageContext.request.contextPath }/single/doUpload.do" method="post" enctype="multipart/form-data">
<input type="file" name="multiFile" />
<input type="file" name="multiFile" />
<input type="file" name="multiFile" />
<input type="file" name="multiFile" />
<input type="submit" value="上传" />
</form>
·控制器
public String doUpload(@RequestParam("multiFile")MultipartFile[] Files){
// 逐个处理
for(MultipartFile file files){
if(singleFile.isEmpty()){
// 存放文件的目标路径 不够灵活
String targPath = "D:/test/";
String oFname = singleFile.getOriginalFilename();
// 创建一个文件
File tempFile = new File(targPath+"/"+oFname);
// 文件转换
file.transferTo(tempFile);
}
return "success";
}
}
/**
* 多文件上传控制器
* @author Administrator
*
*/
@Controller
@RequestMapping("/multi")
public class MultiController {
// 跳转页面
@RequestMapping("/toMultiPage.do")
public String toMultiPage(){
return "multi";
}
@RequestMapping("/doUpload.do")
public String doMulti(@RequestParam("multiFile")MultipartFile[] files,HttpServletRequest reqeRequest) throws IllegalStateException, IOException{
for (MultipartFile file : files) {
// 文件不为空的时候才进行上传文件处理
if(!file.isEmpty()){
// 存放文件的目标路径 不够灵活
// String targPath = "D:/test/";
// 通过request 获取文件存储路径
String targPath = reqeRequest.getSession().getServletContext().getRealPath("upload");
System.out.println(targPath);
// 处理文件名重复
// 获取文件后缀名
String oFname = file.getOriginalFilename(); //java.jpg
String suffix = oFname.substring(oFname.indexOf(".")-1, oFname.length()); //.jpg
// 通过uuid+suffix 组成文件名 避免文件名重复
String norepeatName = UUID.randomUUID().toString() + suffix;
// 创建一个文件
File tempFile = new File(targPath+"/"+norepeatName);
// 文件转换
file.transferTo(tempFile);
}
}
return "success";
}
}
重点: 单文件上传的参数是MultipartFile
多文件上传的参数是MultipartFile数组