通过 POST 将位图发送到服务器 (B2) 不起作用 OKhttpClient HttpURLConnection
Sending Bitmap to Server (B2) via POST not working OKhttpClient HttpURLConnection
我正在使用 Backblaze 并设法将文件(图像和视频)上传到我的 iOS 应用程序 (swift 5) 中的存储桶,但我无法在我的 [=35] 中这样做=] android 应用
我收到正确的授权令牌,下载 url,上传 url 和 API Url。
但是当我尝试 post 时 url 它不起作用并且我收到以下错误
W/OkHttpClient: A connection to https://googleads.g.doubleclick.net/ was leaked. Did you forget to close a response body?
D/yyyy enclosed: saveImgToStorage: buffer(com.android.okhttp.internal.http.Http1xStream$FixedLengthSource@e5232ab).inputStream()
saveImgToStorage: 401
W/System.err: java.net.ProtocolException: cannot write request body after response has been read
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:264)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0)
at com.flax.de.Helper.StorageService.saveImgToStorage(StorageService.java:192)
at com.flax.de.Helper.StorageService.getUploadUrl(StorageService.java:142)
at com.flax.de.Helper.StorageService.getAuthData(StorageService.java:95)
W/System.err: at com.flax.de.Categories.NewPostFragment.run(NewPostFragment.java:341)
at java.lang.Thread.run(Thread.java:764)
试图post将数据上传url的函数如下
static private void saveImgToStorage(Context context, String id, String uploadUrl, String uploadAuthorizationToken
, String downloadUrl, Uri imageUrl, String bucketName, Interfaces.OnSuccessHashMap onSuccess
, Interfaces.OnError onError) throws IOException {
String photoUrl = downloadUrl+"/file/"+bucketName+"/"+id;
HashMap hashMap = new HashMap();
hashMap.put(API.databaseStrings.URL, photoUrl);
Bitmap bmp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUrl);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] fileData = baos.toByteArray();
String contentType = "image/jpg";
String sha1 = "do_not_verify";
HttpURLConnection connection = null;
try {
URL url = new URL(uploadUrl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", uploadAuthorizationToken);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("X-Bz-File-Name", id);
connection.setRequestProperty("X-Bz-Content-Sha1", sha1);
connection.setDoOutput(true);
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getErrorStream().toString());
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getResponseCode());
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(fileData);
String jsonResponse = myInputStreamReader(connection.getInputStream());
JSONObject obj = new JSONObject(jsonResponse);
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
onError.onError();
} finally {
connection.disconnect();
}
}
错误码如您所见
401
我不知道为什么它不起作用。
正如我所说,它在我的 iOS 设备上工作得很好,它使用基本上相同的代码只写在 swift 中(取自 back blaze 的指南)
我个人认为这不是 back blaze 的问题,而是我的请求是错误的?
或者我的位图格式?
或者我必须异步进行?我在将它从主线程中移出之前用线程尝试过,但它也没有用。
W/System.err: java.io.FileNotFoundException: https://pod-000-1070-11.backblaze.com/b2api/v2/b2_upload_file/afec4cd53b2f323f715a0c18/c000_v0001070_t0002
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
如果使用此代码,我会收到什么
String photoUrl = downloadUrl+"/file/"+bucketName+"/"+id;
HashMap hashMap = new HashMap();
hashMap.put(API.databaseStrings.URL, photoUrl);
Bitmap bmp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUrl);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] fileData = baos.toByteArray();
String contentType = "image/jpg";
String sha1 = "do_not_verify";
HttpURLConnection connection = null;
String json = null;
try {
URL url = new URL(uploadUrl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", uploadAuthorizationToken);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("X-Bz-File-Name", id);
connection.setRequestProperty("X-Bz-Content-Sha1", sha1);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(fileData);
String jsonResponse = myInputStreamReader(connection.getInputStream());
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
函数第一个版本中的问题是这两行:
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getErrorStream().toString());
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getResponseCode());
getErrorStream()
和 getResponseCode()
都会导致发送请求和读取响应,因为您已从服务器请求错误响应和响应代码。 connection.getOutputStream()
抛出错误,因为发送请求正文为时已晚。
您应该删除这些行,或将它们移到以下行之后:
writer.write(fileData);
HttpURLConnection
是 'broken by design' 在您尝试读取响应时隐式发送请求的方式。
我正在使用 Backblaze 并设法将文件(图像和视频)上传到我的 iOS 应用程序 (swift 5) 中的存储桶,但我无法在我的 [=35] 中这样做=] android 应用
我收到正确的授权令牌,下载 url,上传 url 和 API Url。
但是当我尝试 post 时 url 它不起作用并且我收到以下错误
W/OkHttpClient: A connection to https://googleads.g.doubleclick.net/ was leaked. Did you forget to close a response body?
D/yyyy enclosed: saveImgToStorage: buffer(com.android.okhttp.internal.http.Http1xStream$FixedLengthSource@e5232ab).inputStream()
saveImgToStorage: 401
W/System.err: java.net.ProtocolException: cannot write request body after response has been read
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:264)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(Unknown Source:0)
at com.flax.de.Helper.StorageService.saveImgToStorage(StorageService.java:192)
at com.flax.de.Helper.StorageService.getUploadUrl(StorageService.java:142)
at com.flax.de.Helper.StorageService.getAuthData(StorageService.java:95)
W/System.err: at com.flax.de.Categories.NewPostFragment.run(NewPostFragment.java:341)
at java.lang.Thread.run(Thread.java:764)
试图post将数据上传url的函数如下
static private void saveImgToStorage(Context context, String id, String uploadUrl, String uploadAuthorizationToken
, String downloadUrl, Uri imageUrl, String bucketName, Interfaces.OnSuccessHashMap onSuccess
, Interfaces.OnError onError) throws IOException {
String photoUrl = downloadUrl+"/file/"+bucketName+"/"+id;
HashMap hashMap = new HashMap();
hashMap.put(API.databaseStrings.URL, photoUrl);
Bitmap bmp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUrl);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] fileData = baos.toByteArray();
String contentType = "image/jpg";
String sha1 = "do_not_verify";
HttpURLConnection connection = null;
try {
URL url = new URL(uploadUrl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", uploadAuthorizationToken);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("X-Bz-File-Name", id);
connection.setRequestProperty("X-Bz-Content-Sha1", sha1);
connection.setDoOutput(true);
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getErrorStream().toString());
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getResponseCode());
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(fileData);
String jsonResponse = myInputStreamReader(connection.getInputStream());
JSONObject obj = new JSONObject(jsonResponse);
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
onError.onError();
} finally {
connection.disconnect();
}
}
错误码如您所见
401
我不知道为什么它不起作用。 正如我所说,它在我的 iOS 设备上工作得很好,它使用基本上相同的代码只写在 swift 中(取自 back blaze 的指南) 我个人认为这不是 back blaze 的问题,而是我的请求是错误的? 或者我的位图格式?
或者我必须异步进行?我在将它从主线程中移出之前用线程尝试过,但它也没有用。
W/System.err: java.io.FileNotFoundException: https://pod-000-1070-11.backblaze.com/b2api/v2/b2_upload_file/afec4cd53b2f323f715a0c18/c000_v0001070_t0002
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
如果使用此代码,我会收到什么
String photoUrl = downloadUrl+"/file/"+bucketName+"/"+id;
HashMap hashMap = new HashMap();
hashMap.put(API.databaseStrings.URL, photoUrl);
Bitmap bmp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUrl);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] fileData = baos.toByteArray();
String contentType = "image/jpg";
String sha1 = "do_not_verify";
HttpURLConnection connection = null;
String json = null;
try {
URL url = new URL(uploadUrl);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", uploadAuthorizationToken);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("X-Bz-File-Name", id);
connection.setRequestProperty("X-Bz-Content-Sha1", sha1);
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(fileData);
String jsonResponse = myInputStreamReader(connection.getInputStream());
System.out.println(jsonResponse);
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
函数第一个版本中的问题是这两行:
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getErrorStream().toString());
Log.d("yyyy enclosed", "saveImgToStorage: " + connection.getResponseCode());
getErrorStream()
和 getResponseCode()
都会导致发送请求和读取响应,因为您已从服务器请求错误响应和响应代码。 connection.getOutputStream()
抛出错误,因为发送请求正文为时已晚。
您应该删除这些行,或将它们移到以下行之后:
writer.write(fileData);
HttpURLConnection
是 'broken by design' 在您尝试读取响应时隐式发送请求的方式。