Java服务器将HTML页面从文件发送到浏览器

斯诺伊斯

我一直在尝试制作一个显示文件索引的Java服务器。我正在创建一个SocketServer并将其连接到端口。然后创建一个充当客户端的套接字,并创建一个连接到客户端套接字输出流的PrintWriter。如果将页面硬编码到PrintWriter,一切都会正常,但是当我尝试逐行读取文件并将其发送到PrintWriter时,什么都不会显示。

package com.github.masonnoyce;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Date;
import java.io.File;
import java.nio.file.Files;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;


public class AServer
{
    public static void main (String[] args) throws Exception
    {
        AServer server = new AServer();
        int portNumber = 8080;
        //create server socket and say that it is running
        final ServerSocket myServer = new ServerSocket(portNumber);
        System.out.println("I have Connected To Port " + portNumber);

        boolean running = true;
        while(running)
        {
                //See if anyone connects
                try(Socket client = myServer.accept())
                {                    
                    server.sendPage(client);
                }
                catch(IOException e)
                {
                    System.out.println("Something went wrong streaming the page");
                    System.exit(1);
                }
        }
        try
        {
            myServer.close();
        }
        finally
        {
            System.out.println("Server Is now closed");
        }        
    }
    private void sendPage(Socket client) throws Exception
    {
        System.out.println("Page writter called");

        PrintWriter printWriter = new PrintWriter(client.getOutputStream());//Make a writer for the output stream to the client
        BufferedReader reader = new BufferedReader(new FileReader("path/to/index.html"));//grab a file and put it into the buffer
        String line = reader.readLine();//line to go line by line from file
        while(line != null)//repeat till the file is empty
        {
            printWriter.println(line);//print current line
            printWriter.flush();// I have also tried putting this outside the while loop right before 
            printWriter.close()
            line = reader.readLine();//read next line
        }
        reader.close();//close the reader
        printWriter.close();//Close the writer
//***********This section works if I replace the While loop With It**********//
//            printWriter.println("HTTP/1.1 200 OK");
//            printWriter.println("Content-Type: text/html");
//            printWriter.println("\r\n");
//            printWriter.println("<p> Hello world </p>");
//            printWriter.flush();//needed to actually send the data to the client
//            printWriter.close();//Close the writer
//************Above is the Hardcoding I was talking about****************************// 
    }    
} 

现在,我知道服务器应该在线程上运行,目前它永远不会从技术上退出,并且不需要某些导入。我现在不担心这一点。我只需要弄清楚为什么在浏览器中呈现html时PrintWriter不喜欢使用文件。我已经创建了一个调试PrintWriter,它将写入文件以测试我是否具有正确的文件位置,并且可以使用该文件位置。

亚历克斯·鲁登科

在使用Web浏览器测试服务器时,您需要发送一个有效的HTTP请求,包括在硬编码部分中提供的HTTP标头。

因此,您应该只在打印文件内容之前添加标题部分的输出。

另外,您需要收集有关文件大小的信息,并发送"Content-Length: " + file_size + "\r\n"标头。

在完成阅读页面文件之前,有一个关闭printWriter的错误,您需要在循环后关闭它:

    while(line != null)//repeat till the file is empty
    {
        printWriter.println(line);//print current line
        printWriter.flush();// I have also tried putting this outside the while loop right before 
        printWriter.close() // BUG: no ";" and closing printWriter too early
        line = reader.readLine();//read next line
    }

