【问题标题】:How to tell the Browser that the data that its getting is html and not regular text?如何告诉浏览器它获取的数据是 html 而不是常规文本?
【发布时间】:2020-03-19 16:22:06
【问题描述】:

我编写了一个 Java 服务器应用程序,它在浏览器请求文件时返回一个文件。浏览器向我的套接字发出 GET 请求,套接字返回文件。但是浏览器(在我的例子中是 firefox)将 html 文件视为常规文本文件,并且不会呈现实际页面。所以浏览器显示了整个 html 源代码。我该如何解决?

这里是代码:

package ml.mindlabor.networking;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    static final int PORT = 9806;
    static final int MAX_BYTES_PER_STREAM = 10_000; // 10 kB
    static final String FILE_SYSTEM_PATH = "C:\\Users\\SBrau\\eclipse-workspace\\Networking\\src\\ml\\mindlabor\\networking\\Files\\public_html";

    static boolean running = true;

    ServerSocket ss = null;
    Socket soc = null;

    public static void main(String[] args) {
        Server server = new Server();

        // When the connection could not be established
        System.out.println("Waiting for connection ...");
        if (!server.connect()) return;

        server.listenForResponse();
    }

    public Server() {
        try {
            ss = new ServerSocket(PORT);
        } catch (IOException e) {
            System.err.println("Could not create ServerSocket on Port " + PORT);
            shutdown();
        }
    }

    boolean respond(String response) {
        try {
            PrintWriter out = new PrintWriter(soc.getOutputStream(), true);
            out.println(response);
            return true;
        } catch (IOException e) {
            System.err.println("Could not send to Port " + PORT);
        }
        return false;
    }

    boolean respond(byte[] response) {
        try {
            soc.getOutputStream().write(response);
            return true;
        } catch (IOException e) {
            System.err.println("Could not send to Port " + PORT);
        }
        return false;
    }

    boolean respondFile(String relPath) {
        String path = Server.FILE_SYSTEM_PATH + relPath;
        File file = new File(path);

        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

            byte[] buffer = new byte[(int)file.length()]; // or 4096, or more
            in.read(buffer, 0, buffer.length);
            soc.getOutputStream().write(buffer, 0, buffer.length);
            System.out.println("Loaded :D");
            in.close();
            soc.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    String rawDataToString(byte[] rawData) {
        return new String(rawData);
    }

    void listenForResponse() {
        new Thread(() -> {
            while (true) {
                try {
                    DataInputStream in = new DataInputStream(soc.getInputStream());

                    byte[] packetData = new byte[MAX_BYTES_PER_STREAM];
                    in.read(packetData);
                    receivedPackage(packetData);
                } catch (IOException e) {
                    System.err.println("Could not get data from port " + PORT);
                    shutdown();
                }
            }
        }).start();
    }

    void shutdown() {
        Server.running = false;
    }

    void receivedPackage(byte[] pkg) {
        String request = new String(pkg).trim();

        // GET Request for file
        if (request.contains("GET ")) {
            String[] arr = request.split(" ");
            respondFile(arr[1].trim());
        }
    }

    boolean connect() {
        try {
            soc = ss.accept();
            //soc.setKeepAlive(true);
            System.out.println("Connected!");
            return true;
        } catch (IOException e) {
            System.err.println("Could not wait for connection on port " + PORT);
            shutdown();
        }
        return false;
    }

}

【问题讨论】:

    标签: java html sockets networking server


    【解决方案1】:

    好的。知道了。我通过重写以下方法解决了它:

    boolean respondFile(String relPath) {
        String path = Server.FILE_SYSTEM_PATH + relPath;
        File file = new File(path);
    
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            PrintWriter out = new PrintWriter(soc.getOutputStream());
            BufferedOutputStream dataOut = new BufferedOutputStream(soc.getOutputStream());
    
            byte[] fileData = readFileData(file, (int)file.length());
            out.println("HTTP/1.1 501 Not Implemented");
            out.println("Content-type: text/html");
            out.println(); // blank line between headers and content, very important !
            out.flush();
            dataOut.write(fileData, 0, fileData.length);
            dataOut.flush();
    
            System.out.println("Loaded :D");
            in.close();
            soc.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    
        return true;
    }
    
    private byte[] readFileData(File file, int fileLength) throws IOException {
        FileInputStream fileIn = null;
        byte[] fileData = new byte[fileLength];
    
        try {
            fileIn = new FileInputStream(file);
            fileIn.read(fileData);
        } finally {
            if (fileIn != null) 
                fileIn.close();
        }
    
        return fileData;
    }
    

    【讨论】:

    • 既然你回答了你自己的问题,我就补充一点。浏览器查看 Content-Type 标头以确定您在答案中所做的响应类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 2014-06-29
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多