gpt4 book ai didi

java - 通过套接字 java 发送时,HTML 页面不会加载图像

转载 作者:行者123 更新时间:2023-11-28 02:43:13 25 4
gpt4 key购买 nike

我有这个简单的 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”位于同一目录中。我错过了什么?

最佳答案

我认为问题在于它发送了另一个 http 请求。你应该做出规定,例如:127.0.0.1/index.html 来显示你的 index.html 文件,而 127.0.0.1/cat.jpg 应该返回你的图片。但是我不确定您将如何在 java 中解析 jpg 文件。但是如果你在 chrome 中并且你去检查;然后你去网络。如果您从那里重新加载,您将看到对您的图片的请求处于待处理状态,或者您返回的信息不正确,具体取决于您如何实现我们对 HTTP 请求的处理。

关于java - 通过套接字 java 发送时,HTML 页面不会加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com