第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java服務(wù)器將html頁面從文件發(fā)送到瀏覽器

Java服務(wù)器將html頁面從文件發(fā)送到瀏覽器

慕少森 2024-01-03 14:07:02
我一直在嘗試制作一個顯示文件索引的java 服務(wù)器。我正在創(chuàng)建一個 SocketServer 并將其連接到一個端口。然后創(chuàng)建一個充當(dāng)客戶端的 Socket,并創(chuàng)建一個連接到客戶端套接字輸出流的 PrintWriter。如果我將頁面硬編碼到 PrintWriter,一切都會正常,但是當(dāng)我嘗試逐行讀取文件并將其發(fā)送到 PrintWriter 時,不會顯示任何內(nèi)容。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");        }            }} 現(xiàn)在我知道服務(wù)器應(yīng)該在一個線程上運行,從技術(shù)上講它目前永遠(yuǎn)無法退出,并且不需要一些導(dǎo)入。我現(xiàn)在并不擔(dān)心這個。我只需要弄清楚為什么 PrintWriter 在瀏覽器上呈現(xiàn) html 時不喜歡使用文件。我創(chuàng)建了一個調(diào)試 PrintWriter,它寫入一個文件來測試我是否有正確的文件位置并且它可以使用它。
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗 獲得超4個贊

當(dāng)您使用 Web 瀏覽器測試服務(wù)器時,您需要發(fā)送有效的 HTTP 請求,其中包括硬編碼部分中提供的 HTTP 標(biāo)頭。


因此,您應(yīng)該在打印文件內(nèi)容之前添加標(biāo)題部分的輸出。


您還需要收集有關(guān)文件大小的信息并發(fā)送"Content-Length: " + file_size + "\r\n"標(biāo)頭。


在讀完頁面文件之前關(guān)閉 printWriter 存在一個錯誤,您需要在循環(huán)之后關(guān)閉它:


    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

    }

因此,將文件發(fā)送到客戶端的更新方法如下:


    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();

    }


查看完整回答
反對 回復(fù) 2024-01-03
  • 1 回答
  • 0 關(guān)注
  • 217 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號