HTTPPost multipart (upload file) from Java to Python webapp2

I am trying to upload a file to GAE - serveride code:

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()) 

Using HTML, I can make it work:

 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>""") 

But I should be able to make a multi-page submit request with Java. I have a jar project hosted in openshift (flask, request.files[0] instead), in which Java code works:

 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)); 

But when I run it on GAE, I get an index outside on upload_files[0] at upload - Where is the error? I can not find it, the code is so similar to the following, which I confirmed to work (bulb, first):

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

UPDATE: full error log:

 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 
+6
source share
1 answer

blobstore.create_upload_url('/upload') does not create the URL /upload yet in java, / upload, where you are trying to send a message, look at the received html from this handler, and you will see that the action URL is different.

You could handle a handler with a download URL that gets your Java code, and then use it to publish.

+2
source

Source: https://habr.com/ru/post/923162/


All Articles