gpt4 book ai didi

java - 通过 Socket Android/Java 接收文件时出错

转载 作者:行者123 更新时间:2023-11-30 02:42:32 26 4
gpt4 key购买 nike

我正在尝试将文件从 Android 发送到带有 java 服务器的计算机终端。我离开你写了下面的代码来发送和接收文件,并考虑收到错误。

Android(发送方-客户端):

                byte[] mybytearray = new byte[(int) selectedFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(selectedFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = socket2.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();

Java(接收服务器):

                    76)int lenghtf = Integer.parseInt(lenght);
77)byte[] mybytearray = new byte[lenghtf];
78)InputStream is = socket.getInputStream();
79) FileOutputStream fos = new FileOutputStream(namef);
80) BufferedOutputStream bos = new BufferedOutputStream(fos);
81) int bytesRead = is.read(mybytearray, 0, mybytearray.length);
82) bos.write(mybytearray, 0, bytesRead);
83)bos.close();

我收到这个错误

enter image description here

最佳答案

接收者:

FileOutputStream out1 = new FileOutputStream("File Ricevuti\\"+namef);
byte[] buf = new byte[socket.getReceiveBufferSize()];
int len = 0;
while ((len = inp.read(buf)) != -1) {
out1.write(buf, 0, len);
out1.flush();
//out1.flush();
}

out1.close();

发件人:

 InputStream in =  new FileInputStream( f );

byte[] buf = new byte[socket2.getSendBufferSize()];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);

}
in . close ();
out.close();

关于java - 通过 Socket Android/Java 接收文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567836/

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