18910140161

java-local hosted映像在宿主HTTPSocket服务器时不显示在浏览器上-堆栈溢出

顺晟科技

2022-10-19 13:39:26

194

编辑:想通了。我使用PrinterWriter来编写标题,并使用OutputStream来编写内容。我使用OutputStream来编写头、内容和加载的图像。

我正在使用ServerSocket用Java创建一个简单的服务器。当我运行代码并连接到localhost站点时,它会正确地显示我的html和css,但不显示我的本地宿主图像,并显示alt文本。

当我在没有服务器的情况下通过firefox打开html文件时,图像会加载。我99%肯定文件路径是正确的,因为其他文件(即我的css文件)检索是正确的。

当直接链接到熊(警告:可爱的熊)时,熊会在我的html中显示。

有什么想法吗?谢谢。

以下是我的文件布局: 文件布局

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
    // HttpServer object contains a ServerSocket object, representing the server, and an
    // ArrayList which contains the sockets connected
    private ServerSocket server_;
    private ArrayList<Socket> sockets_ = new ArrayList<>();
    // Starts the server and runs a while loop waiting for sockets to connect. If a socket
    // connects, serve the client the requested page or index.html
    public static void main(String[] args) throws IOException {
        HttpServer testServer = new HttpServer(8080);
        boolean cont = true;
        while(cont){
            Request newRequest = testServer.AcceptSocket();
            Response newResponse = testServer.generateResponse(newRequest);
            cont = testServer.Respond(newResponse);
        }
    }
    // HttpServer constructor that makes a server at the specified port
    public HttpServer(int port) throws IOException {
        server_ = new ServerSocket(port);
    }
    // Waits for a client socket to connect. Returns a Request object representing the
    // client's request
    public Request AcceptSocket() throws IOException {
        sockets_.add(server_.accept());
        return new Request(sockets_.get(sockets_.size()-1).getInputStream());
    }
    // Returns a Response object when given a Request object
    public Response generateResponse(Request newRequest) throws IOException {
        return new Response(new File("Resources" + newRequest.getPath_()));
    }
    // Writes out Response to the most recently connected client socket based on the given
    // Response object
    public boolean Respond(Response newResponse) throws IOException {
        try{
            OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println(newResponse.getHeader_());
            writer.print(newResponse.getResponse_());
            writer.flush();
            writer.close();
            sockets_.get(sockets_.size()-1).close();
            return true;
        }
        catch(FileNotFoundException e){
            return false;
        }

    }
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
    // HttpServer object contains a ServerSocket object, representing the server, and an
    // ArrayList which contains the sockets connected
    private ServerSocket server_;
    private ArrayList<Socket> sockets_ = new ArrayList<>();
    // Starts the server and runs a while loop waiting for sockets to connect. If a socket
    // connects, serve the client the requested page or index.html
    public static void main(String[] args) throws IOException {
        HttpServer testServer = new HttpServer(8080);
        boolean cont = true;
        while(cont){
            Request newRequest = testServer.AcceptSocket();
            Response newResponse = testServer.generateResponse(newRequest);
            cont = testServer.Respond(newResponse);
        }
    }
    // HttpServer constructor that makes a server at the specified port
    public HttpServer(int port) throws IOException {
        server_ = new ServerSocket(port);
    }
    // Waits for a client socket to connect. Returns a Request object representing the
    // client's request
    public Request AcceptSocket() throws IOException {
        sockets_.add(server_.accept());
        return new Request(sockets_.get(sockets_.size()-1).getInputStream());
    }
    // Returns a Response object when given a Request object
    public Response generateResponse(Request newRequest) throws IOException {
        return new Response(new File("Resources" + newRequest.getPath_()));
    }
    // Writes out Response to the most recently connected client socket based on the given
    // Response object
    public boolean Respond(Response newResponse) throws IOException {
        try{
            OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println(newResponse.getHeader_());
            writer.print(newResponse.getResponse_());
            writer.flush();
            writer.close();
            sockets_.get(sockets_.size()-1).close();
            return true;
        }
        catch(FileNotFoundException e){
            return false;
        }

    }
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
    // HttpServer object contains a ServerSocket object, representing the server, and an
    // ArrayList which contains the sockets connected
    private ServerSocket server_;
    private ArrayList<Socket> sockets_ = new ArrayList<>();
    // Starts the server and runs a while loop waiting for sockets to connect. If a socket
    // connects, serve the client the requested page or index.html
    public static void main(String[] args) throws IOException {
        HttpServer testServer = new HttpServer(8080);
        boolean cont = true;
        while(cont){
            Request newRequest = testServer.AcceptSocket();
            Response newResponse = testServer.generateResponse(newRequest);
            cont = testServer.Respond(newResponse);
        }
    }
    // HttpServer constructor that makes a server at the specified port
    public HttpServer(int port) throws IOException {
        server_ = new ServerSocket(port);
    }
    // Waits for a client socket to connect. Returns a Request object representing the
    // client's request
    public Request AcceptSocket() throws IOException {
        sockets_.add(server_.accept());
        return new Request(sockets_.get(sockets_.size()-1).getInputStream());
    }
    // Returns a Response object when given a Request object
    public Response generateResponse(Request newRequest) throws IOException {
        return new Response(new File("Resources" + newRequest.getPath_()));
    }
    // Writes out Response to the most recently connected client socket based on the given
    // Response object
    public boolean Respond(Response newResponse) throws IOException {
        try{
            OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println(newResponse.getHeader_());
            writer.print(newResponse.getResponse_());
            writer.flush();
            writer.close();
            sockets_.get(sockets_.size()-1).close();
            return true;
        }
        catch(FileNotFoundException e){
            return false;
        }

    }
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
    // HttpServer object contains a ServerSocket object, representing the server, and an
    // ArrayList which contains the sockets connected
    private ServerSocket server_;
    private ArrayList<Socket> sockets_ = new ArrayList<>();
    // Starts the server and runs a while loop waiting for sockets to connect. If a socket
    // connects, serve the client the requested page or index.html
    public static void main(String[] args) throws IOException {
        HttpServer testServer = new HttpServer(8080);
        boolean cont = true;
        while(cont){
            Request newRequest = testServer.AcceptSocket();
            Response newResponse = testServer.generateResponse(newRequest);
            cont = testServer.Respond(newResponse);
        }
    }
    // HttpServer constructor that makes a server at the specified port
    public HttpServer(int port) throws IOException {
        server_ = new ServerSocket(port);
    }
    // Waits for a client socket to connect. Returns a Request object representing the
    // client's request
    public Request AcceptSocket() throws IOException {
        sockets_.add(server_.accept());
        return new Request(sockets_.get(sockets_.size()-1).getInputStream());
    }
    // Returns a Response object when given a Request object
    public Response generateResponse(Request newRequest) throws IOException {
        return new Response(new File("Resources" + newRequest.getPath_()));
    }
    // Writes out Response to the most recently connected client socket based on the given
    // Response object
    public boolean Respond(Response newResponse) throws IOException {
        try{
            OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println(newResponse.getHeader_());
            writer.print(newResponse.getResponse_());
            writer.flush();
            writer.close();
            sockets_.get(sockets_.size()-1).close();
            return true;
        }
        catch(FileNotFoundException e){
            return false;
        }

    }
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
    // HttpServer object contains a ServerSocket object, representing the server, and an
    // ArrayList which contains the sockets connected
    private ServerSocket server_;
    private ArrayList<Socket> sockets_ = new ArrayList<>();
    // Starts the server and runs a while loop waiting for sockets to connect. If a socket
    // connects, serve the client the requested page or index.html
    public static void main(String[] args) throws IOException {
        HttpServer testServer = new HttpServer(8080);
        boolean cont = true;
        while(cont){
            Request newRequest = testServer.AcceptSocket();
            Response newResponse = testServer.generateResponse(newRequest);
            cont = testServer.Respond(newResponse);
        }
    }
    // HttpServer constructor that makes a server at the specified port
    public HttpServer(int port) throws IOException {
        server_ = new ServerSocket(port);
    }
    // Waits for a client socket to connect. Returns a Request object representing the
    // client's request
    public Request AcceptSocket() throws IOException {
        sockets_.add(server_.accept());
        return new Request(sockets_.get(sockets_.size()-1).getInputStream());
    }
    // Returns a Response object when given a Request object
    public Response generateResponse(Request newRequest) throws IOException {
        return new Response(new File("Resources" + newRequest.getPath_()));
    }
    // Writes out Response to the most recently connected client socket based on the given
    // Response object
    public boolean Respond(Response newResponse) throws IOException {
        try{
            OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            writer.println(newResponse.getHeader_());
            writer.print(newResponse.getResponse_());
            writer.flush();
            writer.close();
            sockets_.get(sockets_.size()-1).close();
            return true;
        }
        catch(FileNotFoundException e){
            return false;
        }

    }
}

顺晟科技:

答案是使用OutputStream write()函数写出已读取的文件。对文件的头和内容都使用write()。

我的错误是将PrintWriter用作标题,而将write()用作内容,这是行不通的。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航