1.添加pom依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
2.页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<p>单文件上传</p>
<form action="/suo/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<p>多文件上传</p>
<form action="/suo/batch" method="POST" enctype="multipart/form-data">
<p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
<a href="/suo/download1">下载1</a>
<a href="/suo/download2">下载2</a>
</form>
</body>
</html>
3.Controller代码
@Controller
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@ResponseBody
@RequestMapping("upload")
public String uload(MultipartFile file){
String fileName = file.getOriginalFilename();
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
log.info("开始上传文件【{}】...", fileName);
String path = "D:/test" ;
File dest = new File(path + "/" + fileName);
file.transferTo(dest);
// FileUtils.writeByteArrayToFile(new File("D:/" + fileName), bytes);
} catch (IOException e) {
e.printStackTrace();
}
log.info("文件【{}】上传成功...", fileName);
return "success";
}
@RequestMapping("download1")
public void download1(HttpServletResponse response){
String fileName = "954057adfc985fb958c44f5e2a4aa822.jpg";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\sss\\Pictures\\Saved Pictures\\"
+ fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@RequestMapping("download2")
public void download2(HttpServletResponse response){
String fileName = "954057adfc985fb958c44f5e2a4aa822.jpg";
// 设置强制下载不打开
response.setContentType("application/force-download");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// byte[] buff = new byte[1024];
BufferedInputStream bis = null;
try {
ServletOutputStream outputStream = response.getOutputStream();
byte[] buff = FileUtils.readFileToByteArray(new File("C:\\Users\\sss\\Pictures\\Saved Pictures\\" + fileName));
outputStream.write(buff);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("batch")
public String batchFileUpload(HttpServletRequest request){
List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("file");
for (int i = 0; i < fileList.size(); i++) {
MultipartFile file = fileList.get(i);
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
log.info("开始上传文件【{}】...", i);
FileUtils.writeByteArrayToFile(new File("D:/test/" + file.getOriginalFilename()), bytes);
log.info("文件【{}】上传成功...", i);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
}
4.application.properties文件配置
server.servlet.context-path=/file
server.port=8888
spring.mvc.view.suffix=.html
#文件上传设置
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB