Java 通过套接字发送文件
Java Send file via Socket
我正在写一个 class 用于在 Java 中通过套接字进行双向发送文件
这里是on GitHub。
一切正常,直到文件接收完成。
很快:
- in client.java 是 C:\Maven\README.txt
的硬编码方式
- 首先我发送文件名
- 然后我发送文件长度
- 在第三步,我将文件从 FileInputStream 发送到 DataOutputStream
在客户端:
byte[] bytes = new byte[(int)forSend.length()];
InputStream fin = new FileInputStream(forSend);
int count;
while ((count = fin.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
fin.close();
fout = new FileOutputStream(filename);
byte[] bytes = new byte[length];
System.out.println("receiving file...");
int count;
while ((count = in.read(bytes)) > 0) {
fout.write(bytes, 0, count);
}
fout.flush();
fout.close();
- 服务器上的文件已完全接收(相同的长度和内容)
当我尝试添加用于向套接字写入内容的代码时,启动后服务器和客户端正在等待某些内容(我不知道是什么)
以前我遇到过这种情况,当丢失一个 DataInputStream 读取时(消息从服务器发送但客户端没有接收此消息)。但目前我正在尝试添加文件传输完成后更改的标志,稍后检查它的状态。它在服务器和客户端上都可以工作,但是添加 read/write from/to Socket return 我回到服务器和客户端都在等待某些东西的情况。
现在怎么了?
我的朋友Denr01帮助了我,所以我的错误是文件长度的控制,我的问题中没有任何地方。因此,我的 "finishing" 确认被写入文件。
解决问题的方法在发件人:
int read = 0;
int block = 8192;
int count = 0;
byte[] bytes = new byte[block];
while (read != forSend.length()) {
count = fin.read(bytes, 0, block);
out.writeInt(count);
out.write(bytes, 0, count);
read += count;
System.out.println("already sent " + read + " bytes of " + forSend.length());
}
- 发件人读取字节数并写入字节数
- 它向接收方发送计数,因此接收方将知道在当前循环迭代中要接收多少字节
- 然后发送方发送字节块并递增读取的字节计数器
- 当计数器不等于文件长度时重复此操作
发件人:
int block = 8192;
int count = 0;
int read = 0;
byte[] bytes = new byte[block];
System.out.println("recieving file...");
while (read != length) {
block=in.readInt();
in.readFully(bytes, 0, block);
fout.write(bytes, 0, block);
read += block;
System.out.println("already recieved " + read + " bytes of " + length);
}
- 创建长度等于发送方块长度的字节数组
- 在每次迭代中,首先读取下一个块长度,然后读取这个字节数
- 增加接收者的计数器
- 当计数器不等于之前收到的文件长度时重复此操作
在这种情况下,我们可以控制每个文件读取迭代,并且始终知道要接收多少字节,因此当接收到的所有字节都相同时,下一个 "messages" 将不会写入文件。
我正在写一个 class 用于在 Java 中通过套接字进行双向发送文件 这里是on GitHub。 一切正常,直到文件接收完成。 很快:
- in client.java 是 C:\Maven\README.txt 的硬编码方式
- 首先我发送文件名
- 然后我发送文件长度
- 在第三步,我将文件从 FileInputStream 发送到 DataOutputStream
在客户端:
byte[] bytes = new byte[(int)forSend.length()];
InputStream fin = new FileInputStream(forSend);
int count;
while ((count = fin.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
fin.close();
fout = new FileOutputStream(filename);
byte[] bytes = new byte[length];
System.out.println("receiving file...");
int count;
while ((count = in.read(bytes)) > 0) {
fout.write(bytes, 0, count);
}
fout.flush();
fout.close();
- 服务器上的文件已完全接收(相同的长度和内容)
当我尝试添加用于向套接字写入内容的代码时,启动后服务器和客户端正在等待某些内容(我不知道是什么)
以前我遇到过这种情况,当丢失一个 DataInputStream 读取时(消息从服务器发送但客户端没有接收此消息)。但目前我正在尝试添加文件传输完成后更改的标志,稍后检查它的状态。它在服务器和客户端上都可以工作,但是添加 read/write from/to Socket return 我回到服务器和客户端都在等待某些东西的情况。
现在怎么了?
我的朋友Denr01帮助了我,所以我的错误是文件长度的控制,我的问题中没有任何地方。因此,我的 "finishing" 确认被写入文件。 解决问题的方法在发件人:
int read = 0;
int block = 8192;
int count = 0;
byte[] bytes = new byte[block];
while (read != forSend.length()) {
count = fin.read(bytes, 0, block);
out.writeInt(count);
out.write(bytes, 0, count);
read += count;
System.out.println("already sent " + read + " bytes of " + forSend.length());
}
- 发件人读取字节数并写入字节数
- 它向接收方发送计数,因此接收方将知道在当前循环迭代中要接收多少字节
- 然后发送方发送字节块并递增读取的字节计数器
- 当计数器不等于文件长度时重复此操作
发件人:
int block = 8192;
int count = 0;
int read = 0;
byte[] bytes = new byte[block];
System.out.println("recieving file...");
while (read != length) {
block=in.readInt();
in.readFully(bytes, 0, block);
fout.write(bytes, 0, block);
read += block;
System.out.println("already recieved " + read + " bytes of " + length);
}
- 创建长度等于发送方块长度的字节数组
- 在每次迭代中,首先读取下一个块长度,然后读取这个字节数
- 增加接收者的计数器
- 当计数器不等于之前收到的文件长度时重复此操作
在这种情况下,我们可以控制每个文件读取迭代,并且始终知道要接收多少字节,因此当接收到的所有字节都相同时,下一个 "messages" 将不会写入文件。