gpt4 book ai didi

android - 如何使用来自 aws S3 服务器的 presignedurl 在 android 中上传图像

转载 作者:行者123 更新时间:2023-11-29 00:08:58 24 4
gpt4 key购买 nike

我正在尝试从预签名的 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();
}

最佳答案

终于明白了这是使用预签名 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();
}

关于android - 如何使用来自 aws S3 服务器的 presignedurl 在 android 中上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com