2

我试图在我的 HTTP Web 服务器中显示图像,但我无法。我可以显示 HTML。我认为这与我处理 IO(输入和输出流)的方式有关。那里可能有很多我没有注意到的错误。

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);          
            }
        }
    }
}
4

1 回答 1

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;

最后一点,考虑使用 Tomcat 或 Jetty 之类的 servlet 容器,这将使您摆脱 HTTP 处理代码并提供更高级的构造。

于 2012-10-16T19:48:05.117 回答