我有一个 java 代码,我将使用它从远程机器的目录中下载文件。现在使用可用的 ftp 类来做同样的事情。当我运行导致所有文件被下载的代码时出现问题但是当我看到它们的大小时,我发现它们都是零字节并且不包含任何东西。如果你能分析一下这背后的可能原因,请提供帮助?
package login.multiple;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class Downloader {
/**
* @param args
* @throws FTPException
* @throws FTPIllegalReplyException
* @throws IOException
* @throws IllegalStateException
* @throws FTPListParseException
* @throws FTPAbortedException
* @throws FTPDataTransferException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FTPFile[] list = null;
String fileNames[];
String path = "\\Users\\XXX\\Documents\\Downloads\\Coldplay - Mylo Xyloto [mp3-vbr-2011]";
int i = 0;
FileOutputStream fos = null;
FTPClient client = new FTPClient();
//client.connect("192.168.1.2");
try {
client.connect("127.0.0.1");
client.login("xxx", "yyy");
list = client.listFiles(path);
for(i=0;i<list.length;i++){
//for(i=0;i<1;i++){
fos = new FileOutputStream(list[i].getName());
client.retrieveFile(path + list[i].getName(), fos);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}