2017-08-06 197 views
8

使用下面的代碼上傳影像服務器 -Android的圖片上傳AWS服務器

final InputStream fileInputStream = MyApplication.getInstance().getContentResolver().openInputStream(imageFile); 
    bitmap = BitmapFactory.decodeStream(fileInputStream); 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
    final byte[] bitmapData = byteArrayOutputStream.toByteArray(); 

    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 

    // Add binary body 
    if (bitmap != null) { 
    ContentType contentType = ContentType.create("image/png"); 
    builder.addBinaryBody("", bitmapData, contentType, ""); 

    final HttpEntity httpEntity = builder.build(); 
    StringRequest request = 
     new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 

     } 
     }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
     }) { 
     @Override 
     public byte[] getBody() throws AuthFailureError { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      try { 
      httpEntity.writeTo(bos); 
      } catch (IOException e) { 
      VolleyLog.e("IOException writing to ByteArrayOutputStream"); 
      } 
      return bos.toByteArray(); 
     } 
     }; 

這是上傳圖像細膩,但文件

--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U 
Content-Disposition: form-data; name=""; filename="" 
Content-Type: image/png 
âPNG 
.... 
--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U-- 

中添加人體頭部如果我刪除從該特定內容該文件,圖像可以很容易地用作PNG。有沒有辦法只上傳PNG文件部分到服務器?

+0

嘗試使用亞馬遜sdk轉移實用程序 – g7pro

回答

2

我有同樣的問題試圖上傳圖像到AWS服務器。我已將它作爲八位字節流發送。我使用了翻新2.2。 注意:如果我們使用Octet-stream,則不需要將其作爲Multipart請求。

@PUT 
Observable<Response<Void>> uploadMedia(
     @Header("Content-Type") String contentType, @Header("filetype") 
String FileType, 
     @Url String urlPath, @Body RequestBody picture); 


private void UploadSignedPicture(String url, File filename, String mediaUrl) { 

mAmazonRestService.uploadMedia("application/octet-stream", "application/octet-stream", url, mAppUtils.requestBody(filename)). 
      subscribeOn(mNewThread). 
      observeOn(mMainThread). 
      subscribe(authenticateResponse -> { 
       if (this.isViewAttached()) { 

        if (authenticateResponse.code() == ApiConstants.SUCCESS_CODE) 

        else 

       } 
      }, throwable -> { 
       if (isViewAttached()) 
        getMvpView().showServerError(this, throwable); 
       } 
      }); 
} 

如何創建一個請求,最重要的是:

@NonNull 
public RequestBody requestBody(File filename) { 

    InputStream in = null; 
    byte[] buf = new byte[0]; 
    try { 
     in = new FileInputStream(new File(filename.getPath())); 
     buf = new byte[in.available()]; 
     while (in.read(buf) != -1) ; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return RequestBody 
      .create(MediaType.parse("application/octet-stream"), buf); 
} 

感謝希望這會幫助你。

+0

我知道如何在'Retrofit'中做到這一點,但問題是該項目是使用'Volley'開發的 –

+0

我已經看到你正在使用multipart.So,它可能是其他方式相同在凌空中也是如此。 – Saveen

+0

這就是問題,我找不到其他方法。由於整個應用程序是凌空寫的,不能只是現在改變圖書館 –