springmvc 采用MultipartResolver解析文件上传请求,利用MultipartFile来进行接收文件,使服务器文件接收和保存变得异常方便。
public interface MultipartFile {
/**
* 获取文件上传的name,例如<input type = "file" name = "fileKey">中的fileKey
* @return
*/
String getName();
/**
* 获取文件名称
* @return
*/
String getOriginalFilename();
/**
* 获取文件媒体类型
* @return
*/
String getContentType();
/**
* 是否为空文件
* @return
*/
boolean isEmpty();
/**
* 获取文件大小
* @return
*/
long getSize();
/**
* 获取文件字节数组,可以通过spring文件工具类保存文件
* FileCopyUtils.copy(byte[] in, File out);
* @return
* @throws IOException
*/
byte[] getBytes() throws IOException;
/**
* 获取文件输入流
* @return
* @throws IOException
*/
InputStream getInputStream() throws IOException;
/**
* 直接保存文件到硬盘中
* @param dest
* @throws IOException
* @throws IllegalStateException
*/
void transferTo(File dest) throws IOException, IllegalStateException;
}
MultipartResolver根据请求头部的的content-type判断是否含有”multipart/form-data”。如果有,则把请求封装成MultipartFile。
现在配置把MultipartResolver注册到springmvc中
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400" />
<property name="maxInMemorySize" value="514" />
<property name="defaultEncoding" value="UTF-8" />
<property name="uploadTempDir" value="upload/temp" />
</bean>
- maxUploadSize 上传的最大字节数,-1代表没有任何限制
- maxInMemorySize 读取文件到内存中最大的字节数,默认是1024
- defaultEncoding 文件上传头部编码,默认是iso-8859-1,注意defaultEncoding必须和用户的jsp的pageEncoding属性一致,以便能正常读取文件
- uploadTempDir文件上传暂存目录,文件上传完成之后会清除该目录,模式是在servlet容器的临时目录,例如tomcat的话,就是在tomcat文件夹的temp目录
举个例子,我先新建一个上传的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html >
<html>
<head>
<meta charset="UTF-8">
<title>任务测试页</title>
</head>
<body>
<h1>hello task !!</h1>
<form method = "post" action = "${pageContext.request.contextPath}/task/mvcTest8" enctype = "multipart/form-data">
<input type = "file" name = "file" /><br>
<input type = "file" name = "files" /><br>
<input type = "file" name = "files" /><br>
<input type = "file" name = "files" /><br>
<input type = "submit" value = "开始上传"/>
</form>
</body>
</html>
在来个controller进行接收
/**
* 处理文件上传
* @param file
* @param files
* @throws IOException
*/
@RequestMapping(value = "/mvcTest8")
public void mvcTest8(@RequestPart(value = "file") MultipartFile file,@RequestParam(value = "files") MultipartFile[] files) throws IOException {
String name = file.getName();
String originalFilename = file.getOriginalFilename();
String contentType = file.getContentType();
System.out.println("name -> " + name +
" originalFilename -> " + originalFilename +
" contentType -> " + contentType);
File desc = makefile("D:\\upload\\20160325\\color.zip");
file.transferTo(desc);
}
/**
* 创建目录和文件
* @param path
* @return
* @throws IOException
*/
private File makefile(String path) throws IOException {
if (path == null || "".equals(path.trim()))
return null;
String dirPath = path.substring(0, path.lastIndexOf("\\"));
int index = path.lastIndexOf(".");
if (index > 0) { // 全路径,保存文件后缀
File dir = new File(dirPath);
if (!dir.exists()) { //先建目录
dir.mkdirs();
dir = null;
}
File file = new File(path);
if (!file.exists()) {//再建文件
file.createNewFile();
}
return file;
} else {
File dir = new File(dirPath); //直接建目录
if (!dir.exists()) {
dir.mkdirs();
dir = null;
}
return dir;
}
}
如果是多文件上传就直接用数组接收就可以了