2016-08-20 99 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,但我正在寻找一种更简单的方法来完成这项任务,特别是他们已经打开'文件请求'。 –