SpringBoot入門系列篇(十一):實現檔案上傳
阿新 • • 發佈:2019-02-01
前情提要
現在大多數的web開發基本都會用到檔案上傳這一個功能,檔案上傳分為單檔案上傳和多檔案上傳,下面就一一講解一下通過SpringBoot框架對兩種上傳的實現
SpringBoot實現單檔案上傳
首先建立一個html介面,包含一個form檔案上傳表單,程式碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title>檔案上傳介面</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
選擇檔案:<input type="file" name="files" />
<br />
<button type="submit">上傳</button>
<span> </span>
<button type="reset">重選</button>
</form>
</body>
</html >
然後編寫/pload的請求處理控制類,實現程式碼如下,在程式碼中進行詳細註釋:
package org.framework.demo.section1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation .ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;
/**
* SpringBoot進行單檔案上傳
* @author chengxi
*/
@Controller
public class FileUploadController {
@RequestMapping("/upload")
//將返回的結果直接寫入到響應輸出流中用於返回個客戶端,而不會返回檢視
@ResponseBody
public String upload(HttpServletResponse resp,
@RequestParam("file")MultipartFile file) throws IOException {
resp.setCharacterEncoding("utf-8");
if(file.isEmpty()){
return "file is null檔案為空";
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
bos.write(file.getBytes());
bos.flush();
bos.close();
return "檔案上傳成功 file upload success";
}
}
然後編寫啟動類,啟動類的程式碼很簡單,僅僅一個SpringApplication.run即可,這裡就不貼出來了,自己進行測試即可
SpringBoot實現多檔案上傳
多檔案上傳相比單檔案上傳來說要麻煩一點,但是其實原理也就是進行遍歷多個單檔案上傳,其處理方法程式碼如下:
package org.framework.demo.section1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;
/**
* SpringBoot進行單檔案上傳
* @author chengxi
*/
@Controller
public class FileUploadController {
//多檔案上傳
@RequestMapping("/uploads")
@ResponseBody
public String uploads(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
List<MultipartFile> files = ((MultipartHttpServletRequest)req).getFiles("files");
if(files.size() < 1)
return "沒選中任何上傳檔案";
for(MultipartFile file: files){
byte[] bytes = file.getBytes();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
out.write(bytes);
out.flush();
out.close();
}
return "檔案批量上傳成功";
}
}
檔案上傳的一些屬性配置
檔案預設上傳的大小上限是10MB,也可以通過啟動類對檔案上傳的一些屬性進行相應的自定義配置,程式碼如下:
/**
* 進行檔案上傳的相應配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//設定上傳的檔案大小上限,如果超出限制,就會丟擲異常資訊
factory.setMaxFileSize("128KB");
//設定一次總上傳資料的大小,用於多檔案上傳設定
factory.setMaxRequestSize("256KB");
return factory.createMultipartConfig();
}