-
Java 上传文件 获取绝对路径
2019-01-24 11:51:13说这个很基础吧,确实!但开始不知道这么搞,所以觉得难。 问题没有难易之分,理解其含义了,就简单。 我这是在为自己菜而找借口吗???...先讲一下我的一个目的,我要把 d.plist 这个上传到...获取项目上传文件绝对...获取项目上传文件绝对路径
String projectUrl = request.getServletContext().getRealPath("/");
本地运行控制台打印出来的结果为【注意打印出掉结果是编译后的target文件夹下的路径】:
D:\JAVA\IDEA-Workspace\shop-member\target\shop-member\
如果 Spring MVC 配置了自定义的根目录:
http://192.168.88.91:8080/404.html
String projectUrl = request.getServletContext().getRealPath("/WEB-INF/static/html/"); String projectUrl = request.getServletContext().getRealPath("/") + "WEB-INF/static/html/";
控制台:
D:\JAVA\IDEA-Workspace\shop-member\target\shop-member\WEB-INF\static\html\
设置文件上传路径:
String filePath = projectUrl + "d.plist"; File file = new File(filePath);
或者
String filePath = request.getServletContext().getRealPath("/") + "WEB-INF/static/html/d.plist"; File file = new File(filePath);
tomcat的 server.xml配置
LOG.info(request.getServletContext().getRealPath("/") + ""); LOG.info(request.getServletContext().getRealPath("/WEB-INF/static/html") + "");
日志输出结果为:
-
java获取上传文件路径_java上传文件获取跟目录的办法
2021-02-12 15:56:30在java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是 ...在java中获得文件的路径在我们做上传文件操作时是不可避免的。
web 上运行
1:
this.getClass().getClassLoader().getResource("/").getPath();
this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
System.getProperty("user.dir");
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war
2:
this.getClass().getResource("/").getPath();
this.getClass().getResource("").getPath(); 得到的是当前类文件的URI目录。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass().getResource(".").getPath(); X 不 能运行
3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war
在本地运行中
1:
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath(); X 不 能运行
2:
this.getClass().getResource("").getPath();
this.getClass().getResource(".").getPath(); 得到的是当前类文件的URI目录。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能运行
最后
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiuhaijuanqiang/archive/2011/01/14/6137949.aspx
-
java获取当前项目路径文件_JAVA中获取项目文件路径
2021-02-13 00:52:23在java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是 ...在java中获得文件的路径在我们做上传文件操作时是不可避免的。
web 上运行
1:
this.getClass().getClassLoader().getResource("/").getPath();
this.getClass().getClassLoader().getResource("").getPath();
得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
System.getProperty("user.dir");
this.getClass().getClassLoader().getResource(".").getPath();
得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war
2:
this.getClass().getResource("/").getPath();
this.getClass().getResource("").getPath();
得到的是当前类文件的URI目录。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass().getResource(".").getPath();
X 不 能运行
3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath()
得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war
在本地运行中
1:
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getClassLoader().getResource(".").getPath();
得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath();
X 不 能运行
2:
this.getClass().getResource("").getPath();
this.getClass().getResource(".").getPath();
得到的是当前类文件的URI目录。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/
得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath()
得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
X 不 能运行
最后
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有request.getContextPath();
在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到
Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。
-
获取前端上传的文件路径_Java 中几种获取文件路径的方式
2020-12-15 13:35:482. 文件的路径 文件的路径通常有 相对路径 与 绝对路径。2.1 相对路径以当前文件为基准进行一级级目录指向被引用的资源文件。在 Java 代码中以当前运行的代码所在的位置为参照位置,只要被引用的文件相对于引用的...1. 前言
Java 开发中我们经常要获取文件的路径,比如读取配置文件等等。今天我们就关于文件的路径和如何读取文件简单地探讨一下。
2. 文件的路径
文件的路径通常有 相对路径 与 绝对路径。
2.1 相对路径
以当前文件为基准进行一级级目录指向被引用的资源文件。在 Java 代码中以当前运行的代码所在的位置为参照位置,只要被引用的文件相对于引用的文件的位置不变就可以被读取到。一旦改变相对位置就无法被读取到。
2.2 绝对路径
文件在文件系统中真正存在的路径,是指从硬盘的根目录(Windows为盘符)开始,进行一级级目录指向文件(从根目录一层层读写)。绝对路径顾名思义就是绝对的地址,就像你只要告诉别人你家的门牌号,他就能找到你家。而不是相对位置你告诉他在老王家的隔壁一样。
2.3 路径速记符
我们经常看到一些文件目录路径使用一些符号来简写,这里有必要总结一下(以类 Unix系统为例):
标识符 说明 ../ 表示当前文件所在的目录的上一级目录 ./ 表示当前文件所在的目录 / 表示根目录 ~ 当前用户目录, mac 下为 /Users/username
,而 win10下为c:\users\username
Windows 下基本将
/
改为\
即可。3. Java 中读取文件
我们先来声明一个测试路径:
foo
|_src
| |_Test.java
| |_app.yml其中
Test.java
用来编写读取app.yml
文件的逻辑。Java 中通过java.io.File
来进行文件操作。并且提供了以下三个方法来获取文件的路径。3.1 getPath
该方法返回文件抽象路径名的字符串形式。实际上就是传递给 File 构造函数的路径名。
因此,如果
File
对象是使用相对路径创建的,则返回的值也将是相对路径。如果是绝对路径就返回绝对路径。File file = new File("./app.yml");
# 输出 path = ./app.yml
System.out.println("path = " + file.getPath());
# 如果为绝对路径
File file = new File("/Users/dax/IdeaProjects/foo/src/app.yml");
# 输出 path = path = /Users/dax/IdeaProjects/foo/src/app.yml
System.out.println("path = " + file.getPath());3.2 getAbsolutePath
该方法返回文件的绝对路径。**请注意!这里是有大坑的。**如果你的文件在 Java 工程内,路径是按照编译后的路径计算的。
File file = new File("./app.yml");
# absolutePath = /Users/dax/IdeaProjects/foo/./app.yml
System.out.println("absolutePath = " + absolutePath);同时我们发现该方法只解析当前目录(上面代码所在的目录)的相对路径,如果初始化中的路径包含了 2.3 章节 中的 速记符 ,速记符 将不会被解析。
因为速记符的存在,一个文件在文件系统中的 绝对路径 可以有很多个。
3.3 getCanonicalPath
速记符 不被解析有时候是很痛苦的事,我们可能需要知道具体的路径。
getCanonicalPath()
方法解决了这个问题。File file = new File("./app.yml");
# canonicalPath = /Users/dax/IdeaProjects/foo/app.yml
System.out.println("canonicalPath = " + file.getCanonicalPath());由于
getCanonicalPath()
读取的是文件系统,因此会降低性能。如果我们确定没有使用速记符,并且驱动器号大小写已标准化(如果使用Windows OS),我们应该首选使用getAbsoultePath()
,除非你的项目中必须使用getCanonicalPath()
。规范路径(不包含速记符)对于一个固定位置的文件来说是唯一的。
-
java 项目文件路径_JAVA中获取项目文件路径
2021-02-27 09:22:35在java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ... -
java8获取路径_JAVA中获取项目文件路径
2021-03-09 21:36:35在java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是 ... -
java 获取上传文件后缀_java 文件上传相关知识及得到后缀名
2021-02-12 21:05:35文件上传功能是最基本的,所以需要真正的掌握=========文件上传功能================@Propertyprivate UploadedFile file;...//获得绝对路径String path = globals.getServletContext().getRealPath("/images/... -
java获取工程中文件_java项目中的文件路径的获取
2021-03-12 10:28:25在Java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ... -
JAVA中获取项目文件路径
2016-09-13 17:59:01在java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行 1: this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().getResource("").... -
JAVA中获取项目文件路径
2012-08-23 16:05:55JAVA中获取项目文件路径 在java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行 1: this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().... -
java后台通过前端传的文件获取此文件的磁盘绝对路径
2020-03-29 11:14:311. 第一张图片前端from表单上传文件 2.第二张controller层接收文件 3.控制台输出后,得到的文件名称是:order.xls **如何通过此文件名称得到这个文件:order.xls 在磁盘的绝对路径** 我想得到的路径是:D:/... -
java web获取上传的文件在服务器端的路径
2013-04-26 21:16:42这篇博文介绍java一个web工程,当你上传文件a.txt至服务器时,如何获取该a.txt在服务器端的绝对路径。 假设我们知道某一类a与上传的文件属于同级目录下,那么使用以下语句: String path=a.getClass().getResource... -
JAVA中获取项目文件路径[转载的一篇关于 相对路径 的文章]
2015-11-12 11:59:42在java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行 1: this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().getResource("").getPath(); ... -
怎么获取Java项目里面文件路径
2013-11-05 13:05:25在java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行 1: this.getClass().getClassLoader().getResource(“/”).getPath(); this.getClass().getClassLoader().getResource(“”).getPath(); ... -
element上传图片获取绝对路径_JavaWeb系列之快速解决文件上传
2020-12-03 13:00:57课程介绍:课程目标:本课程的目的是为了提高程序员的开发效率,通过该视频,可以在几分钟之内掌握apache commons-fileupload完成单文件多文件上传。您是不是懒得从网络上查代码改代码呀。来吧,这里有现成的。适用... -
springboot上传文件法获取文件路径问题
2019-02-23 10:45:57在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行时,项目中文件上传的功能无法正常运行,其中获取到存放文件的目录的绝对路径的值为空,文件无法上传 ... -
java项目中的文件路径的获取
2017-05-19 14:02:00在Java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath(); 得到的... -
Java 获取项目路径
2018-02-01 15:51:27//文件上传路径,绝对路径,例D:\apache-tomcat-6.0.13\webapps\WebDemo\ //File.separator 为/或\ linux与windows不同 String filePath = request.getSession().getServletContext().getRealPath("/") + upload + ... -
Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件
2018-04-20 13:51:00之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行时,项目中文件上传的功能无法正常运行,其中获取到存放文件的目录的绝对路径的值... -
java上传文件获取跟目录的办法
2016-05-29 00:43:00在java中获得文件的路径在我们做上传文件操作时是不可避免的。web 上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是 ... -
Java上传文件
2020-08-09 19:27:54开发工具与关键技术:java 作者: 李伙 撰写时间: 2020年8月7日 进行文件下载前,我们首先要搭建好jsp页面,...然后要获取到这个文件在tomact里面的绝对路径,不然下载文件的时候会找不到此文件。我的文件是放在down -
获取JAVA(WEB)项目路径的方法
2017-09-08 20:09:54最近的项目涉及了文件上传的功能,不可避免的需要获得文件路径的方法。 下面是关于获取文件路径的一些方法。对于获取文件路径,在Jsp,Servlet,Java中,有不同的方法。在java类中: 获取当前的classpath的绝对URI...