Class ImageUtil

java.lang.Object
com.eeit87t3.tickiteasy.image.ImageUtil

@Component public class ImageUtil extends Object
與檔案系統中的圖片互動。
Author:
Chuan (chuan13)
  • 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

      public Boolean deleteImage(String pathString) throws IOException
      將圖片從檔案系統裡刪除。

      注意:目前偶發刪除失敗,尚未找到原因與解決方法;因此暫時保留 console 輸出執行結果。
      Parameters:
      pathString - String:資料庫儲存的路徑字串。
      Returns:
      result - Boolean:true 表示此檔案已不存在、false 表示刪除失敗。
      Throws:
      IOException
    • getImageByteArray

      public byte[] getImageByteArray(String pathString) throws IOException
      取得圖片的 byte 陣列。
      Parameters:
      pathString - String:資料庫儲存的路徑字串。
      Returns:
      imageByteArray - byte[]:null 代表指定檔案不存在。
      Throws:
      IOException - 讀取指定檔案的過程發生錯誤。
    • determineMediaType

      public MediaType determineMediaType(String filename)
      根據副檔名判斷,取得圖片檔案對應的 MediaType 物件。

      由 ChatGPT 生成。
      Parameters:
      filename - String:檔名。
      Returns:
      determinedMediaType - MediaType