【问题标题】:android :uploading image to server , base64encoded or multipart/form-data?android:将图像上传到服务器,base64encoded 还是 multipart/form-data?
【发布时间】:2015-06-01 05:58:24
【问题描述】:

在我的android应用中,用户可以上传300kb的图片;

我将使用This ( Android Asynchronous Http Client ) ,我认为这很棒,而且 Whatsapp 也是它的用户之一。

在这个库中,我可以使用RequestParams(我认为它是由 apache 提供的),并向其中添加文件或字符串(还有很多其他的)。

这里是:

1- 添加我的图像文件(我认为是多部分/表单数据)

   RequestParams params = new RequestParams();
   String contentType = RequestParams.APPLICATION_OCTET_STREAM;
   params.put("my_image", new File(image_file_path), contentType); // here I added my Imagefile direcyly without base64ing it.
   .
   .
   .
   client.post(url, params, responseHandler);

2- 以字符串形式发送(所以它将是 base64 编码)

   File fileName = new File(image_file_path);
   InputStream inputStream = new FileInputStream(fileName);
   byte[] bytes;
   byte[] buffer = new byte[8192];
   int bytesRead;
   ByteArrayOutputStream output = new ByteArrayOutputStream();
   try {
       while ((bytesRead = inputStream.read(buffer)) != -1) {
       output.write(buffer, 0, bytesRead);
   }
   } catch (IOException e) {
   e.printStackTrace();
   }
   bytes = output.toByteArray();
   String encoded_image = Base64.encodeToString(bytes, Base64.DEFAULT);

   // then add it to params : 

   params.add("my_image",encoded_image);

  // And the rest is the same as above

所以我的问题是:

速度更高质量哪个更好?

有什么区别?

注意:

我已经阅读了许多类似问题的答案,但没有一个人真正回答了这个问题,例如This One

【问题讨论】:

  • 你无法比较,因为第一次发送文件,第二次你从我认为是位图的图像开始。以文件开头或以位图开头。
  • 我认为图像是-A-文件,不是吗?
  • 没有。而不是在你的代码中。请在两个示例中以文件或位图开头。
  • 在您的第一个示例中,我看到了一个文件(image_file_path)。您应该在第二个示例中也使用 image_file_path,以便我们可以看到您尝试上传相同的文件/图像。
  • 谢谢。我看到您的代码没有按照您在文本中解释/询问的内容执行。在第一个示例中,您发送文件的内容。现在我希望在第二个示例中您将发送该文件的 base64 编码内容。但你没有那样做。相反,您对其内容进行各种操作,最后发送这些操作的 base64 编码结果。但是你比的是什么?

标签: android base64 image-uploading multipartform-data


【解决方案1】:

不知道 params.put() 和 params.add 是否会导致多部分编码发生变化。

base64 ended 数据的传输速度会慢 30%,因为要传输的字节数要多 30%。

我不知道你所说的质量是什么意思。上传图像的质量将与原始图像逐字节相同。

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多