【问题标题】:How to upload image using presignedurl from aws S3 server in android如何在android中使用来自aws S3服务器的presignedurl上传图像
【发布时间】:2015-07-25 10:41:28
【问题描述】:

我正在尝试从预签名的 url 将图像上传到 S3 存储桶,它给出了 ssl 异常错误连接被对等方关闭

这是我的代码

public int upload(String filePath, URL url) {
 Bitmap bm = BitmapFactory.decodeFile(filePath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            byte[] fileBytes = bos.toByteArray();

            connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/octet-stream"); 

            OutputStream output = connection.getOutputStream();
            InputStream input = new ByteArrayInputStream(fileBytes);
            byte[] buffer = new byte[4096];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();

            return connection.getResponseCode();
}

【问题讨论】:

    标签: android amazon-s3 httpurlconnection


    【解决方案1】:

    终于想通了 这是使用预签名 URL 将图像发送到 S3 的代码

    try {
    
                Bitmap bm = BitmapFactory.decodeFile(fileBytes);
                connection = (HttpsURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestMethod("PUT");
                connection.setRequestProperty("Content-Type", "application/octet-stream"); // Very important ! It won't work without adding this!
    
                OutputStream output = connection.getOutputStream();
    
    
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 50, output);
                output.flush();
    
                int response = connection.getResponseCode();
    
                return connection.getResponseCode();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2019-11-14
      • 2012-12-06
      • 2019-08-10
      • 2018-02-23
      相关资源
      最近更新 更多