我正在尝试将文件上传到 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 (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