2017-03-09 73 views
1

我試圖做一個文件和im小堵塞,我可以通過捲曲來做到這一點,但我得到一個500 http錯誤代碼,但如果我更改文件的文件名參數我得到一個400 ,所以看起來這個帖子是很好的。終點是一個WordPress的休息API,即時通訊使用Android模擬器Nexus 5 API 22.所以我不知道這是否是正確的,或者如果有另一種方式來做我想做的事情。HTTP POST文件與HttpURLConnection

try { 
     FileInputStream fileInputStream = new FileInputStream(sourceFile); 

     URL url = new URL("http://server/v2/media"); 

     urlConnection = (HttpURLConnection) url.openConnection(); 

     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.setUseCaches(false); 
     urlConnection.setChunkedStreamingMode(0); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("Authorization", "Basic " + encoding); 
     urlConnection.setRequestProperty("Connection", "Keep-Alive"); 
     urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 

     DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; filename=\"" + imagePath + "\"" + lineEnd); 
     dos.writeBytes("Content-Type: image/jpeg" + lineEnd); 
     dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); 
     dos.writeBytes(lineEnd); 


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

     Log.d(CameraAPI.TAG, "Bytes Available:" + bytesAvailable); 
     Log.d(CameraAPI.TAG, "Buffer Size:" + bufferSize); 

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

     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"post\"" + lineEnd); 
     dos.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd); 
     dos.writeBytes("Content-Length: " + id.length() + lineEnd); 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(id); 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"title\"" + lineEnd); 
     dos.writeBytes("Content-Type: text/plain;charset=UTF-8"+lineEnd); 
     dos.writeBytes("Content-Length: " + title.length() + lineEnd); 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(title); 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 


     // Execute HTTP Post Request 
     Log.d(CameraAPI.TAG, "ResposeCode:" + urlConnection.getResponseCode()); 
     Log.d(CameraAPI.TAG, "Content-Length:" + urlConnection.getContentLength()); 
     Log.d(CameraAPI.TAG, "Content-Type:" + urlConnection.getContentType()); 
     Log.d(CameraAPI.TAG, "ResponseMessage:" + urlConnection.getResponseMessage()); 

     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 

     if (urlConnection.getResponseCode() == 201) { 
      InputStream bis = new BufferedInputStream(urlConnection.getInputStream()); 
      String result = convertStreamToString(bis); 
      Log.d(CameraAPI.TAG, "DataInputStream:" + result); 
      bis.close(); 
      return true; 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return Boolean.valueOf(e.getMessage()); 
    } finally { 
     urlConnection.disconnect(); 
    } 

回答

0

最後我得到了答案,我用捲曲查看請求必須如何做..我改變了的multipart/form-data的過程如下:

try { 
      FileInputStream fileInputStream = new FileInputStream(sourceFile); 

      URL url = new URL("http://server/wp/v2/media"); 

      urlConnection = (HttpURLConnection) url.openConnection(); 

      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.setUseCaches(false); 
      urlConnection.setChunkedStreamingMode(0); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setRequestProperty("Authorization", "Basic " + encoding); 
      urlConnection.setRequestProperty("Connection", "Keep-Alive"); 
      urlConnection.setRequestProperty("Content-Type", this.getMimeType(imagePath)); 
      urlConnection.setRequestProperty("Content-Disposition", "attachment;filename=\"" + sourceFile.getName() + "\";post=" + id + ";title=\"" + title + "\"" + lineEnd); 

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

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

      Log.d(CameraAPI.TAG, "Bytes Available:" + bytesAvailable); 
      Log.d(CameraAPI.TAG, "Buffer Size:" + bufferSize); 

      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      Log.d(CameraAPI.TAG, "Bytes to read:" + bytesRead); 

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


      // Execute HTTP Post Request 
      Log.d(CameraAPI.TAG, "ResposeCode:" + urlConnection.getResponseCode()); 
      Log.d(CameraAPI.TAG, "Content-Length:" + urlConnection.getContentLength()); 
      Log.d(CameraAPI.TAG, "Content-Type:" + urlConnection.getContentType()); 
      Log.d(CameraAPI.TAG, "ResponseMessage:" + urlConnection.getResponseMessage()); 

      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 

      if (urlConnection.getResponseCode() == 201) { 
       InputStream bis = new BufferedInputStream(urlConnection.getInputStream()); 
       String result = convertStreamToString(bis); 
       Log.d(CameraAPI.TAG, "DataInputStream:" + result); 
       bis.close(); 
       return true; 
      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return Boolean.valueOf(e.getMessage()); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     }