【发布时间】:2017-03-08 03:46:29
【问题描述】:
我有这个简单的 HTML,当我在 Chrome 中加载它时,它可以加载我想要的方式。
<body>
<div><h1>Welcome to my webpage!</h1></div>
<div>This page is being hosted on the local machine.</div>
<div>Now, here's a picture of a cat. Please enjoy.</div>
<img src="cat.jpg" alt="Business Cat" width="800" height="600"/>
</body>
当我通过 Java 中的套接字传递它时,我总是得到一个损坏的图像。我不知道为什么,因为我只是通过套接字传递字节。
File index = new File("index.html");
byte[] bytes = new byte[16 * 1024];
InputStream in = new FileInputStream(index);
while (true)
{
int read = in.read(bytes);
if (read < 0)
break;
out.write(bytes, 0, read);
}
out.flush();
out.close();
图像文件“cat.jpg”与“index.html”位于同一目录中。我错过了什么?
【问题讨论】:
标签: java html sockets client-server