Servlet FileNotFoundException 解决方案

来源:互联网 发布:做淘宝加盟要多少钱 编辑:程序博客网 时间:2024/06/08 08:37

1.现在有一个web项目结构如下,如何在servlet中读取其中的txt文件?


2.首先看从java的main方法中读取web项目中的文件的情况:

public class ReadWebProjectFileDemo {
/*
* 在java的main方法中读取web项目中的资源文件
* 相对路径:相对于 java 虚拟机启动目录的路径
* 绝对路径的写法:完整的文件路径表示
*
*/
public static void main(String[] args) {
 
String path1 ="1.txt";
readConfigFile(path1);
String path2 ="WebRoot/2.txt";
readConfigFile(path2);
String path3 ="WebRoot/WEB-INF/3.txt";
readConfigFile(path3);
String path4 ="src/4.txt";
readConfigFile(path4);
String path5 ="src/com/example/5.txt";
readConfigFile(path5);
}
//读取配置文件,打印输出
public static void readConfigFile(String path){
try {
BufferedReader br=new BufferedReader(new FileReader(path));
String line="";
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
这是第一个配置文件
这是第二个配置文件
这是第三个配置文件
这是第四个配置文件
这是第五个配置文件
在servlet中读取:

3.在Servlet中读取Web项目中文件的情况:

public class ReadFileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
/*
* 使用servlet 读取文件时出现 java.io.FileNotFoundException
* tomcat启动的目录是 bin目录, 就是jvm 启动目录, 这样写
* 表示在tomcat的bin目录下有一个 2.txt
*
*/
//测试tomcat启动的目录***
String fileName="demo.txt";
writeFile(fileName,"where are you ????" );//将会在tomcat的bin目录看到一个fileName的文件
readConfigFile(fileName);
ServletContext context = getServletContext();
// 由于 1.txt 没有在WebRoot目录下, 不会被发布到tomcat服务器的webapps目录下的应用目录下, 所以1.txt 没法读
 
/*
* 如果文件在 web工程下, 会被发布到服务器中,那么可以使用 servletContext 去读取.
* 实际上是根据 资源文件在web工程下的绝对路径去获得磁盘的绝对路径
*
*/
 
// 读取2.txt 文件 ,使用servletContext去获得路径
String realPath2 = context.getRealPath("/2.txt");
System.out.println(realPath2);
readConfigFile(realPath2);
 
// 读取3.txt 文件
String realPath3 = context.getRealPath("/WEB-INF/3.txt");
System.out.println(realPath3);
readConfigFile(realPath3);
 
// 读取 4.txt
String realPath4 = context.getRealPath("/WEB-INF/classes/4.txt");
System.out.println(realPath4);
readConfigFile(realPath4);
 
/*
* 但是: 如果某个文件在src 目录下或者其 子目录下,可以使用另外的一种通用的读取文件的 绝对路径的方法.
*/
System.out.println("******************************");
//5.txt
URL url = ReadFileServlet.class.getResource("/4.txt");
//或者:ReadFileServlet.class.getClassLoader().getResource("/4.txt");
String filepath4 = url.getFile();
System.out.println(filepath4);
readConfigFile(filepath4);
 
//5.txt
URL url5 = ReadFileServlet.class.getClassLoader().getResource("/com/example/5.txt");
String filepath5 = url5.getPath();
System.out.println(filepath5);
readConfigFile(filepath5);
 
 
}
 
// 读取配置文件,打印输出
public static void readConfigFile(String path) {
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeFile(String fileName,String content){
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
bw.write(content);
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
输出:
where are you ????
D:\bin\Tomcat\tomcat7\webapps\ServletPathDemo\2.txt
这是第二个配置文件
D:\bin\Tomcat\tomcat7\webapps\ServletPathDemo\WEB-INF\3.txt
这是第三个配置文件
D:\bin\Tomcat\tomcat7\webapps\ServletPathDemo\WEB-INF\classes\4.txt
这是第四个配置文件
******************************
/D:/bin/Tomcat/tomcat7/webapps/ServletPathDemo/WEB-INF/classes/4.txt
这是第四个配置文件
/D:/bin/Tomcat/tomcat7/webapps/ServletPathDemo/WEB-INF/classes/com/example/5.txt
这是第五个配置文件

0 0