2017-08-06 132 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

这就是问题,我找不到其他方法。由于整个应用程序是凌空写的,不能只是现在改变图书馆 –