2014-06-25 56 views
0

下面的代碼涉及到「開始上傳文件」和「嘗試從服務器刪除文件...」,然後從未上傳或刪除文件調用「onFileCreate」或「onFileDelete」,也不會拋出任何錯誤來給出它被阻止在哪裏的任何想法。我已經等了5分鐘,因爲它做了什麼,沒有。有誰知道我可以投入的任何東西給我更多關於發生什麼事情的想法,並最終通過上傳和刪除?FTPClient(apache.commons)沒有上傳或刪除我的文件

public void onFileCreate(File file) { 

       try { 
       FTPClient ftpClient = new FTPClient(); 
       System.out.println("File Created! - Do Work!" + file); 
       ftpClient.connect(server, port); 
       ftpClient.login(user, pass); 
       ftpClient.enterLocalPassiveMode(); 

       ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

       InputStream inputStream = new FileInputStream(file); 

       String filename = file.getPath(); 
       System.out.println("Start uploading file"); 
       boolean done = ftpClient.storeFile(filename, inputStream); 
       inputStream.close(); 
       if (done) { 
        System.out.println("The file was uploaded successfully: " + file); 
       } 
        ftpClient.isConnected(); 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } catch(Exception e){ 
        e.printStackTrace(System.err); 
        } 


      } 


      @Override 
      public void onFileDelete(File file) { 
       try { 
        System.out.println("File removed: Do Work!"); 

        FTPClient ftpClient = new FTPClient(); 

        ftpClient.connect(server, port); 
        ftpClient.login(user, pass); 
        ftpClient.enterLocalPassiveMode(); 

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
        String filename = file.getPath(); 
        System.out.println("Trying to delete file from server..."); 
        boolean done = ftpClient.deleteFile(filename); 
        if(done){ 
        ftpClient.isConnected(); 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
        System.out.println("File deleted from server: " + file); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(System.err); 
       } 
      } 

下面是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.testapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="19" 
     android:targetSdkVersion="19" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.testapp.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

回答

0

我是個白癡....我用的是完整的文件路徑(而不是文件名)爲storeFile命令。

代替:

String filename = file.getPath(); 

它必須是:

String filename = file.getName(); 

第一個字符串命令是在遠程位置如此明顯的本地文件路徑是行不通的。