use*_*032 6 python java google-app-engine android flask
我正在尝试将文件上传到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())
Run Code Online (Sandbox Code Playgroud)
使用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>""")
Run Code Online (Sandbox Code Playgroud)
但我必须能够从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));
Run Code Online (Sandbox Code Playgroud)
但是当我在GAE上运行它时,我upload_files[0]在上传时获得了一个超出范围的索引- 错误在哪里?我似乎无法找到它,代码是如此相似,我已经确认工作(烧瓶,openshift):
def upload_file():
if request.method == 'POST':
file = request.files['file']
...
Run Code Online (Sandbox Code Playgroud)
更新:完整的错误日志:
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
Run Code Online (Sandbox Code Playgroud)
blobstore.create_upload_url('/upload')在 java 中尚未生成 url /upload
,/upload 是您尝试发布以查看该处理程序生成的 html 的位置,您将看到表单的操作 url 不同。
您可以让处理程序使用 Java 代码获取的上传 URL 进行响应,然后用于发布到该 URL。
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |