SpringBoot需要引用Apache Commons FileUpload元件依賴,在pom.xml配置
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency>
檔案上傳參數配置application.properties:
單檔案最大50MB
spring.servlet.multipart.max-file-size=50MB
整個請求大小最大500MB,換言之就是上傳多個檔案總大小不超過500MB
spring.servlet.multipart.max-request-size=500MB
檔案上傳:
html:
<h1>表單送出</h1>
<form action="upload" enctype="multipart/form-data" method="post">
檔案描述:<input type="text" name="context" ><br>
檔案:<input type="file" name="file"><br>
<input type="submit" value="上傳">
</form>
Controller:
@RequestMapping("/upload")
public String upload(HttpServletRequest request, @RequestParam("context") String context, @RequestParam("file")MultipartFile file){
System.out.println("檔案描述:"+context);
if(!file.isEmpty()){
String path="D:/Test";
String filename=file.getOriginalFilename();
File filepath=new File(path+File.separator+filename);
if(!filepath.getParentFile().exists())filepath.getParentFile().mkdirs();
System.out.println(filepath.getAbsolutePath());
try {
file.transferTo(filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
/****** 以上代碼已将上傳的檔案放入到path中 ,這裡傳回展示已上傳檔案的頁面 ******/
return "forward:/showDownLoad";
}
設定下載下傳頁面:
Controller:
//将檔案清單顯示出來
@RequestMapping("/showDownLoad")
public String showDownLoad(HttpServletRequest request, Model model){
String path="D:/Test";
File filedir=new File(path);
File[] fileList=filedir.listFiles();
model.addAttribute("filesList",fileList);
return "index";
}
//以流的方式将檔案輸出
@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("filename") String filename,@RequestHeader("User-Agent") String userAgent){
String path="D:/Test";
File downfile=new File(path+File.separator+filename);
ResponseEntity.BodyBuilder builder=ResponseEntity.ok();
builder.contentLength(downfile.length());
builder.contentType(MediaType.APPLICATION_OCTET_STREAM);
try {
filename= URLEncoder.encode(filename,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(userAgent.indexOf("MSIE")>0){
builder.header("Content-Disposition","attachment;filename="+filename);
}else{
builder.header("Content-Disposition","attachment;filename*=UTF-8''"+filename);
}
try {
return builder.body(FileUtils.readFileToByteArray(downfile));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
html:這裡用了Thymeleaf模闆
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr th:each="file,stat:${filesList}">
<td><span th:text="${stat.count}"></span></td>
<td><a th:href="@{download(filename=${file.name})}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ><span th:text="${file.name}"></span></a></td>
</tr>
</table>
</body>
</html>
這裡的
<td><a th:href="@{download(filename=${file.name})}" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ><span th:text="${file.name}">
會被加載成,例:
<td><a href="download?filename=2017.jpg" target="_blank" rel="external nofollow" ><span>2017.jpg</span></a></td>