2016-08-20 70 views
0

我想讓用戶發送一些應用程序相關的文件給我。我在我的下拉框中爲此做了一個「文件請求」文件夾。每當我收到這條消息Error 405 - Method not allowed。這裏是我的代碼:通過Android上傳文件到Dropbox文件夾

private class UploadFile extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 
      String sourceFileUri = "/data/com.mostafa.android.roadbump/databases/matab.db"; 
      HttpURLConnection conn = null; 
      DataOutputStream dos = null; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1 * 1024 * 1024; 

      if (dB.isFile()) { 

       try { 
        String upLoadServerUri = "https://www.dropbox.com/request/KJcdVMDyxHvM2So1mJkK"; 

        // open a URL connection to the Server 
        FileInputStream fileInputStream = new FileInputStream(dB); 
        URL url = new URL(upLoadServerUri); 

        // Open a HTTP connection to the URL 
        conn = (HttpURLConnection) url.openConnection(); 
        conn.setDoInput(true); // Allow Inputs 
        conn.setDoOutput(true); // Allow Outputs 
        conn.setUseCaches(false); // Don't use a Cached Copy 
        conn.setRequestMethod("PUT"); 
        conn.setRequestProperty("Connection", "Keep-Alive"); 
        conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
        conn.setRequestProperty("bill", sourceFileUri); 

        dos = new DataOutputStream(conn.getOutputStream()); 

        dos.writeBytes(twoHyphens + boundary + lineEnd); 
        dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\"" 
          + sourceFileUri + "\"" + lineEnd); 

        dos.writeBytes(lineEnd); 

        // create a buffer of maximum size 
        bytesAvailable = fileInputStream.available(); 

        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 

        // read file and write it into form... 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        while (bytesRead > 0) { 

         dos.write(buffer, 0, bufferSize); 
         bytesAvailable = fileInputStream.available(); 
         bufferSize = Math 
           .min(bytesAvailable, maxBufferSize); 
         bytesRead = fileInputStream.read(buffer, 0, 
           bufferSize); 

        } 

        // send multipart form data necesssary after file 
        // data... 
        dos.writeBytes(lineEnd); 
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

        Log.d("Sasaaa", "Done"); 

        int responseCode = conn.getResponseCode(); 
        String responseMessage = conn.getResponseMessage(); 

        Log.d("Sasaaa", String.valueOf(responseCode)); 
        Log.d("Sasaaa", responseMessage); 

        // close the streams // 
        conn.disconnect(); 
        fileInputStream.close(); 
        dos.flush(); 
        dos.close(); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 
} 
+0

Dropbox不提供用於上傳文件請求的編程接口。我們會將其視爲功能請求。程序上傳應通過API完成,如下面的答案中所述。 – Greg

回答

0

Error 405 - Method not allowed告訴我你的請求類型不正確。你正試圖在你的服務器上執行一個PUT請求。請再次檢查,服務器請求類型可能是POST。

您需要檢查的另一件事是url或您調用服務器方法的路徑。錯誤的網址可能會導致此類錯誤。

在調用方法之前,您需要檢查是否存在任何類型的身份驗證過程。

編輯

的Dropbox有自己的API,將文件上傳到Dropbox的服務器。您可以檢查他們是否輕鬆完成這項工作。

我在引用另一個answer from here爲您輕鬆訪問。

private DropboxAPI<AndroidAuthSession> mDBApi; 

File tmpFile = new File(fullPath, "/data/com.mostafa.android.roadbump/databases/matab.db); 

FileInputStream fis = new FileInputStream(tmpFile); 

try { 
    DropboxAPI.Entry newEntry = mDBApi.putFileOverwrite("matab.db", fis, tmpFile.length(), null); 
} catch (DropboxUnlinkedException e) { 
    Log.e("DbExampleLog", "User has unlinked."); 
} catch (DropboxException e) { 
    Log.e("DbExampleLog", "Something went wrong while uploading."); 
} 

您可能需要look at here以瞭解API密鑰和APP SECRET。

+0

謝謝你的迴應:) ..我用POST較早,它給了我相同的迴應,所以我嘗試PUT作爲替代..該網址是正確的,我仔細檢查與DROPBOX確保..我不瞭解身份驗證過程,所以如果任何人都可以啓發我們,我真的會很感激。 –

+0

請參閱編輯答案。 –

+0

我知道DropBox API,但我正在尋找一種更簡單的方法來完成這項任務,特別是他們已經打開'文件請求'。 –