【问题标题】:How to display an image/gif in a browser through a Java Web Server如何通过 Java Web 服务器在浏览器中显示图像/gif
【发布时间】:2020-06-06 15:23:10
【问题描述】:

我有一个简单的 Java Web 服务器,我试图用它在浏览器中显示图像。

到目前为止,我拥有它,因此当转到 localhost:7500/image2.jpg 时,它会下载图像而不是在浏览器中显示它

当转到 gif 扩展 (localhost:7500/image33.gif) 时,它只显示一个黑色的小方块。

这是我到目前为止所做的:

    public void getType(File f, String path, BufferedReader bfr)
    {
        String extention = path.substring(path.lastIndexOf("/") + 1);
        try {
        if (extention == "gif")
        {
            String line;
            String httpResponse = "HTTP/1.1";
            httpResponse += " 200 OK \n";
            httpResponse += "Content-Type: image/gif\n" ;
            httpResponse += "Content-Length: " + f.length()+"\n\n";

            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }
        }
        else if (extention == "jpg")
        {
            String line;
            String httpResponse = "HTTP/1.1";
            httpResponse += " 200 OK \n";
            httpResponse += "Content-Type: image/jpg\n" ;
            httpResponse += "Content-Length: " + f.length()+"\n\n";

            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }

        }
        else
        {
            String line;
            String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }
        }
    }catch(Exception ex)
      {
      //when page is loaded, print confirmation to system
      System.out.println("999999999");
      }

    }

【问题讨论】:

    标签: java sockets http-headers webserver


    【解决方案1】:

    使用 BufferedReader 会破坏 JPG/GIF 文件,因为这会将一系列字节 -> 逐行转换为 char -> UTF-8 字节。相反,您应该将 GIF/JPG 作为 InputStream 打开,并将它们直接写入 servlet 输出流而不作改变。摆脱这些循环:

    //loop to print each line of file to browser
    while ((line = bfr.readLine()) != null) 
    {
       serverClient.getOutputStream().write(line.getBytes("UTF-8"));
    }
    

    只需使用 NIO Files 直接复制图像,无需任何转换:

    Files.copy(f.toPath(), serverClient.getOutputStream());
    

    bfr 上的第三个也是最后一个循环可能会也可能不会工作,具体取决于文件所代表的内容。您的文件可能是基于字符的(例如 TXT)文件,因此一次写入一行调用 BufferedReader readLine 将起作用,如果您在 HTTP servlet 响应上设置字符集,这将很有帮助。如果文件是二进制格式,例如 MP3 等,那么您的第三个循环只会像 GIF/JPG 一样破坏流。

    【讨论】:

    • 感谢您的回复。但是,我不太确定您的意思。您能否提供一个如何执行此操作的示例?
    • 我添加了更多细节,我希望这能解释它
    • 您似乎也没有正确提取文件扩展名的值
    • 字符串比较错误,因为 == 比较引用,"gif".equals(ext) 将检查 2 个不同字符串引用的值。
    猜你喜欢
    • 2018-01-22
    • 2012-09-03
    • 2015-05-20
    • 1970-01-01
    • 2023-04-03
    • 2017-06-01
    • 1970-01-01
    • 2023-04-07
    • 2020-01-10
    相关资源
    最近更新 更多