Java 是否可以在没有ImageIO的情况下读取图像?

Java 是否可以在没有ImageIO的情况下读取图像?,java,sockets,Java,Sockets,我正在尝试读取图像并通过Java套接字传递它。但也有一些不合适的地方。在diff工具中查看时,我意识到所有大于127的数字都被截断了 所以我想把它转换成一个char[]数组,然后返回它。现在我得到了一个完全不同的图像,可能是由于字符的大小 try (PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true); BufferedInputStream in = new

我正在尝试读取图像并通过Java套接字传递它。但也有一些不合适的地方。在diff工具中查看时,我意识到所有大于127的数字都被截断了

所以我想把它转换成一个char[]数组,然后返回它。现在我得到了一个完全不同的图像,可能是由于字符的大小

        try (PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename), BUFSIZ)) {
            byte[] buffer = new byte[BUFSIZ];
            while (in.read(buffer) != -1) {
                response.append(new String(buffer));
                out.print(response.toString());
                response.setLength(0);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
这是我的阅读和交付代码

我已经阅读了很多次使用ImageIO,但我想不使用它,因为我不知道它是否是图像。(其他文件类型如可执行文件呢?)


那么,有没有办法将其转换为无符号字节之类的内容,以便在客户端上正确传递?我必须使用与read()不同的东西才能实现这一点吗?

编写器用于字符数据。使用
OutputStream.
通常会犯一个错误,即假定
read()
填充了缓冲区

下面的循环将正确复制任何内容。记住它

int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

写入程序
用于字符数据。使用
OutputStream.
通常会犯一个错误,即假定
read()
填充了缓冲区

下面的循环将正确复制任何内容。记住它

int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

跟我重复:字符不是字节,也不是代码点

跟我重复:写入程序不是输出流

    try (OutputStream out = this.socket.getOutputStream();
         BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename), BUFSIZ)) {
        byte[] buffer = new byte[BUFSIZ];
        int len;
        while ((len = in.read(buffer))) != -1) {
            out.write(buffer, 0, len);
        }
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }

(这是从内存中获取的,请检查参数中的write())。

跟着我重复:字符
不是字节
也不是代码点

跟我重复:写入程序不是输出流

    try (OutputStream out = this.socket.getOutputStream();
         BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename), BUFSIZ)) {
        byte[] buffer = new byte[BUFSIZ];
        int len;
        while ((len = in.read(buffer))) != -1) {
            out.write(buffer, 0, len);
        }
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }

(这是内存中的数据,请检查参数中的write())。

二进制数据不是
字符串。
。只要保留字节,字节就可以。一旦将它们转换为字符(创建
字符串
时必须这样做),就必须面对字节被签名的问题。二进制数据不是
字符串
。只要它们保留为字节,字节就可以。一旦您将它们转换为字符(正如您创建
字符串时所必须的那样),您就必须面对字节被签名的问题。字符不是字节,也不是代码点。谢谢你的指点!字符不是字节,也不是代码点。谢谢你的指点!非常感谢。我将其附加到respone,因为它在第一次迭代时存储http头。在我猜之前把它寄出去应该没问题。谢谢。我将其附加到respone,因为它在第一次迭代时存储http头。在我猜之前把它寄出去应该没问题。