我正在使用 Backblaze 并设法将文件(图像和视频)上传到我的 iOS 应用程序(swift 5)中的存储桶,但我无法在我的 java android 应用程序中这样做。
我收到了正确的身份验证令牌、下载网址、上传网址和 API 网址。
但是当我尝试将其发布到该 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$3.run(NewPostFragment.java:341)
at java.lang.Thread.run(Thread.java:764)
尝试将数据发布到上传 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();
}