gpt4 book ai didi

PNG 图像数据的 Javax.ImageIO.IIOException

转载 作者:可可西里 更新时间:2023-11-01 02:43:36 25 4
gpt4 key购买 nike

我正在用 java 实现一个服务器,它从 android 应用程序接收图像。基本上它是从 android 模拟器而不是从 android 设备接收图像。从 Android 模拟器或 Android 设备接收图像的所有过程都在本地主机上完成。

这是我的服务器代码。

public class Server {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
private static byte[] image;

public static void main(String[] args) throws UnknownHostException, IOException {
try {
serverSocket = new ServerSocket(4000); // Server socket

} catch (IOException e) {
System.out.println("Could not listen on port: 4000");
}
System.out.println("Server started. Listening to the port 4000");

while (true) {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
DataInputStream inputFromClient = new DataInputStream(clientSocket.getInputStream());
int count = inputFromClient.available();
System.out.println(count);
image = new byte[count];
inputFromClient.readFully(image);
final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image));
ImageIO.write(bufferedImage, "jpg", new File("image.jpg"));
}
}

异常

Exception in thread "main" javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1308)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1577)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)
at server.Server.main(Server.java:39)

Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.io.DataInputStream.readFully(DataInputStream.java:195)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1092)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1196)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1301)
... 4 more

Java Result: 1

如果从 android 设备发送图像但对于 android 模拟器它工作正常,我会得到 javax.imageiio.exception 异常。请帮助我。谢谢你。

最佳答案

您的主要问题位于这一行:

int count = inputFromClient.available();

它返回当时已经接收到的字节数。但是,服务器端可能有更多字节等待传输。因此,当您尝试加载时,传输的图像被截断/不完整。

你现在有两个选择:

  1. 在传输图像数据之前发送一个包含数据长度的整数

  2. 如果只传输一张图像,请在写入图像后关闭客户端的流。在服务器端,您现在必须阅读直到到达流的末尾。

关于PNG 图像数据的 Javax.ImageIO.IIOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29506088/

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