【问题标题】:Android image upload AWS ServerAndroid 图片上传 AWS Server
【发布时间】:2018-01-13 20:08:55
【问题描述】:

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

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 传输工具

标签: android image amazon-web-services upload android-volley


【解决方案1】:

我在尝试将图像上传到 AWS 服务器时遇到了同样的问题。我已将它作为 octet-stream 发送。我使用了改造 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);
}

谢谢,希望对你有帮助。

【讨论】:

  • 我知道如何在 Retrofit 中执行此操作,但问题是该项目是使用 Volley 开发的
  • 我看到你正在使用 multipart。所以,它也可以在 volley 中使用其他方式。
  • 这就是问题所在,我找不到其他方法。由于整个应用程序是用 volley 编写的,所以现在不能只更改库
猜你喜欢
  • 2016-10-11
  • 2015-09-09
  • 2012-03-07
  • 2012-01-25
  • 2019-04-30
  • 2012-04-03
  • 2014-04-27
  • 2011-12-22
  • 1970-01-01
相关资源
最近更新 更多