2012-10-16 34 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'之间的区别。 –

相关问题