3

我是安卓新手。我正在尝试使用 Apache Commons FTPClient 将文件从 ftp 服务器下载到 sdcard。行 InputStream input = client.retrieveFileStream("/" + fileName); 总是返回 null。但该文件在 Ftp 位置。请帮助我知道错误在哪里。

我在清单中设置了以下权限;android:name="android.permission.INTERNET" 和 android:name="android.permission.WRITE_EXTERNAL_STORAGE"

我的代码

private static void downLoad(){
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {
        client.connect("ftp.doamin.com");
        client.login("8888", "8888");
String filePath = "/mnt/sdcard/download/CheckboxHTML.txt" ;
String fileName = "CheckboxHTML.txt";
fos = new FileOutputStream(filePath);
InputStream input = client.retrieveFileStream("/" + fileName);
byte[] data = new byte[1024];
int count  = input.read(data); 
while ((count = input.read(data)) != -1) {
      fos.write(data, 0, count);
}
fos.close();
      if(!client.completePendingCommand()) { 
      client.logout(); 
      client.disconnect(); 
      System.err.println("File transfer failed."); 
} 
    } catch (SocketException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
    }

}

感谢您的时间和兴趣。阿南特。

4

3 回答 3

6

AFAIK。您假设通过调用 completePendingCommand() 并验证传输确实成功来完成文件传输。即你需要在fos.clos()下面添加调用函数。

fos.close();
client.completePendingCommand()

你也可以考虑一下,根据 FTPClient.retrieveFileStream() 的 API,该方法在无法打开数据连接时返回 null,这种情况下你应该检查回复代码(例如 getReplyCode()、getReplyString()、getReplyStrings( )) 看看为什么它失败了。

于 2013-06-05T12:20:34.043 回答
0
          File file = new File(Environment.getExternalStorageDirectory() + "/pdf");
        if(!file.exists())

           file.mkdir(); //directory is created;
            try {
                ftp = new FTPClient();
                ftp.connect("yours ftp URL",21);//don't write ftp://

                try {
                    int reply = ftp.getReplyCode();
                  if (!FTPReply.isPositiveCompletion(reply)) {
                    throw new Exception("Connect failed: " + ftp.getReplyString());
                    }
                    if (!ftp.login("username","password")) {
                      throw new Exception("Login failed: " + ftp.getReplyString());
                    }
                    try {
                        ftp.enterLocalPassiveMode();
                        if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
//                          Log.e(TAG, "Setting binary file type failed.");
                        }

                        transferFile(ftp);
                    } catch(Exception e) {
//                      handleThrowable(e);
                    } finally {
                        if (!ftp.logout()) {
//                          Log.e(TAG, "Logout failed.");
                        }
                    }
                } catch(Exception e) {
//                  handleThrowable(e);
                } finally {
                    ftp.disconnect();
                }
            } catch(Exception e) {
//              handleThrowable(e);
            }


    }


       private void transferFile(FTPClient ftp) throws Exception {
        long fileSize=0;
        fileSize = getFileSize(ftp, "nag.pdf");
        Log.v("async","fileSize"+fileSize);
        if(!(fileSize==0)){
            InputStream is = retrieveFileStream(ftp,  "nag.pdf");
            downloadFile(is,  fileSize);
            is.close();
            }
            else


                 //nosuch files

        if (!ftp.completePendingCommand()) {
            throw new Exception("Pending command failed: " + ftp.getReplyString());
        }
    }

    private InputStream retrieveFileStream(FTPClient ftp, String filePath)
    throws Exception {
        InputStream is = ftp.retrieveFileStream(filePath);
        int reply = ftp.getReplyCode();
        if (is == null
                || (!FTPReply.isPositivePreliminary(reply)
                        && !FTPReply.isPositiveCompletion(reply))) {
            throw new Exception(ftp.getReplyString());
        }
        return is;
    }

    private byte[] downloadFile(InputStream is, long fileSize)
    throws Exception {
    outputStream os = newFileOutputStream(Environment.getExternalStorageDirectory()
                + "/pdf/nag.pdf");  
        byte[] buffer = new byte[(int) fileSize];
        int readCount;
        while( (readCount = is.read(buffer)) > 0) {
            os.write(buffer, 0, readCount);
        }
        Log.i("tag", "buffer = " + buffer);
        return buffer; // <-- Here is your file's contents !!!
    }

    private long getFileSize(FTPClient ftp, String filePath) throws Exception {
        long fileSize = 0;
        FTPFile[] files = ftp.listFiles(filePath);
        if (files.length == 1 && files[0].isFile()) {
            fileSize = files[0].getSize();
        }
        Log.i("tag", "File size = " + fileSize);
        return fileSize;
    }

}
于 2012-11-09T10:03:46.057 回答
0

在解决了这个问题并花费了几个小时之后,我发现当FileSize太大时retrieveFile(), Android ApacheretrieveFileStream有时不能很好地工作。

确保将正确的TypeFile设置为BinaryFile

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);

也永远不要忘记传递 LocalPassiveMode 以获取下载命令

mFTPClient.enterLocalPassiveMode();

于 2020-03-04T12:49:35.367 回答