简体   繁体   English

Java FTP Commons-Net错误下载文件

[英]Java FTP commons-net error downloading files

I'm using apache commons.net to access an ftp site which is the directory is in unix: 我正在使用apache commons.net来访问ftp站点,该目录位于unix中:

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

I'm looping thru a list with the names of the filenames I want to download on a specific ftp site 我正在遍历一个列表,其中包含要在特定ftp站点上下载的文件名的名称

String ftpPath = "/home/user1/input/";
FileOutputStream fos = null;
File file;

try {
    for (int i = 0; i < fileList.size(); i++) {
        file = new File(ftpPath+fileList.get(i).toString());
        OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(file));
        boolean download = ftpClient.retrieveFile("c:/test/downloadedFile.csv", outputStream1);
        outputStream1.close();
        if (download) {
           System.out.println("File downloaded successfully !");
        } else {
           System.out.println("Error in downloading file ! " + downloadFile);
        }
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

But once I try to start to download the files I get this error althougth checking the ftp site the file exists under /home/user1/input/TejasSDH_PM_AU_09_07_2014_09_00.csv -rw-r--r--: 但是,一旦我尝试开始下载文件,就一直在检查ftp站点时出现此错误,该文件位于/home/user1/input/TejasSDH_PM_AU_09_07_2014_09_00.csv -rw-r--r--下:

java.io.FileNotFoundException: \home\user1\input\TejasSDH_PM_AU_09_07_2014_09_00.csv (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at com.syntronic.client.FTPDataExtract$1.downloadTejasFiles(FTPDataExtract.java:255)
at com.syntronic.client.FTPDataExtract$1.run(FTPDataExtract.java:133)

I'm thinking as the ftp site I'm connecting, the path dir is in unix home/user1/input and java is converting all the "/" to "\\". 我在想作为我正在连接的ftp站点,路径dir在unix home / user1 / input中,而java正在将所有的“ /”转换为“ \\”。 Anyone has an idea on what the error in eclipse means or is something wrong with my code? 有人对eclipse中的错误意味着什么或我的代码有问题吗? thank you 谢谢

You seem to be switching things around. 您似乎正在改变事物。

You are opening a file outputstream to \\home\\user1\\input\\TejasSDH_PM_AU_09_07_2014_09_00.csv and you seem to be on windows so it won't work. 您正在将文件输出流打开到\\home\\user1\\input\\TejasSDH_PM_AU_09_07_2014_09_00.csv并且似乎在Windows上,因此它将无法正常工作。

You have the local path where the ftp path should go and the other way around. 您拥有ftp路径应该到达的本地路径,反之亦然。

Please read your errors more carefully, I'm willing to bet that line 255 in FTPDataExtract.java is: 请更仔细地阅读您的错误,我敢打赌,FTPDataExtract.java中的255行是:

fos = new FileOutputStream(downloadFile);

Which should alert you to the fact that it's not actually an ftp problem. 这应该使您警惕这实际上不是ftp问题。

for (int i = 0; i < fileList.size(); i++) {
    OutputStream output;
    output = new FileOutputStream("C:/test/" + fileList.get(i).toString());
    ftpClient.retrieveFile(ftpPath + fileList.get(i).toString(), output); 
    output.close();
}

I wrongfully switch the remote and local path, switching it properly will run the program smoothly. 我错误地切换了远程和本地路径,正确切换它会顺利运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM