【发布时间】:2021-07-09 03:40:13
【问题描述】:
我正在尝试通过套接字发送大约 2.3 GB 的大文件。
所以,当我创建字节数组new byte[fileLength] 时,我会收到内存不足的错误消息。
我的代码是
fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
int fileLength = (int) file.length();
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
mybytearray = new byte[fileLength];
dis.readFully(mybytearray, 0, mybytearray.length);
dos.writeUTF(file.getName());
dos.writeLong(mybytearray.length);
int i = 0;
while (i<100) {
dos.write(mybytearray, i*(mybytearray.length/100), mybytearray.length/100);
final int c=i;
Log.e("TAG", "REQ HANDLER: Completed: \"+c+\"%\"" );
i++;
}
dos.flush();
如何通过socket发送和接收这种大文件
【问题讨论】: