1

大家好,我已经用 Java 创建了一个 Web 服务器。它提供目录中的文件。它工作正常,一切都很好,除了当我尝试访问其中包含图像的文件时,它不会加载它们。

这是代码。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

         new Main().runserver();






    }

     ServerSocket serverSocket;





    public void runserver() throws Exception {


        serverSocket = new ServerSocket(8080);
        acceptRequest();

    }

    private void acceptRequest() throws Exception{

        while(true){

            Socket s = serverSocket.accept();

            ConnectionHandler ch = new ConnectionHandler(s);
            ch.start();
        }

    }


       public class ConnectionHandler extends Thread {

           PrintWriter pw;
           BufferedReader br;

           Socket s;
           public ConnectionHandler(Socket s) throws Exception{
               this.s = s;

               br = new BufferedReader(new InputStreamReader(s.getInputStream()));
               pw = new PrintWriter(s.getOutputStream()); 



           }

           @Override
        public void run() {
               try{

               String reqS = "";


               while (br.ready() || reqS.length() == 0){

                   reqS += (char) br.read();
        }

               System.out.println(reqS);

               HttpRequest req = new HttpRequest(reqS);
               HttpResponse res = new HttpResponse(req);


               pw.write(res.response.toCharArray());
               pw.close();
               br.close();
               s.close();


               }
               catch (Exception e) {
                   e.printStackTrace(); 
                   }
               }


       }


       public class HttpRequest{
           public String filename ;

        public HttpRequest(String request){

               String lines[] = request.split("\n");
               lines = lines[0].split(" ");
               filename = lines[1];


           } 

       }
             public class HttpResponse{

                 HttpRequest req;

                 String root;

                 String response;

                 public HttpResponse(HttpRequest request){
                     req=request;

                     root = "D:/";

                     File f  = new File(root + req.filename);


                     try{


                     response+= "HTTP/1.1 200 \r\n";
                     response+= "Apache Server /1.0";
                     response+= "Content-Type: text/html \r\n";
                     response+="Connection: close \r\n";
                     response+= "Content-Length:" + f.length() + "\r\n";
                     response+= "\r\n";

                     FileInputStream fis = new FileInputStream(f);


                     int s;

                     while ((s = fis.read()) != -1){

                         response += (char)s ;


                     }

                     fis.close(); 



                 }catch(FileNotFoundException fg){
                     response = response.replace("200", "404");

                 }

                     catch(IOException e ){

                         response  = response.replace("200", "500");

                         e.printStackTrace();
                     }
             }




}

}
4

1 回答 1

3

您为每个项目明确设置内容类型text/html

response+= "Content-Type: text/html \r\n";

这对于文本和/或 HTML 的内容可以正常工作,但对于其他任何内容(例如图像),它会混淆 Web 浏览器。您基本上是在告诉 Web 浏览器将图像的原始数据视为文本。它试图遵守这一点,但它自然无法这样做。

在构建 HTTP 响应时,您的 Web 服务器将必须确定文件类型并为该文件使用正确的内容类型

于 2013-07-20T15:47:05.253 回答