问题描述
我使用 Java 中的 ServerSocket 创建了一个 Web 服务器。
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
PrintWriter os = new PrintWriter(socket.getoutputStream(),true);
os.print("HTTP/1.0 200" + "\r\n");
os.print("Content type: text/html" + "\r\n");
os.print("Content length: " + html.length() + "\r\n");
os.print("\r\n");
os.print(html + "\r\n");
os.flush();
socket.close();
现在我可以在请求到来时成功地将 html 页面发送到 Web 浏览器。但是我无法将服务器中的图像文件显示到网页
我试过下面的代码
OutputStream outputStream = socket.getoutputStream();
BufferedImage image = ImageIO.read(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image,"jpg",byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
Failed to load resource: net::ERR_INVALID_HTTP_RESPONSE
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)