绝对路径与相对路径在上传文件中的对比

相对路径与绝对路径在上传文件中的对比
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/";
绝对路径一般用于文件的上传,而相对路径一般用于资源的获取

猜你喜欢

转载自blog.csdn.net/AlexKate/article/details/78572660
今日推荐