如何使用移動後端入門或Google雲終端將文件從Android上傳到Google App Engine Blobstore?移動後端入門 - 上傳到AppEngine Blobstore
3
A
回答
5
與移動後端入門者共享我的前景。
要獲取上傳和下載網址,你需要這兩種方法添加到CloudBackend.java
類,使從活動訪問網址:
public String getUploadBlobURL(String bucketName, String path, String accessMode) {
String url = null;
try {
url = getMBSEndpoint().blobEndpoint()
.getUploadUrl(bucketName, path, accessMode).execute()
.getShortLivedUrl();
} catch (IOException e) {
e.printStackTrace();
}
return url;
}
public String getDownloadBlobURL(String bucketName, String path) {
String url = null;
try {
url = getMBSEndpoint().blobEndpoint()
.getDownloadUrl(bucketName, path).execute()
.getShortLivedUrl();
} catch (IOException e) {
e.printStackTrace();
}
return url;
}
然後你可以使用URL來流的字節到谷歌雲存儲與幫扶的標準客戶端庫。
下面我會舉例說明如何使用它們。
對於上傳文件到谷歌雲存儲,你可以使用類似這樣的東西:
活動
File fileUp = new File(Environment.getExternalStorageDirectory(), fileName);
new AsyncBlobUploader(this, mProcessingFragment.getCloudBackend()).execute(fileUp);
的AsyncTask
public class AsyncBlobUploader extends AsyncTask<File, Void, String> {
private Context context;
private ProgressDialog pd;
private CloudBackend cb;
public AsyncBlobUploader(Context context, CloudBackend cb) {
this.context = context;
this.cb = cb;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(context, null,
"Loading... Please wait...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setIndeterminate(true);
pd.setCancelable(true);
pd.show();
}
protected String doInBackground(File... files) {
File file = files[0];
String uploadUrl = cb.getUploadBlobURL(bucketName, file.getName(),"PUBLIC_READ_FOR_APP_USERS");
String url = uploadUrl.split("&Signature")[0]; // url without Signature
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
FileBody filebody = new FileBody(file,ContentType.create(getMimeType(file
.toString())), file.getName());
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", filebody);
httppost.setEntity(multipartEntity.build());
System.out.println("executing request " + httppost.getRequestLine());
try {
HttpResponse response = httpclient.execute(httppost);
Log.i("response", response.getStatusLine().toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
return (String) uploadUrl;
}
protected void onPostExecute(String result) {
pd.dismiss();
Log.d("BlobUrl", result);
}
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
}
MultipartEntityBuilder
類沒有納入android標準庫,所以你需要下載httpclient幷包含到您的項目中。
注意此行String url = uploadUrl.split("&Signature")[0];
我正在切斷網址簽名。 (使用URL簽名我越來越503 Internal Server Error
但是沒有它一切正常我不爲什麼發生這種情況。)
如需下載,你可以使用這個片段:
活動
File fileDown = new File(Environment.getExternalStorageDirectory(),
fileName); //file to create
new AsyncBlobDownloader(imageView, mProcessingFragment.getCloudBackend())
.execute(fileDown);
的AsyncTask
public class AsyncBlobDownloader extends AsyncTask<File, Integer, File> {
private ImageView imageView;
private ProgressDialog pd;
private CloudBackend cb;
public AsyncBlobDownloader(ImageView imageView, CloudBackend cb) {
this.imageView = imageView;
this.cb = cb;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(imageView.getContext(), null,
"Loading... Please wait...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(true);
pd.show();
}
protected File doInBackground(File... files) {
File file = files[0];
String downloadUrl = cb.getDownloadBlobURL(bucketName,
file.getName());
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(downloadUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.i("Response",
"Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage());
}
int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream(file);
byte data[] = new byte[4096];
int count;
while ((count = input.read(data)) != -1) {
if (isCancelled()) {
input.close();
return null;
}
output.write(data, 0, count);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return file;
}
protected void onPostExecute(File result) {
pd.dismiss();
imageView.setImageURI(Uri.fromFile(result));
}
}
注意:要使用Google雲端存儲,您需要啓用結算。你也需要在GCS中創建桶。
相關問題
- 1. 文件上傳到AppEngine Blobstore
- 2. 上傳到Android中的Appengine Blobstore
- 3. 服務器端上傳到Blobstore - Java Google Appengine
- 4. AppEngine:從Blobstore進入S3?
- 5. 清理AppEngine BlobStore
- 6. 通過HTTP請求將二進制數據上傳到AppEngine Blobstore
- 7. 在appengine中,如何將db.Blob移動到Blobstore中
- 8. 上傳到Appengine Blobstore時處理表單失敗
- 9. GAE - 將圖像從移動設備上傳到blobstore
- 10. 如何使用Appengine Blobstore上傳和流式傳輸視頻
- 11. FinalizationException - AppEngine Java Blobstore API
- 12. Appengine Blobstore - 視頻流
- 13. 使用urlfetch上傳到blobstore
- 14. 從Internet到AppEngine BlobStore(Java)的圖像
- 15. 將文件寫入Blobstore AppEngine並提供給客戶端
- 16. 行動後端入門通知警報
- 17. 將Blobstore移動到GCS:Google App Engine Python
- 18. 的Java BLOBSTORE和谷歌SPI AppEngine上端點
- 19. 直接將數據放入AppEngine的Blobstore
- 20. GraphQL java後端入門
- 21. 從代碼發佈圖像到blobstore appengine?
- 22. 從Blobstore遷移到Google Cloud Storage for AppEngine Connected Android項目
- 23. Go + Appengine Blobstore,調整圖片上傳尺寸
- 24. 後端開發入門
- 25. appengine python後端
- 26. 無法將文件從Google雲端硬盤移動到Blobstore
- 27. Blobstore上傳+ Ajax/Alternative
- 28. Blobstore上傳與AsiHttpRequest
- 29. Phpstorm不能上傳到谷歌的AppEngine
- 30. GAE:java.lang.NoClassDefFoundError:com/google/appengine/api/blobstore/BlobstoreServiceFactory