【发布时间】:2015-05-20 22:18:32
【问题描述】:
我用 Java 中的 TCP 套接字构建了一个简单的并发 Web 服务器。它可以很好地提供 html、txt、css 等内容,但我在提供图像文件(在这种情况下为 gif)时遇到问题。
我认为这个过程还可以,因为浏览器获取的图像状态为 200 OK,并且数据(路径、文件大小等)也可以;但我无法实现浏览器显示图像,它总是显示带有替代文本的空图像。图片的路径也可以。
我尝试了不同的方法将图像提供给客户,但到目前为止没有一个对我有用。
这是我用于提供图片/gif 的代码:
out = new PrintWriter(client.getOutputStream());
httpHeader= (h+" 200 OK \n" +
"Content-Type: image/gif"+"\n" +
"Content-Length: "+f.length()+"\n\n");
//Send header to the client
out.println(httpHeader);
out.flush();
//Send gif file content to the cliente
returnGIF(f);
private void returnGIF(File f) throws IOException{
FileInputStream fis = new FileInputStream(f.getPath());
int b=0;
while ((b=fis.read()) != -1){
out_bis.write(b);
}
fis.close();
}
【问题讨论】:
-
尝试将 responseType:'arraybuffer' 添加到标题中。
-
returnGif如何获取客户端的输出流?
-
@SomeRandomName 谢谢!!这就是解决方案!您可以将其发布为答案 =)
标签: java image http server gif