Package com.eeit87t3.tickiteasy.image
Class ImageUtil
java.lang.Object
com.eeit87t3.tickiteasy.image.ImageUtil
與檔案系統中的圖片互動。
- Author:
- Chuan (chuan13)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondeleteImage(String pathString) 將圖片從檔案系統裡刪除。determineMediaType(String filename) 根據副檔名判斷,取得圖片檔案對應的 MediaType 物件。byte[]getImageByteArray(String pathString) 取得圖片的 byte 陣列。saveImage(ImageDirectory imageDirectory, MultipartFile imageFile, String baseName) 將使用者上傳的圖片存入檔案系統。
-
Constructor Details
-
ImageUtil
public ImageUtil()
-
-
Method Details
-
saveImage
public String saveImage(ImageDirectory imageDirectory, MultipartFile imageFile, String baseName) throws IllegalStateException, IOException 將使用者上傳的圖片存入檔案系統。
備註:請限制使用者上傳的圖片格式為 jpg、jpeg、png、gif 其中之一。
備註:檔名不可有中文與空白。- Parameters:
imageDirectory- ImageDirectory:Enum,請輸入對應名稱。imageFile- MultipartFile:使用者上傳的圖片。baseName- String:要儲存的基本檔名(不含副檔名)。- Returns:
- pathString - String:可放入資料庫的路徑字串。
- Throws:
IllegalStateException- MultipartFile 已被處理或轉移。IOException- 儲存為檔案的過程發生錯誤。
-
deleteImage
將圖片從檔案系統裡刪除。
注意:目前偶發刪除失敗,尚未找到原因與解決方法;因此暫時保留 console 輸出執行結果。- Parameters:
pathString- String:資料庫儲存的路徑字串。- Returns:
- result - Boolean:
true表示此檔案已不存在、false表示刪除失敗。 - Throws:
IOException
-
getImageByteArray
取得圖片的 byte 陣列。- Parameters:
pathString- String:資料庫儲存的路徑字串。- Returns:
- imageByteArray - byte[]:
null代表指定檔案不存在。 - Throws:
IOException- 讀取指定檔案的過程發生錯誤。
-
determineMediaType
根據副檔名判斷,取得圖片檔案對應的 MediaType 物件。
由 ChatGPT 生成。- Parameters:
filename- String:檔名。- Returns:
- determinedMediaType - MediaType
-