2013-07-20 36 views
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(); 
        } 
      } 




} 

} 

回答

3

你明確的Content Type設置爲text/html爲每個項目:

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

這將工作的優良它是文本和/或HTML內容,但爲別的(如圖片)它會混淆網絡瀏覽器。你基本上是告訴網絡瀏覽器將圖像的原始數據作爲文本來處理。它試圖遵守這一點,但自然無法這樣做。

在構建HTTP響應時,您的Web服務器將不得不確定文件類型併爲該文件使用正確的Content Type

+0

好的,你可以給我代碼片段來檢查文件類型和做出正確的迴應,或者請你告訴我如何在活動中使用nanohttpd。 thanx答覆將upvote。 – Prakhar

+0

@ user2589896:我不能給你完整的代碼,沒有。然而,快速谷歌搜索「java獲取文件的內容類型」引發了一個相關的問題,並提供了非常有用的信息:http://stackoverflow.com/questions/51438/getting-a-files-mime-type- in-java – David

+0

嘿,你能告訴我如何在活動中使用nanohttpd。 – Prakhar