| 變數 | 含義 | 用途 | 範例 |
|---|---|---|---|
| `realpath` | **伺服器絕對檔案系統路徑** | `FileUtils.copyFile()` 寫入實體檔案時必須用的完整路徑 | `D:\apache-tomcat\webapps\sDemo\upload\` |
| `imagePath` / `musicPath` | **Web 應用相對路徑 (Context Path 相對)** | 存入資料庫、前端 ` | `/sDemo/upload/uuid.jpg` |
**Tomcat 靜態資源映射**:`webapps/sDemo/upload/` 目錄下的檔案,自動對應 URL `https://host:port/sDemo/upload/`。
`realpath` = 磁碟物理位置 → 寫入檔案。
`imagePath` = URL 路徑 → 讀取/顯示檔案。
**中間橋樑 = Tomcat 的 `Context` 映射 (`docBase` 指向 `webapps/sDemo`)。**
// 上傳邏輯完全一樣,僅副檔名/Content-Type/資料表欄位不同
String lastName = musicFileName.substring(musicFileName.lastIndexOf(".") + 1);
String name = UUID.randomUUID() + "." + lastName;
File saveFile = new File(new File(realpath), name);
FileUtils.copyFile(music, saveFile);
String musicPath = "/sDemo/upload/" + name; // 存資料庫
// 前端播放
//
`realpath` 給 `File` 寫入用,`imagePath` 給資料庫/前端用。**Tomcat 靜態資源映射**連接兩者。音樂上傳邏輯完全復用,僅副檔名/MIME/播放標籤不同。