Send image from Android client to AppEngine Cloud Endpoint

I am developing an Android application with AppEngine support. I am building the back end using Google Cloud Endpoints in Java. My problem is that I cannot send Bitmap from client to server.

I used the answer to this question , but even if the client part does not have any problems whatsoever, the server part does not receive data at all. I also think that this solution can be a bit complicated and that it can work in a different, simpler way, however this is my first time that I deploy the server and send the image to it for the first time, so I take any useful tips on this issue . Thanks!

Here is my code:

String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. String CRLF = "\r\n"; // Line separator required by multipart/form-data. String charset = "UTF-8"; HttpURLConnection connection = (HttpURLConnection) new URL("https://path_to_my_app/_ah/api/registration/v1/uploadImage").openConnection(); connection.setDoOutput(true); connection.setReadTimeout(60000); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); PrintWriter writer = null; try { OutputStream output = connection.getOutputStream(); writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important! // Send text file. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + somename + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF).flush(); BufferedReader reader = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byteArray = stream.toByteArray(); try { reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(byteArray), charset)); for (String line; (line = reader.readLine()) != null;) { writer.append(line).append(CRLF); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} } writer.flush(); // End of multipart/form-data. writer.append("--" + boundary + "--").append(CRLF); } finally { if (writer != null) { writer.close(); } } 

Server side:

 @ApiMethod(name = "uploadImage", httpMethod = "POST") public void uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException { ServletFileUpload fileUpload = new ServletFileUpload(); try { FileItemIterator iterator = fileUpload.getItemIterator(request); while(iterator.hasNext()){ FileItemStream itemStream = iterator.next(); String fieldName = itemStream.getFieldName(); log.info("field name:"+fieldName); InputStream stream = itemStream.openStream(); String result = getStringFromInputStream(stream); log.info("result: "+result); stream.close(); } } catch (FileUploadException e) { e.printStackTrace(); } 

}

I am currently getting 204 content types.

+1
source share
1 answer

I did it!

I think this is not the best way to do this, but it works, so I am fine until I get a better solution.

So, I take a Bitmap image and convert it to String:

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byte[] bitmapByte = outputStream.toByteArray(); String stringEncodedImage = Base64.encodeToString(bitmapByte, Base64.DEFAULT); 

Then I create an httpPostRequest and set a JsonObject to it with the image converted to String in it.

 HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("https://my_app_path/_ah/api/registration/v1/uploadImage"); JSONObject jsonObject = new JSONObject(); jsonObject.put("image",stringEncodedImage); StringEntity stringEntity = new StringEntity(jsonObject.toString()); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(stringEntity); HttpResponse response = httpClient.execute(httpPost); 

On the server side, at my endpoint, I do this:

 @ApiMethod(name = "uploadImage", httpMethod = "POST") public JSONObject uploadImage(JSONObject request) throws IOException { String imageInString = (String) request.get("image"); Blob blob = new Blob(imageInString.getBytes()); ....save blob and do whatever you want... } 

The same thing happens differently. I will pack Blob in JsonObject and submit it.

+3
source

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


All Articles