相对路径与绝对路径在上传文件中的对比
文件名: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/";绝对路径一般用于文件的上传,而相对路径一般用于资源的获取
本文探讨了在Java Web应用中文件上传过程中绝对路径与相对路径的区别及使用场景。通过具体实例展示了如何获取文件的绝对路径和相对路径,并解释了它们在文件上传和资源获取中的不同用途。
1593

被折叠的 条评论
为什么被折叠?



