2012-01-28 204 views
1

我正在使用org.apache.commons.net.ftp庫,我上傳了該文件,但是當我嘗試從FTP服務器下載文件到我的模擬器的虛擬它沒有工作的SDCard。 如何指定目標路徑?那是我需要下載的SD卡路徑,以及如何指定源文件路徑(FTP服務器頁面路徑中的文件)?Android FTP客戶端應用程序 - 如何從FTP服務器下載文件

這裏是我試圖執行下載的代碼,

try 
{ 
    FileOutputStream desFileStream1 = new FileOutputStream("/sdcard/Baby.jpg"); 
    Boolean status1 = con.retrieveFile("/Baby", desFileStream1); 
    if(status1) 
    { 
     lblResult2.setText("File downloaded Successfully"); 
    } 
    else 
    { 
     lblResult2.setText("File download failed"); 
    } 
    DesFileStream1.close(); 
} catch (Exception e) 
    { 
    Log.d(TAG, "download failed"); 
    } 

你們中的任何一個幫助我。

回答

0

Environment上使用getExternalStorageDirectory()getExternalStoragePublicDirectory()可以在大多數Android設備上找到正確的外部存儲根目錄,而不是/sdcard

+0

這是正確的嗎?但仍然無效FileOutputStream desFileStream1 = new FileOutputStream(Environment.getExternalStorageDirectory());布爾status1 = con.retrieveFile(「ftp://ftp.drivehq.com/Baby」,desFileStream1);如何指定服務器路徑 – VSK 2012-01-28 16:47:22

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; 
    } 

} 
相關問題