5

/* 我在 localhost 上运行 FTP 服务器。当我下载文件时使用 ftpClient.retrieveFile() 方法,它的 replyCode 是 550 。我阅读了 commons-net 的 API 并找到了 550 回复代码,定义为“public static final int FILE_UNAVAILABLE 550”。但我无法从我的代码中找到问题。
谢谢你的帮助。

*/

    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;

    try {
        ftpClient.connect("192.168.1.102",2121);
        ftpClient.login("myusername", "12345678");
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        String remoteFileName = "ftpserver.zip";//this file in the rootdir
        fos = new FileOutputStream("f:/down.zip");
        ftpClient.setBufferSize(1024);
        ftpClient.enterLocalPassiveMode();
        ftpClient.enterLocalActiveMode();
        ftpClient.retrieveFile(remoteFileName, fos);  
        System.out.println("retrieveFile?"+ftpClient.getReplyCode());
        fos.close();
        ftpClient.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("关闭FTP异常", e);
        }
    }
4

6 回答 6

11

我发现 Apache retrieveFile(...) 有时不能处理超过一定限制的文件大小。为了克服这个问题,我会改用retrieveFileStream()。在下载之前,我已经设置了正确的文件类型并将模式设置为 PassiveMode

所以代码看起来像

    ....
    ftpClientConnection.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClientConnection.enterLocalPassiveMode();
    ftpClientConnection.setAutodetectUTF8(true);

    //Create an InputStream to the File Data and use FileOutputStream to write it
    InputStream inputStream = ftpClientConnection.retrieveFileStream(ftpFile.getName());
    FileOutputStream fileOutputStream = new FileOutputStream(directoryName + "/" + ftpFile.getName());
    //Using org.apache.commons.io.IOUtils
    IOUtils.copy(inputStream, fileOutputStream);
    fileOutputStream.flush();
    IOUtils.closeQuietly(fileOutputStream);
    IOUtils.closeQuietly(inputStream);
    boolean commandOK = ftpClientConnection.completePendingCommand();
    ....
于 2013-05-15T12:53:33.770 回答
2

FTP 错误 550 未采取请求的操作。文件不可用,未找到,不可访问

所以我认为编码有点奇怪,我没有设置控制编码并使用retrieveFile只是在java中发送一个普通的字符串。还有这一行:

ftpClient.retrieveFile(new String(remoteFileName.getBytes("ms932"),"ISO-8859-1"), fos);

什么都不做,因为您正在从另一个字符串创建一个新的 Java 字符串。Java 字符串以不同的编码保存在内存中,如果我没记错的话,它与所有编码兼容。

另外,您使用的路径是错误的,请参阅:

String remoteFileName = "//ftpserver.zip";

Ftp 会导致以 / 开头的路径出错,试试这个:

"ftpserver.zip"

或者如果你有一个子目录,试试这个:

"subdir/myfile.zip"

干杯

于 2011-09-26T18:31:34.857 回答
1

最近我遇到了同样的错误,但这主要是因为路径不正确,而不是像 /data/csms/trt/file.txt 那样附加斜杠,而是附加为 /data/csms/trtfile.txt ...因此没有从所需位置检索文件。

于 2012-04-16T10:04:08.037 回答
0

setControlEncoding 需要在连接之前调用,像这样

[...]
try {
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect("192.168.1.102",2121);
    ftpClient.login("myusername", "12345678");
[...]
于 2013-02-08T16:46:11.240 回答
0

看起来输出路径不正确。检查服务器上的共享根目录。如果根目录是 f:\ 并且你的文件在这个根目录下,那么你只需要这样做:`fos = new FileOutputStream("down.zip");

如果您的文件位于根目录的子目录中,例如 f:\sub,那么它应该是fos = new FileOutputStream("sub\\down.zip");

于 2013-11-04T22:46:00.373 回答
0

我遇到了同样的问题,因为我的 SDCARD/PHONE 内存中没有空间。

于 2016-01-24T06:54:49.147 回答