如何定义一个Servlet,读取一个文本文件的内容,并且在页面上打印文件的内容
我的代码如下,运行后显示”文件不存在!“但是我新建文本文件,里面有内容的呀packagecom.javaweb.ch10;importjava.io.BufferedRe...
我的代码如下,运行后显示”文件不存在!“但是我新建文本文件,里面有内容的呀
package com.javaweb.ch10;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class fileRead extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
String fileName="ch10/content.txt";
String realPath=request.getRealPath(fileName);
File file=new File(realPath);
if (file.exists()){
FileReader reader=new FileReader(file);
BufferedReader bufferReader=new BufferedReader(reader);
String line=null;
while((line=bufferReader.readLine()) !=null){
out.println(line+"<br>");
}
}else{
out.print("文件不存在!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
} 展开
package com.javaweb.ch10;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class fileRead extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
String fileName="ch10/content.txt";
String realPath=request.getRealPath(fileName);
File file=new File(realPath);
if (file.exists()){
FileReader reader=new FileReader(file);
BufferedReader bufferReader=new BufferedReader(reader);
String line=null;
while((line=bufferReader.readLine()) !=null){
out.println(line+"<br>");
}
}else{
out.print("文件不存在!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
} 展开
3个回答
展开全部
Serverlet里的路径,和你的项目路径是不一样的。
前者是容器里的路径,也就是你的项目发布到Tomcat或者其他server上的路径
后者是你本地workspace路径
你可以用这个方法来获得绝对路径
File f = new File("");
String absolutePath = f.getAbsolutePath();
System.out.println(absolutePath);
//read jms properties
File configFile = new File(absolutePath+"\\your_file_name.txt");
if (configFile == null) {
throw new Exception("Configuration File Not Found: facs_fasg.properties");
}
InputStream propertiesInStream = configFile.toURL().openStream();
前者是容器里的路径,也就是你的项目发布到Tomcat或者其他server上的路径
后者是你本地workspace路径
你可以用这个方法来获得绝对路径
File f = new File("");
String absolutePath = f.getAbsolutePath();
System.out.println(absolutePath);
//read jms properties
File configFile = new File(absolutePath+"\\your_file_name.txt");
if (configFile == null) {
throw new Exception("Configuration File Not Found: facs_fasg.properties");
}
InputStream propertiesInStream = configFile.toURL().openStream();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询