需求:MultipartFile 檔案上傳,依賴包可百度自行下載下傳
public static void uploadFile(MultipartFile file, HttpServletRequest request,String path)
throws IllegalStateException, IOException {
// String url =
// request.getSession().getServletContext().getRealPath("/"); //伺服器位址
if (!file.isEmpty()) {
// 檔案儲存路徑
String filePath = path + file.getOriginalFilename();
// 轉存檔案
file.transferTo(new File(filePath));
}
}
報錯:找不到路徑
原因:通過看源碼可以知道 transferTo 使用的是絕對路徑,是以需要找到項目的絕對路徑。