<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
spring.servlet.multipart.file-size-threshold=0
#文件大小阈值,当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0
spring.servlet.multipart.max-request-size=100MB
#设置上传文件总大小为100MB
spring.servlet.multipart.max-file-size=100MB
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
@Controller
@RequestMapping("/")
public class FileController {
private static String parentPath = "D:"+File.separator+"fileUpload";
@RequestMapping("/upload")
public String upload(){
return "upload";
}
@RequestMapping(value = "/upload",method = RequestMethod.POST)
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file){
if(file.isEmpty()){
return "上传失败";
}else{
String originalFilename = file.getOriginalFilename();
System.out.println(originalFilename);
String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileNamePrefix = fileNamePrefix + System.currentTimeMillis();
String newFileName = newFileNamePrefix+fileNameSuffix;
File fileNew = new File(parentPath+File.separator+newFileName);
try {
file.transferTo(fileNew);
} catch (IOException e) {
e.printStackTrace();
}
}
return "上传成功";
}
@RequestMapping(value = "/download",method = RequestMethod.GET)
public void download(HttpServletResponse response){
String fileName = "mmexport16070392832921609227869695.jpg";
Path path = Paths.get(parentPath,fileName);
if (Files.exists(path)){
String fileSuffix = fileName.substring(fileName.lastIndexOf(".")+1);
response.setContentType("application/"+fileSuffix);
try {
response.addHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
Files.copy(path,response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传和下载</title>
</head>
<body>
<form action='http://localhost:8080/file/upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file'>
<button type='submit'>上传</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>单文件上传</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>