2012-01-28 2 views
1

org.apache.commons.net.ftp 라이브러리를 사용하고 있습니다. 파일을 업로드했지만 FTP 서버에서 내 에뮬레이터의 가상으로 파일을 다운로드하려고합니다. SDCard가 작동하지 않았습니다. 대상 경로를 지정하는 방법은 무엇입니까? 즉, 다운로드해야하는 SD 카드 경로와 소스 파일 경로 (FTP 서버 페이지 경로의 파일)를 지정하는 방법은 무엇입니까? 여기 안드로이드 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

대부분의 Android 기기에서 /sdcard이 아닌 외부 저장소의 적절한 루트를 찾으려면 getExternalStorageDirectory() 또는 getExternalStoragePublicDirectory()Environment에 사용하십시오.

+0

이 정확한가요? 하지만 여전히 작동하지 FileOutputStream desFileStream1 = 새로운 FileOutputStream (Environment.getExternalStorageDirectory()); 부울 status1 = con.retrieveFile ("ftp://ftp.drivehq.com/Baby", desFileStream1); 서버 경로를 지정하는 방법 – VSK

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

} 
관련 문제