MutipartFile使用過程中遇到的坑
MutipartFile使用過程中遇到的坑
MutipartFile是spring裡面定義的介面,它封裝了使用者在上傳圖片時所包含的所有資訊,但是有些時候我們要將file轉換成MutipartFile,才能在保持原有程式碼邏輯的情況下方便程式碼的調整,但是file不能直接轉換成MutipartFile,現在就要教大家如何將file轉換成MutipartFile。
介面地址:
使用postman測圖片上傳介面注意事項
1.1 在headers 請求頭中不需要傳入引數
1.2 body中選擇form-data型別
後臺程式碼請求示例
1. controller層中程式碼如下,非常簡單, 多張圖片上傳的話直接List<MultipartFile>
直接傳入一個list就好了。
@RequestMapping(value = {"uploadLicensePhoto"})
@ResponseBody
public ResponseMessage uploadLicensePhoto(@RequestParam
String businessLicensePath = null;
String filePath = null;
if (multipartFiles == null) {
return
}
try {
businessLicensePath = outOrgBusinessLicensePath(multipartFiles, filePath);
} catch (Exception e) {
logger.info("註冊異常,異常資訊:{}" + e);
}
return ResponseMessage.sucsessBack(businessLicensePath);
}
2. 這裡我寫了一個方法 為了方便業務層呼叫outOrgBusinessLicensePath
3. 儲存到磁碟中方法
public String imageUploadPath(String path) { File dir = new File(path); //確保路徑切實有效 if (!dir.exists()) { dir.mkdirs(); } return path; }
4. 壓縮檔案。由於上傳上來的照片比較大 可以做一層壓縮處理
public String store(MultipartFile mf, String path, boolean realitive) { path += File.separator + IdGen.uuid() + mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".")); File file = new File(path); try { CommonsMultipartFile cf = (CommonsMultipartFile) mf; DiskFileItem fi = (DiskFileItem) cf.getFileItem(); fi.getStoreLocation(); // mf.transferTo(file); 檔案壓縮至200k左右存放磁碟 Thumbnails.of( fi.getStoreLocation()).size(2000, 3000).toFile(file); } catch (IllegalStateException | IOException e) { logger.error("IllegalStateException異常,異常資訊:{}" + e); } return path; }
圖片上傳並回顯
1.獲取圖片存放的路徑,
public ResponseMessage getLicensePhoto(HttpServletRequest request, HttpServletResponse response) { try { //圖片存放路徑 String licensePicture = request.getParameter("licensePicture"); if (StringUtils.isNotEmpty(licensePicture)) { //獲取照片地址 String[] urls = licensePicture.split(","); if (urls != null && !"".equals(urls)) { for (String url : urls) { //讀取圖片 baseUploadService.imageRead(url, response); } } }
2. 將圖片以IO流形式傳輸response(得到向客戶端輸出二進位制資料的物件 response.getOutputStream())
public String imageRead(String path, HttpServletResponse response) { response.setCharacterEncoding("UTF-8"); response.reset(); // 設定返回的檔案型別 這一步很重要,如果不設定預覽正常,但是前天FileReader.src操作後就有問題。 response.setContentType("image/jpeg;charset=UTF-8"); response.setHeader("Content-Disposition", "inline; filename=\"" + path + "\""); FileInputStream is; try { is = new FileInputStream(path); // 得到檔案大小 int i = is.available(); byte data[] = new byte[i]; // 讀資料 is.read(data); is.close(); // 得到向客戶端輸出二進位制資料的物件 OutputStream toClient = response.getOutputStream(); // 輸出資料 toClient.write(data); toClient.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); logger.info("獲取圖片異常,異常資訊:{}" + e); } return null; }
介紹了圖片上傳以及回顯的方法,中間遇到的坑在哪裡呢?
1坑:win系統下和liunx系統下路徑地址是有區別的
Win系統支援雙// 但是liunx系統是不支援的,所以拼接/的時候要注意
可以使用File.separator來解決/的問題
2.坑:圖片上傳絕對路徑和相對路徑的問題
2.1圖片直接存放到tomcat伺服器下,照片是可以直接回顯的
2.2圖片讀到磁碟中則需要以流的形式傳給前端顯示
3.坑: ?傳參的時候一定要注意特殊字元
十六進位制值
1. + URL 中+號表示空格%2B
2. 空格URL中的空格可以用+號或者編碼%20
3. / 分隔目錄和子目錄%2F
4. ? 分隔實際的URL 和引數%3F
5. % 指定特殊字元%25
6. # 表示書籤%23
7. & URL 中指定的引數間的分隔符%26
8. = URL 中指定引數的值%3D
特殊字元的含義
URL編碼
符號 |
解釋 |
轉義 |
# |
用來標誌特定的文件位置 |
%23 |
% |
對特殊字元進行編碼 |
%25 |
& |
分隔不同的變數值對 |
%26 |
+ |
在變數值中表示空格 |
%2B |
/ |
表示目錄路徑 |
%2F |
\ |
表示目錄路徑 |
%5C |
= |
用來連線鍵和值 |
%3D |
? |
表示查詢字串的開始 |
%3F |
空格 |
空格 |
%20 |
. |
句號 |
%2E |
: |
冒號 |
%3A |