gpt4 book ai didi

Java 通过套接字传输多个文件

转载 作者:行者123 更新时间:2023-12-01 18:42:52 26 4
gpt4 key购买 nike

好吧,尝试通过套接字传输指定目录的文件,从数组列表中删除目录对象,这样只剩下文件,然后通过同一个套接字将它们一一传输。这里的数组列表仅包含文件,没有目录。下面分别是客户端和服务器的接收和发送代码。除了所有数据都被写入第一个文件之外,代码运行良好,没有错误。后续文件将在服务器文件夹中创建,但它们是 0 字节。任何意见将不胜感激。

这是用于接收文件的服务器代码

public void receive(){


try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//read the number of files from the client
int number = dis.readInt();
ArrayList<File>files = new ArrayList<File>(number);
System.out.println("Number of Files to be received: " +number);
//read file names, add files to arraylist
for(int i = 0; i< number;i++){
File file = new File(dis.readUTF());
files.add(file);
}
int n = 0;
byte[]buf = new byte[4092];

//outer loop, executes one for each file
for(int i = 0; i < files.size();i++){

System.out.println("Receiving file: " + files.get(i).getName());
//create a new fileoutputstream for each new file
FileOutputStream fos = new FileOutputStream("C:\\users\\tom5\\desktop\\salestools\\" +files.get(i).getName());
//read file
while((n = dis.read(buf)) != -1){
fos.write(buf,0,n);
fos.flush();
}
fos.close();
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}


}

这是用于发送文件的客户端代码

public void send(ArrayList<File>files){

try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println(files.size());
//write the number of files to the server
dos.writeInt(files.size());
dos.flush();

//write file names
for(int i = 0 ; i < files.size();i++){
dos.writeUTF(files.get(i).getName());
dos.flush();
}

//buffer for file writing, to declare inside or outside loop?
int n = 0;
byte[]buf = new byte[4092];
//outer loop, executes one for each file
for(int i =0; i < files.size(); i++){

System.out.println(files.get(i).getName());
//create new fileinputstream for each file
FileInputStream fis = new FileInputStream(files.get(i));

//write file to dos
while((n =fis.read(buf)) != -1){
dos.write(buf,0,n);
dos.flush();

}
//should i close the dataoutputstream here and make a new one each time?
}
//or is this good?
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

最佳答案

您正在读取套接字,直到 read() 返回 -1。这是流结束条件 (EOS)。当对等方关闭连接时,就会发生 EOS。不是在它完成写入一个文件时。

您需要在每个文件之前发送文件大小。您已经对文件计数执行了类似的操作。然后确保您准确地读取了该文件的字节数:

String filename = dis.readUTF();
long fileSize = dis.readLong();
FileOutputStream fos = new FileOutputStream(filename);
while (fileSize > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1)
{
fos.write(buf,0,n);
fileSize -= n;
}
fos.close();

您可以将所有这些包含在一个循环中,该循环在 readUTF() 抛出 EOFException 时终止。相反,当然,在发送数据之前,您必须在发送方调用 writeUTF(filename)writeLong(filesize)

关于Java 通过套接字传输多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59887530/

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