【问题标题】:How to print the contents of a file on the Browser如何在浏览器上打印文件的内容
【发布时间】:2018-04-05 11:47:29
【问题描述】:

读取文件类

public class  ReadFile {

    public void  readFile() throws IOException {

        BufferedReader is = new BufferedReader(new FileReader("D:\\text.txt"));

        if (is != null) {
            BufferedReader reader = new BufferedReader(is);
            String text;

            while ((text = reader.readLine()) != null) {

            }
            is.close();


        }
    }
    public static void  main(String[] args) throws IOException {

        ReadFile read=new ReadFile();
        read.readFile();
    }
}   

Servlet 类

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{
    resp.setContentType("text/html");

    ReadFile readfile = new ReadFile();
    readfile.readFile();
}

【问题讨论】:

  • 你能解释一下是什么问题吗?看来您阅读了该文件,但对该行没有做太多...可能是问题...
  • 您可以参考stackoverflow.com/questions/4716503/…阅读文本文件,也可以参考stackoverflow.com/questions/20593832/…
  • @AxelH 我创建了一个读取类来读取文件的内容。我已经创建了一个 servlet 类,并且我正在调用读取文件.. 我的问题是如何在浏览器中打印文件?
  • 如果您已经阅读了文本文件,然后您只需将文本打印到响应中。
  • @ShafinMahmud 但是如何在 servlet 类中做到这一点。对不起,我是 java 新手,需要知道。请帮帮我

标签: java servlets


【解决方案1】:

ReadFile 类需要接收一个 PrintWriter 参数,resp.getWriter()。 在读取文件时都没有处理字符编码,就像在浏览器中呈现它一样。这里我选择了 UTF-8。

对于文件,它可能是Charset.forName("Windows-1252") 或类似的。

由于 java Files 类具有 ReadFile 可以做的所有事情,所以我使用了它。

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    Path path = Paths.get("D:\\text.txt");
    Files.lines(path, StandardCharsets.UTF_8)
        .forEach(out::println));
}

或使用 HTML 格式:

    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    out.println("<!DOCTYPE html>");
    out.println("<html><head>");
    out.println("<meta charset='UTF-8'>");
    out.println("<title>The text<title>");
    out.println("</head><body><pre>");
    Path path = Paths.get("D:\\text.txt");
    Files.lines(path, StandardCharsets.UTF_8)
        .forEach(line -> {
             line = line.replace("&", "&amp;")
                 .replace("<", "&lt;")
                 .replace(">", "&gt;");
             out.printl(line);
        });
    out.println("</pre></body></html>");

使用 ReadFile 类:

public class  ReadFile {

    public void  readFile(PrintWriter out) throws IOException {
        Path path = Paths.get("D:\\text.txt");
        Files.lines(path, StandardCharsets.UTF_8)
            .forEach(out::println));
    }

    public static void main(String[] args) throws IOException {   
        ReadFile read = new ReadFile();
        read.readFile(new PrintWriter(System.out));
    }
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    PrintWriter out = resp.getWriter();
    ReadFile read = new ReadFile();
    read.readFile(System.out);
}

【讨论】:

  • 但文件路径只能在 ReadFileClass 中给出。在 servelt 类中,我们应该只调用 ReadFile 类
  • 我尝试了上面的代码,即使文件的内容没有显示在浏览器上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多