java使用套接字发送文件
我正在尝试使用Java将文件从一台计算机发送到另一台计算机。我已经写了下面的代码,如果发送方和接收方都在同一台计算机上启动,但如果它们在不同的机器上工作,则可以正常工作。收到的文件大小大于原始文件,并且已损坏 注意:我正在尝试传输最大10 MBs的文件 我怎样才能解决这个问题 发件人:java使用套接字发送文件,java,sockets,network-programming,Java,Sockets,Network Programming,我正在尝试使用Java将文件从一台计算机发送到另一台计算机。我已经写了下面的代码,如果发送方和接收方都在同一台计算机上启动,但如果它们在不同的机器上工作,则可以正常工作。收到的文件大小大于原始文件,并且已损坏 注意:我正在尝试传输最大10 MBs的文件 我怎样才能解决这个问题 发件人: ServerSocket server_socket = new ServerSocket(8989); File myFile = new File(myPath); Socket socket = serv
ServerSocket server_socket = new ServerSocket(8989);
File myFile = new File(myPath);
Socket socket = server_socket.accept();
int count;
byte[] buffer = new byte[1024];
OutputStream out = socket.getOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(myFile));
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
out.flush();
}
socket.close();
接收人:
Socket socket = new Socket(address, 8989);
FileOutputStream fos = new FileOutputStream(anotherPath);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int count;
InputStream in = socket.getInputStream();
while((count=in.read(buffer)) >0){
fos.write(buffer);
}
fos.close();
socket.close();
请记住,
in.read(buffer)
不一定用新数据填充整个缓冲区。因此,您应该确保不写入整个缓冲区。改变
while((count=in.read(buffer)) >0){
fos.write(buffer);
}
到
在客户端,您最多可写入个
字节并发送:
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
在服务器端,您最多读取个字节,然后将整个缓冲区写入文件
while((count=in.read(buffer)) > 0){
fos.write(buffer);
只需将其更改为:
fos.write(buffer, 0, count);
你会安全的。顺便说一句,您的程序还有一个小错误:read()
可以返回0
,这并不意味着InputStream
结束。改用=
:
count = in.read(buffer)) >= 0
你考虑过Apache Commons吗?它会将整个循环减少到:
OutputStream out = socket.getOutputStream();
InputStream in = new FileInputStream(myFile);
IOUtils.copy(in, out);
socket.close();
编写的代码越少,测试的代码越少。缓冲是在内部完成的。发送器
Socket sock = new Socket("127.0.0.1", 5991);
System.out.println("Connecting.........");
File myFile = new File("/root/qrcode/");
File[] files = myFile.listFiles();
OutputStream os = sock.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(files.length);
long totalBytesRead = 0;
int percentCompleted = 0;
for(File file : files)
{
long length = file.length();
dos.writeLong(length);
String name = file.getName();
dos.writeUTF(name);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int theByte = 0;
while((theByte = bis.read()) != -1)
{
totalBytesRead += theByte;
bos.write(theByte);
}
// System.out.println("file read");
bis.close();
}
dos.close();
//Closing socket
sock.close();
接受者
ServerSocket serverSocket = new ServerSocket(5991);
while(true) {
Socket clientSocket = null;
System.out.println("Starting...");
clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream(); //used
BufferedInputStream bis = new BufferedInputStream(in);
String dirPath ;
dirPath = "/root/NewFolder";
try{
DataInputStream dis = new DataInputStream(bis);
int filesCount = dis.readInt();
File[] files = new File[filesCount];
long f_l = 0;
int count =0 ;
long totalBytesRead = 0;
int percentCompleted = 0;
for(int i = 0; i < filesCount; i++)
{
long fileLength = dis.readLong();
String fileName = dis.readUTF();
f_l = f_l +fileLength;
files[i] = new File(dirPath + "/" + fileName);
FileOutputStream fos = new FileOutputStream(files[i]);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int tot = 0;
for(int j = 0; j < fileLength; j++) {
bos.write(bis.read());
}
bos.close();
}
}catch(Exception ex)
{
System.out.println("error in socket programming ");
}
}
ServerSocket-ServerSocket=newserversocket(5991);
虽然(正确){
套接字clientSocket=null;
System.out.println(“开始…”);
clientSocket=serverSocket.accept();
InputStream in=clientSocket.getInputStream();//已使用
BufferedInputStream bis=新的BufferedInputStream(in);
字符串路径;
dirPath=“/root/NewFolder”;
试一试{
DataInputStream dis=新的DataInputStream(bis);
int filescont=dis.readInt();
File[]files=新文件[filescont];
长f_l=0;
整数计数=0;
长totalBytesRead=0;
int percentCompleted=0;
对于(int i=0;i
@Vamshi。如果你使用的是IOUtils,那么是的,因为它实现了自己的缓冲。我使用这个函数发送和接收文件,但我不想关闭套接字,因为我必须使用套接字执行一些其他功能,但是如果我不关闭sock文件接收不完整,解决方案是什么
ServerSocket serverSocket = new ServerSocket(5991);
while(true) {
Socket clientSocket = null;
System.out.println("Starting...");
clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream(); //used
BufferedInputStream bis = new BufferedInputStream(in);
String dirPath ;
dirPath = "/root/NewFolder";
try{
DataInputStream dis = new DataInputStream(bis);
int filesCount = dis.readInt();
File[] files = new File[filesCount];
long f_l = 0;
int count =0 ;
long totalBytesRead = 0;
int percentCompleted = 0;
for(int i = 0; i < filesCount; i++)
{
long fileLength = dis.readLong();
String fileName = dis.readUTF();
f_l = f_l +fileLength;
files[i] = new File(dirPath + "/" + fileName);
FileOutputStream fos = new FileOutputStream(files[i]);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int tot = 0;
for(int j = 0; j < fileLength; j++) {
bos.write(bis.read());
}
bos.close();
}
}catch(Exception ex)
{
System.out.println("error in socket programming ");
}
}