gpt4 book ai didi

java - 为什么图像在通过套接字写入发送时不显示在客户端浏览器中?

转载 作者:行者123 更新时间:2023-11-30 10:58:19 25 4
gpt4 key购买 nike

我正在创建一个简单的 HTTP 服务器(使用 Maven)。当用户输入 http://127.0.0.1:8181/pic 时,应该会显示一张图片。

当我运行类并输入http://127.0.0.1:8181/pic时,图像不显示;我只是得到了几行垃圾字符!

代码:

public static void main(String[] args) throws Exception {
int port = 8181;

ServerSocket server = new ServerSocket(port);
System.out.println("Listening for a connection on port " + port + " ...");

while (true) {
Socket client = server.accept();
System.out.println("A client connected: " + client.getInetAddress());

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

String line = in.readLine();
while (!line.isEmpty()) {
System.out.println(line);

String[] firstLine = line.split("\\s+"); // line is like: GET /pic HTTP/1.1
String method = firstLine[0];

if (method.equalsIgnoreCase("GET")) {

String resourceNme = firstLine[1];

if (resourceNme.equals("/pic")) {

out.write("Server: Apache/0.8.4\r\n");
out.write("Content-Type: text/html\r\n");
out.write("Content-type: image/gif");

out.write("\r\n");
out.write("<TITLE>Example Image</TITLE>");

//find file in system and send as response
URL url = SimpleHTTPServer.class.getResource("/office.jpg");
File file = new File(url.toURI());

if (file.exists()) {
System.out.println("file exists");
ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
long size = imageInputStream.length();

BufferedImage bufferedImage = ImageIO.read(file);
boolean success = ImageIO.write(bufferedImage, "jpeg", client.getOutputStream());
System.out.println("Success: " + success); // is true
} else {
System.out.println("don't exists");
}

out.close();
in.close();
client.close();
break;

}
}
}
}
}

收到的示例输出:

SHùiƃ@8¤ÇîÔéHŽ”c­;… äúÑ“ëIŠ1@ËQ“ï@¥Å��œ÷¥Éõ¢Š�9õ¥Éõ¤¥ 'Ö€ÄzÑF(wzFb;ÑPÈø¤d”Œòj³HÄõ4®ù¨è»›ûÆÍýãIE�.æþñ£sxÒb—�noï7·÷£�ííêi|Æþñ¦âœ�Û?xÒïoïLRâ€
íýãNÞßÞ4ÜRâ€{xѽ¿¼i1F)�»›ûÆÍýãE�noSFæõ4bŒPîoïPíýãH ooï77÷ ¸ ÙûÆ—sxÓ@æ—�»›ûÆ—-ëMÅ--ýãFæõ4QŠ�]ÍêiK6:ÒRã4Ä"ï,�'šé4]5‹od•Ÿ§é­1G*H<×saf©œ`ŽÕ¤DÉ µÅXÆÓš~=iÈÅjf0¯9 ¨àŠv21@ 2äç¸ÈÎ)øãÆ)€ÜdRàÆ)zR‚E �7-�”v P�(�r)hÆ
0œbŒu¥ÆiHç4�˜”`n¥Åw Ú3JGJZ"€G|R°Î)våhÆåÅ�>ZR2™ |Ê>”ª3=)0
£f=©/ð§'+BŒ(c�¸¸Å‌

删除了与 HTML 相关的响应代码的更新代码

                if (resourceNme.equals("/pic")) {
out.write("Server: Apache/0.8.4\r\n");
out.write("Content-type: image/jpeg\r\n");

URL url = SimpleHTTPServer.class.getResource("/office.jpg");
File file = new File(url.toURI());

if (file.exists()) {
System.out.println("file exists");
ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
long size = imageInputStream.length();
out.write("Content-Length: " + size + "\r\n");
out.write("\r\n");

BufferedImage bufferedImage = ImageIO.read(file);
boolean success = ImageIO.write(bufferedImage, "jpeg", client.getOutputStream());
System.out.println("Success: " + success); //true
out.close();
in.close();
client.close();
break;
} else {
System.out.println("don't exists");
out.close();
in.close();
client.close();
break;
}
}

最佳答案

您将直接在彼此后面发送图片的文本和二进制数据。浏览器应该如何知道如何将两者分开?如果你想结合 HTML 和图像,你应该在你的 HTML 中有一个 href=... 元素。如果只想发送图片,将内容类型设置为image/jpeg,但不能同时有两种内容类型。删除您的第一个内容类型行,删除其中包含 TITLE 的行。将您希望能够在其下调用图像的 URL 更改为 pic.jpg,它应该可以工作。

关于java - 为什么图像在通过套接字写入发送时不显示在客户端浏览器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311308/

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