java - 如何通过 Java Web 服务器在浏览器中显示图像/gif

标签 java sockets http-headers webserver

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

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

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

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

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

    }

最佳答案

通过使用 BufferedReader,您将损坏 JPG/GIF 文件,因为这会将一系列字节 -> 逐行转换为字符 -> 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 的流。

关于java - 如何通过 Java Web 服务器在浏览器中显示图像/gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62233839/

相关文章:

java - popBackStack 导致 java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState

php - 基于 accept header 的 ZF2 返回格式

java - 如何在debian虚拟机上安装java

java - 如何使用套接字与socketio交互

ios - iOS 上的 Bonjour 问题

java - 服务器套接字 - 发送测试消息来检测连接

jquery - 如何从服务器端停止重定向

php - 我应该为多语言网站设置哪些 http header ?

Java Swing 问题 - 使用调色板

java - 基于 Minecraft 世界的加密。是否可以?