2016-07-20 130 views
0

我知道如何使用ftp4j但是我想用同样的方法下载从FTP文件上传文件,我希望看到:下载文件从FTP使用ftp4j

  1. 服务器目录(httpdocs资料/文件夹/到/下载/从/ file.extension)
  2. 本地目录(/sdcard/folder/to/download/to/file.extension)

下面是我用上传的代码:

file_destination = new File(filDir +"/"+fileName); // where fileDir + filename = sdcard/document/file.txt 

private void uploadImage() { 
    // TODO Auto-generated method stub 

    file_destination_string = file_destination.toString(); 
    upload_file = new File(file_destination_string); 
    uploadFile(upload_file); 

} 
    private void uploadFile(File f) { 
    // TODO Auto-generated method stub 
     client = new FTPClient(); 
     try { 
      client.connect(FTP_HOST,21);   //where HOST is ip address of server 
      client.login(FTP_USER, FTP_PASS);   // FTP user/password 
      client.setType(FTPClient.TYPE_BINARY); 
      client.changeDirectory(FTP_DIR);   //where FTP_DIR = /httpdocs/folder/ 
      client.upload(f, new MyTransferListener());  

     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), "FTP Failed: "+e, Toast.LENGTH_SHORT).show(); 
      System.out.println("e1..."+e); 
      e.printStackTrace(); 
      try { 
       client.disconnect(true); 

       Log.e("MYAPP", "exception", e); 
       System.out.println("e2..."); 
      } catch (Exception e2) { 
       System.out.println("e3..."); 
       e2.printStackTrace(); 
      } 
     } 
    } 
    public class MyTransferListener implements FTPDataTransferListener { 

     public void started() { 
      Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show(); 
     } 

     public void transferred(int length) { 
      Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show(); 
     } 

     public void completed() { 
      Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show(); 
     } 

     public void aborted() { 
      Toast.makeText(getBaseContext()," transfer aborted, please try again...", Toast.LENGTH_SHORT).show(); 
     } 

     public void failed() { 
      Toast.makeText(getBaseContext()," failed..", Toast.LENGTH_SHORT).show(); 
     } 
} 

如何下载文件?

+1

你可以使用'client.download(“myfile.txt的”,新的Java。 io.File(“d:/myfile.txt”));'这会将“myfile.txt”下载到“D:\ myfile.txt”。 – Bobby

+0

我试图上传和下载到安卓设备上,而不是PC – TwoStarII

+0

容易,只需用“/sdcard/myfile.txt”替换“D:\ myfile.txt” – Bobby

回答

0

有可用的下载方法。

您可以使用client.download("myfile.txt", new java.io.File("d:/myfile.txt"));这会将“myfile.txt”下载到“D:\ myfile.txt”。

对于android来说,整个事情有点不同。 如果你想存储在一个定义文件夹(例如在SD卡),你可以使用它像这样:

client.download("myfile.txt", new java.io.File("/sdcard/myfile.txt"));

如果你想做到这一点下载到应用程序的默认数据目录,你可以使用方法context.getExternalFilesDir()。如果您通过null作为参数,则会返回外部存储上应用程序专用目录的根目录。

点击此处了解详情:https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

另外,在这里引用AndroidDevelopers:

KITKAT开始,没有权限才能读取或写入 返回的路径;它始终可供调用应用程序访问。此 仅适用于为调用 应用程序的包名称生成的路径。要访问属于其他包的路径,需要 WRITE_EXTERNAL_STORAGE和/或READ_EXTERNAL_STORAGE

所以你不需要这两个权限来访问你自己的应用程序文件。

最后,该代码应该下载一个文件,只需在您的应用程序默认目录创建一个新的文件:

client.download("myfile.txt", new java.io.File(context.getExternalFilesDir(null), "myfile.txt"));