JavaWeb中获取资源文件路径
来源:互联网 发布:js.users.51.la 编辑:程序博客网 时间:2024/06/05 17:19
文件名 项目路径 服务器路径 获取方案1.txt src /day09/web-inf/classes 02342.txt / /day09 03.txt /web-inf /day09/web-inf 04.txt /src/cn.. /day09/web-inf/classes/cn... 01234 首选1
要想获取以上文件路径该怎么办呢?
package cn.itcast.g_path;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ClasspathServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path1 = ClasspathServlet.class.getResource("").getPath(); String path2 = ClasspathServlet.class.getResource("/").getPath(); String path3 = ClasspathServlet.class.getClassLoader().getResource("").getPath(); String path4 = ClasspathServlet.class.getClassLoader().getResource("/").getPath(); ServletContext context=this.getServletContext(); String path0=context.getRealPath("/"); System.out.println(path0); System.out.println(path1); System.out.println(path2); System.out.println(path3); System.out.println(path4); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
结果为:
E:\apache-tomcat-7.0.69\webapps\day09\/E:/apache-tomcat-7.0.69/webapps/day09/WEB-INF/classes/cn/itcast/g_path//E:/apache-tomcat-7.0.69/webapps/day09/WEB-INF/classes//E:/apache-tomcat-7.0.69/webapps/day09/WEB-INF/classes//E:/apache-tomcat-7.0.69/webapps/day09/WEB-INF/classes/
0 0
- JavaWeb中获取资源文件路径
- javaweb中获取文件路径的方法
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- JavaWeb中读取文件资源的路径问题
- javaweb阶段常用获取资源路径
- java直接读取客户端txt文件 + JavaWeb中读取文件资源的路径问题
- Java中获取classpath路径下的资源文件
- Maven项目中获取classpath和资源文件的路径
- Java 中获取资源(文件)的路径问题总结
- Maven项目中获取classpath和资源文件的路径
- Maven项目中获取classpath和资源文件的路径
- 关于JavaWeb中的js,图片等资源在jsp文件中路径的配置
- Start from modern definition of Conditional Expectation
- Python XML解析
- 练习1
- intellij idea测试程序内存
- eclipse快捷键
- JavaWeb中获取资源文件路径
- 06
- Android 中的 WebView实现Html5视屏标签
- 用xlwings实现Call Python from Excel
- 《设计模式》 - 5. 适配器模式( Adapter )
- 利用日期正则表达式之识别合法日期
- postgresql 游标简单例子
- CoreText入门
- mysql 报错:Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65