簡體   English   中英

從Android將文件上傳到GAE blobstore(Python)

[英]Upload file to GAE blobstore (Python) from android

以下是我嘗試使用的代碼。 從此線程復制它:

public void sendToServer() throws IOException, URISyntaxException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://www.prescuem.appspot.com/report");

    HttpResponse urlResponse = httpClient.execute(httpGet);

    String result = EntityUtils.toString(urlResponse.getEntity());

我努力了:

 Uri fileUri = Uri.parse("file:///" + filename); // Gets the Uri of the file in the sdcard

    File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri

Uri fileUri = Uri.parse("file://" + filename); // Gets the Uri of the file in the sdcard

        File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri

File file = new File( context.getFilesDir(), filename );

這種添加文件的方式均無效。

    FileBody fileBody = new FileBody(file, "multipart/form-data");

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("file", fileBody);

    HttpPost httpPost = new HttpPost(result);
    show(result);

    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost);
    response.getStatusLine();
    show(response.getStatusLine().toString());

}

文件名就像:“ / storage / sdcard1 / send.png”

服務器端代碼如下:

class GetBlobstoreUrl(webapp2.RequestHandler):
    def get(self):
        logging.info('here')
        upload_url = blobstore.create_upload_url('/upload')
        logging.info("url blob %s", upload_url)
        self.response.out.write(upload_url)


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.response.write('uploaded')

從我的android設備上載時,我收到內部服務器錯誤,當我在GAE儀表板上檢查日志時,就像這樣:

2014-08-16 13:16:51.658 /upload 500 175ms 1kb Apache-HttpClient/UNAVAILABLE (java 1.4) module=default version=1
0.1.0.30 - - [16/Aug/2014:13:16:51 -0700] "POST /upload HTTP/1.1" 500 1832 - "Apache-HttpClient/UNAVAILABLE (java 1.4)" "www.prescuem.appspot.com" ms=175 cpu_ms=0 cpm_usd=0.000205 app_engine_release=1.9.9 trace_id=7d164e50e0f57814373999d3f4c3669e instance=00c61b117cd4ccecda5feef9443470c8a00c9f43
E 2014-08-16 13:16:51.656
list index out of range
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~prescuem/1.378016087775419183/main.py", line 124, in post
    blob_info = upload_files[0]
IndexError: list index out of range

我該如何解決這個問題。

檢查filename 如果以斜杠開頭,則您的uri斜杠太多:

"file:///" + filename # filename = "/storage/sdcard1/send.png"

變成:

"file:////storage/sdcard1/send.png"

在發送文件名之前,請先記錄該文件名,並確保該文件名存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM