【问题标题】:HTTPPost multipart (upload file) from Java to Python webapp2从 Java 到 Python webapp2 的 HTTPPost 多部分(上传文件)
【发布时间】:2012-08-14 10:08:34
【问题描述】:

我正在尝试将文件上传到 GAE - 服务器端代码:

 class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
            upload_files = self.get_uploads('file')  
            blob_info = upload_files[0] # When using flask, request.files[0] gives correct output.
            self.response.out.write('/serve/%s' % blob_info.key())

使用 HTML,我可以让它工作:

upload_url = blobstore.create_upload_url('/upload')
    self.response.out.write('<html><body>')
    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
        name="submit" value="Submit"> </form></body></html>""")

但我必须能够从 Java 执行多部分发布请求。我有一个在 openshift (flask, request.files[0]) 上托管的烧瓶项目,其中 Java 代码似乎可以工作:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
DefaultHttpClient mHttpClient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost(getString(R.string.url_webservice) + "/upload");
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", new FileBody(new File(path)));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new MyUploadResponseHandler(path));

但是当我在 GAE 上运行它时,我在上传时在upload_files[0] 上得到了一个超出范围的索引 - 错误在哪里?我似乎找不到它,代码与我确认可以工作的以下代码非常相似(flask,openshift)

def upload_file():
if request.method == 'POST':
    file = request.files['file']
    ...

更新:完整的错误日志:

list index out of range
Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line  1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
   rv = self.router.dispatch(request, response)
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
   File "myapp.py", line 22, in post
blob_info = upload_files[0]
 IndexError: list index out of range

【问题讨论】:

    标签: java android python google-app-engine flask


    【解决方案1】:

    blobstore.create_upload_url('/upload') 不会生成 URL /upload 然而在java中, /upload 是你试图发布到的地方 查看该处理程序生成的 html,您将看到表单的操作 url 不同。

    您可以让处理程序使用您的 java 代码获取然后用于发布到的上传 url 进行响应。

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多