安卓图片上传AWS服务器

8

使用以下代码将图像上传到服务器 -

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文件部分到服务器?


尝试使用亚马逊SDK传输实用程序。 - g7pro
1个回答

3

我有同样的问题,尝试将图片上传到AWS服务器。我已经将其作为八位字节流发送。我使用了Retrofit 2.2。注意:如果我们使用八位字节流,则无需将其作为多部分请求。

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

感谢您,希望这能帮助到您。


我知道如何使用Retrofit解决这个问题,但问题在于项目是使用Volley开发的。 - Rohan Kandwal
我看到你在使用multipart。所以,在volley中也可以用同样的方式实现。 - Saveen
那就是问题所在,我找不到其他的方法。由于整个应用程序都是使用Volley编写的,现在无法更改库。 - Rohan Kandwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接