在java中使用TCP通过字节数组传输二进制文件
我正在尝试一次按字节块将二进制文件从服务器传输到客户端。然而,我遇到了一个问题,它卡在传输 8kb 上。该文件通常大于 1mb,字节数组的大小为 1024。我相信它必须对我的 while 循环执行某些操作,因为它不会关闭我的连接。有什么帮助吗?感谢
客户端
import java.io.*;
import java.net.Socket;
public class FileClient {
public static void main(String[] argv) throws IOException {
Socket sock = new Socket("localhost", 4444);
InputStream is = null;
FileOutputStream fos = null;
byte[] mybytearray = new byte[1024];
try {
is = sock.getInputStream();
fos = new FileOutputStream("myfile.pdf");
int count;
while ((count = is.read(mybytearray)) >= 0) {
fos.write(mybytearray, 0, count);
}
} finally {
fos.close();
is.close();
sock.close();
}
}
}
服务器
import java.net.*;
import java.io.*;
public class FileServer {
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(4444);
File myFile = new File("myfile.pdf");
FileInputStream fis = null;
OutputStream os = null;
while (true) {
Socket sock = servsock.accept();
try {
byte[] mybytearray = new byte[1024];
fis = new FileInputStream(myFile);
os = sock.getOutputStream();
int count;
while ((count = fis.read(mybytearray)) >= 0) {
os.write(mybytearray, 0, count);
}
os.flush();
} finally {
fis.close();
os.close();
sock.close();
System.out.println("Socket closed");
}
}
}
}
I am trying to transfer a binary file from the server to the client by blocks of bytes at a time. However, I am having a issue where it is stuck at transfering 8kb. The file is usually greater than a 1mb and the byte array is of size 1024. I believe it has to do something with my while loop since it doesnt close my connection. Any help? Thanks
Client
import java.io.*;
import java.net.Socket;
public class FileClient {
public static void main(String[] argv) throws IOException {
Socket sock = new Socket("localhost", 4444);
InputStream is = null;
FileOutputStream fos = null;
byte[] mybytearray = new byte[1024];
try {
is = sock.getInputStream();
fos = new FileOutputStream("myfile.pdf");
int count;
while ((count = is.read(mybytearray)) >= 0) {
fos.write(mybytearray, 0, count);
}
} finally {
fos.close();
is.close();
sock.close();
}
}
}
Server
import java.net.*;
import java.io.*;
public class FileServer {
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(4444);
File myFile = new File("myfile.pdf");
FileInputStream fis = null;
OutputStream os = null;
while (true) {
Socket sock = servsock.accept();
try {
byte[] mybytearray = new byte[1024];
fis = new FileInputStream(myFile);
os = sock.getOutputStream();
int count;
while ((count = fis.read(mybytearray)) >= 0) {
os.write(mybytearray, 0, count);
}
os.flush();
} finally {
fis.close();
os.close();
sock.close();
System.out.println("Socket closed");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的循环应该检查
count >= 0
而不是count >= 0
0,并且流和套接字应该在finally块中关闭。除此之外,代码对我来说看起来不错。“它卡在传输 8kb 上”是什么意思?有什么异常发生吗?
Your loops should check for
count >= 0
rather thancount > 0
, and the streams and the socket should be closed in a finally block. Other than that, the code looks fine to me.What do you mean by "it is stuck at transfering 8kb"? Any exception occurring?