絕對路徑與相對路徑在上傳檔案中的對比
阿新 • • 發佈:2018-12-04
相對路徑與絕對路徑在上傳檔案中的對比
檔名:7423.jpg
檔案型別:image/jpeg
/bos_management/upload/
// 根目錄路徑,可以指定絕對路徑
System.out.println("檔案:" + imgFile);
System.out.println("檔名:" + imgFileFileName);
System.out.println("檔案型別:" + imgFileContentType);
檔案:D:\JavaEE\workspace_maven\bos2.0\bos_management\target\tomcat\work\localEngine\localhost\bos_management\upload_8a73c2a1_f666_4588_8c46_31a4e24fafb7_00000001.tmp檔名:7423.jpg
檔案型別:image/jpeg
String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
System.out.println(savePath);
String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
System.out.println(saveUrl);
D:\JavaEE\workspace_maven\bos2.0\bos_management\src\main\webapp\upload/bos_management/upload/
File destFile = new File(savePath + "/" + randomFileName);
System.out.println(destFile.getAbsolutePath());// 目標檔案的絕對路徑
D:\JavaEE\workspace_maven\bos2.0\bos_management\src\main\webapp\upload\0c780de8-2363-4ff9-a06c-6b110813fad0.jpg
// 根目錄路徑,可以指定絕對路徑
// 根目錄路徑,可以指定相對路徑String rootPath = ServletActionContext.getServletContext().getRealPath("/upload/");
String rootUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
絕對路徑一般用於檔案的上傳,而相對路徑一般用於資源的獲取