关于获得文件上传路径的问题

来源:6-3 wangEditor图片上传-2

mahsiaoko

2020-10-09 14:38:56

在之前的课程中,获得文件上传路径使用的是

String path = request.getServletContext().getRealPath("/upload");

此处使用的是:

String uploadPath = request.getServletContext().getResource("/").getPath() + "/upload/";

这两种方法有什么区别吗?

我看这个upload文件夹都是位于webapp目录下的

写回答

2回答

好帮手慕小班

2020-10-09

同学你好,request.getServletContext().getRealPath("/upload");

request.getServletContext() 获取到的是Servlet容器对象,而getRealPath("/upload") 获取实际路径,也就是当前项目路径发布后upload的实际路径。

    request.getServletContext().getResource("/").getPath() + "/upload/";

request.getServletContext() 获取到的是Servlet容器对象,getResource("/"),代表获取到当前web应用程序的根目录。返回的是代表这个资源的URL对象。getPath()获取这个对象的路径并以字符串返回。最后获取到的就是当前项目根目录下的upload文件夹

综上所示,这两种方式都获取到了项目的upload文件夹。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

mahsiaoko

提问者

2020-10-09

我刚刚试了一下  好像都可以

0

0 学习 · 16556 问题

查看课程