【问题标题】:Upload file to GAE blobstore (Python) from android从 android 将文件上传到 GAE blobstore (Python)
【发布时间】:2014-10-10 05:34:37
【问题描述】:

以下是我尝试使用的代码。从this thread复制它:

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

请问我该如何解决这个问题。

【问题讨论】:

    标签: java android google-app-engine python-2.7 apache-httpclient-4.x


    【解决方案1】:

    检查filename。如果它以斜杠开头,那么你的 uri 有太多的斜杠:

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

    变成:

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

    在发送之前记录文件名,并确保它存在。

    【讨论】:

    • 即使当我使用“file://”或当我使用“File file = new File(context.getFilesDir(), filename);”时,它也不起作用并且文件实际上存在于我的 android 设备(我使用意图选择文件并获取路径)
    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 2014-06-27
    • 2015-06-18
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多