gpt4 book ai didi

java - 从java服务器发送整数或字符串

转载 作者:行者123 更新时间:2023-12-02 08:12:38 25 4
gpt4 key购买 nike

我有一个java服务器,它可以发送任何类型的文件,但我无法发送字符串或整数。

以下是如何发送文件的示例。

File f = new File("/Users/Large/Downloads/android.jpg");
FileInputStream fis = null;
int size = (int)f.length();
byte[] bytes = new byte[size];
fis = new FileInputStream( f );
fis.read( bytes );

System.out.println("entro...");
out.write( bytes );
out.flush();

最佳答案

尝试用这些使用 String 的示例替换对 out.write(bytes) 的调用和 ByteBuffer类:

// A string ("Hello, World").
out.write("Hello, World".getBytes());

// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());

请注意,在某些时候您需要关心 endiannesscharacter encoding让另一端的某些东西以可靠的方式读取这些值。

关于java - 从java服务器发送整数或字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126792/

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