2017-03-09 7 views
1

ポストファイルを作成しようとしていますが、少しブロックされていますが、私はカールすることができますが、このコードでは500 HTTPエラーコードが表示されますが、 、それはポストがよく形成されているようです。エンドポイントは、ワードプレスの残りのAPIです、そして、私はアンドロイドエミュレータのNexus 5 API 22を使用しています。だから私はこれが正しいか、もし私がしたいことをするための別の方法があるか分かりません。HttpURLConnectionを使用したHTTPポストファイル

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

最終的に私は答えを得た、私は要求がなされるために持っているか確認するために、カール使用...と私は、このためのマルチパート/フォームのデータ処理を変更:

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(); 
      } 
     } 
関連する問題