2016-08-20 2 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 또는 서버 메소드를 호출하는 경로입니다. 잘못된 URL로 인해 이러한 유형의 오류가 발생할 수 있습니다.

마지막으로 메서드를 호출하기 전에 인증 프로세스가 있는지 확인해야합니다.

편집

드롭 박스는 보관 서버에 파일을 업로드하는 자신의 API가 있습니다. 이 일을 쉽게 끝낼 수 있는지 확인할 수 있습니다.

쉽게 액세스 할 수 있도록 다른 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."); 
} 

당신은 API 키 및 APP 비밀의 아이디어를 얻을 수있는 look at here이 걸릴 수 있습니다.

+0

답변 해 주셔서 감사합니다. :) .. 이전에 POST를 사용했고 나에게도 동일한 응답을주었습니다. 대체물로 PUT을 시도했습니다. URL은 정확하고 DROPBOX로 확인했습니다. 인증 프로세스에 대해 알고 있다면, 누군가가 우리에게 계몽 할 수 있다면 정말 감사 할 것입니다. –

+0

수정 된 답변을 참조하십시오. –

+0

나는 DropBox API에 대해 알고 있지만, 나는 그들이 'File Requests'를 이미 열었다는 특별한 작업을 쉽게 할 수있는 방법을 찾고 있었다. –

관련 문제