servlet读文件

package com.yuan;
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 ServletTextAction extends HttpServlet {

 /**
  *
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  response.setCharacterEncoding("gb2312");
  PrintWriter out = response.getWriter();
  
  File file = new File("e://yuan.txt");
  if(file.exists()){
   FileReader reader  = new FileReader(file);
   BufferedReader bufferReader = new BufferedReader(reader);
   
   String line = null;
   while((line = bufferReader.readLine())!=null){
    out.print(line);
   }
  }else{
   out.print("文件不存在");
  }
  
  
  out.flush();
  out.close();
 }
 
 /**
  * The doPost method of the servlet. <br>
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   doGet(request,response);
 }

}

你可能感兴趣的:(java,html,servlet)