2012-10-16 32 views
2

我試圖在HTTP Web服務器中顯示圖像,但我無法。我可以顯示HTML。我認爲這與我處理IO(輸入和輸出流)的方式有關。那裏可能有很多我沒有注意到的錯誤。如何顯示來自HTTP Web服務器的圖像?

import java.io.* ; 
import java.net.* ; 
import java.util.Properties; 


public class HTTPThread extends Thread 
{ 
    private Socket socket = null; 
    private Properties config = null; 
    private String root = ""; 

    public HTTPThread(Socket s, Properties config) 
    { 
     this.socket = s; 
     this.config = config; 
     this.root = this.config.getProperty("root");   
    } 

    public void run() 
    { 

//  InputStream in = null; 
     OutputStream out = null; 

     try 
     { 
      out = socket.getOutputStream(); 
      PrintWriter writer = new PrintWriter(out, true); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      String request = reader.readLine(); 
      writer.println("HTTP/1.1 200 OK"); 
      writer.println("Content-Type: text/html");   
      writer.println(); 

      // Search for filename 
      int slash = request.indexOf("/"); //find first occurrence of slash 
      int emptySpace = request.indexOf(" ", slash); //find first space after slash 
      String filename = request.substring(slash, emptySpace); //return filename 
//   writer.println("<b>" + filename + "</b>"); 

      String pathname = ""; 
      try 
      { 
       pathname = (filename == "/") ? root + filename : root; 
//    System.out.println(filename);   
       URL url = new URL(pathname); 
       URLConnection urlc = url.openConnection(); 
       BufferedReader in = new BufferedReader(
         new InputStreamReader(
         urlc.getInputStream())); 

       String line;     
       while ((line = in.readLine()) != null) 
       { 
        writer.println(line); 
       } 
       in.close(); 
      } 
      catch (MalformedURLException e) 
      { 
       System.err.println("Don't know about host: " + pathname); 
       System.exit(1); 
      } 
      catch (IOException e) 
      { 
        System.err.println("Couldn't get I/O for " 
            + "the connection to: " + pathname); 
        System.exit(1); 
      } 



//   reader.close(); 
      writer.close(); 
      socket.close(); 
     } 
     catch(IOException e) 
     { 
      System.out.println("Error: " + e);   
     } 

     finally 
     { 
      try 
      { 
//    in.close() ; 
       out.close() ; 
       socket.close(); 
      } 
      catch(IOException e) 
      { 
       System.out.println("Error: " + e);   
      } 
     } 
    } 
} 

回答

3

你想寫某種代理服務器是需要從請求外部URL和返回的內容?那麼,你的代碼有幾個問題:

writer.println("HTTP/1.1 200 OK"); 
writer.println("Content-Type: text/html");   

當瀏覽器看到上面的內容時,它會假設返回的是HTML。呈現二進制圖像作爲HTML將明顯失敗。我們哪會導致這樣的:

String line;     
while ((line = in.readLine()) != null) 
{ 
    writer.println(line); 
} 
in.close(); 

在上述循環您正在閱讀一些外部URL行由行(文本模式),並將其轉發給原始客戶端。這適用於HTML(基於文本),但對於任何圖像(二進制)都將失敗。您必須改用InputStream/OutputStream

,並在年底小東西:

pathname = (filename == "/") ? root + filename : root; 

不要比較使用==操作字符串,將其替換爲:

pathname = (filename.equals("/")) ? root + filename : root; 

最後一點,可以考慮使用servlet容器如Tomcat或Jetty ,它將從HTTP處理代碼中釋放你並提供更多高級構造。

+0

謝謝。你能指出一些能引導我如何從URL輸入圖像並輸出到瀏覽器的東西,就像我在這裏做的那樣?我嘗試過搜索谷歌,但找不到任何東西=/ –

+0

@JohnathanAu:從http://stackoverflow.com/questions/43157開始,瞭解'Reader'和'InputStream'之間的區別。 –

相關問題