因此,将文件发送到客户端的更新方法如下:

    private void sendPage(Socket client) throws Exception {
        System.out.println("Page writter called");

        File index = new File("index.html");

        PrintWriter printWriter = new PrintWriter(client.getOutputStream());// Make a writer for the output stream to
                                                                            // the client
        BufferedReader reader = new BufferedReader(new FileReader(index));// grab a file and put it into the buffer
        // print HTTP headers
        printWriter.println("HTTP/1.1 200 OK");
        printWriter.println("Content-Type: text/html");
        printWriter.println("Content-Length: " + index.length());
        printWriter.println("\r\n");
        String line = reader.readLine();// line to go line by line from file
        while (line != null)// repeat till the file is read
        {
            printWriter.println(line);// print current line

            line = reader.readLine();// read next line
        }
        reader.close();// close the reader
        printWriter.close();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将 HTML 页面从 C 网络服务器发送到网络浏览器

来自分类Dev

文件从浏览器发送到不是Web服务器的服务器

来自分类Dev

Ckeditor 4文件浏览器并发送到服务器按钮

来自分类Dev

将数据从浏览器发送到服务器并返回

来自分类Dev

将流从浏览器发送到Node JS服务器

来自分类Dev

将数据本地存储在浏览器中并发送到服务器

来自分类Dev

如何:浏览器将JSON发送到服务器?

来自分类Dev

snappy wkhtmltopdf包装器将生成的html文件发送到浏览器

来自分类Dev

反复将PNG发送到服务器。我想告诉传入的浏览器刷新计时器

来自分类Dev

将EOF发送到浏览器,但通过ajax调用继续在服务器上进行处理

来自分类Dev

如何在使用 React 时将 URL 从浏览器发送到服务器以进行抓取?

来自分类Dev

将浏览器窗口的部分打印为 PDF 并发送到服务器以进行传真

来自分类Dev

如何使用java套接字将文件发送到Web浏览器?

来自分类Dev

将命令从文件发送到服务器 - robotsframework

来自分类Dev

通过 HTML 表单页面安全地将 JSON 发送到服务器

来自分类Dev

错误:将数据从html页面发送到服务器,并从服务器接收一些数据

来自分类Dev

打开连接时,将用户ID从浏览器发送到Websocket服务器

来自分类Dev

如何获取浏览器发送到服务器的数据?

来自分类Dev

C#简单HTTP服务器未将响应发送到浏览器

来自分类Dev

ioSocket 不会在服务器负载过重时发送到浏览器

来自分类Dev

将文件作为块从Java客户端发送到服务器

来自分类Dev

如何将HTML表单数据发送到Java http服务器

来自分类Dev

如何不使用浏览器将数据发送到PHP页面?

来自分类Dev

期望将Ctrl + A发送到浏览器

来自分类Dev

将文件直接从浏览器发送到S3,但更改文件名

来自分类Dev

将php文本输出发送到客户端浏览器并在服务器端运行rest

来自分类Dev

Perl如何将zip文件发送到浏览器以在PSGI中下载

来自分类Dev

如何将FTP文件发送到客户端浏览器?

来自分类Dev

如何将多个二进制文件发送到浏览器?

Related 相关文章

  1. 1

    如何将 HTML 页面从 C 网络服务器发送到网络浏览器

  2. 2

    文件从浏览器发送到不是Web服务器的服务器

  3. 3

    Ckeditor 4文件浏览器并发送到服务器按钮

  4. 4

    将数据从浏览器发送到服务器并返回

  5. 5

    将流从浏览器发送到Node JS服务器

  6. 6

    将数据本地存储在浏览器中并发送到服务器

  7. 7

    如何:浏览器将JSON发送到服务器?

  8. 8

    snappy wkhtmltopdf包装器将生成的html文件发送到浏览器

  9. 9

    反复将PNG发送到服务器。我想告诉传入的浏览器刷新计时器

  10. 10

    将EOF发送到浏览器,但通过ajax调用继续在服务器上进行处理

  11. 11

    如何在使用 React 时将 URL 从浏览器发送到服务器以进行抓取?

  12. 12

    将浏览器窗口的部分打印为 PDF 并发送到服务器以进行传真

  13. 13

    如何使用java套接字将文件发送到Web浏览器?

  14. 14

    将命令从文件发送到服务器 - robotsframework

  15. 15

    通过 HTML 表单页面安全地将 JSON 发送到服务器

  16. 16

    错误:将数据从html页面发送到服务器,并从服务器接收一些数据

  17. 17

    打开连接时,将用户ID从浏览器发送到Websocket服务器

  18. 18

    如何获取浏览器发送到服务器的数据?

  19. 19

    C#简单HTTP服务器未将响应发送到浏览器

  20. 20

    ioSocket 不会在服务器负载过重时发送到浏览器

  21. 21

    将文件作为块从Java客户端发送到服务器

  22. 22

    如何将HTML表单数据发送到Java http服务器

  23. 23

    如何不使用浏览器将数据发送到PHP页面?

  24. 24

    期望将Ctrl + A发送到浏览器

  25. 25

    将文件直接从浏览器发送到S3,但更改文件名

  26. 26

    将php文本输出发送到客户端浏览器并在服务器端运行rest

  27. 27

    Perl如何将zip文件发送到浏览器以在PSGI中下载

  28. 28

    如何将FTP文件发送到客户端浏览器?

  29. 29

    如何将多个二进制文件发送到浏览器?

热门标签

归档