Upload image from android to java servlet and save it

I was looking for all this, and nothing worked for me.

I am trying to upload an image from an android application to a Java servlet and save it on the server. Every solution I found did not work for me.

Currently my code is: an Android app sends an image to the servlet, when I try to save it, the file is created, but it is empty :(

Thanks for your help!

My code in android client (i_file is the location of the file on the device):

public static void uploadPictureToServer(String i_file) throws ClientProtocolException, IOException { // TODO Auto-generated method stub HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://192.168.1.106:8084/Android_Server/GetPictureFromClient"); File file = new File(i_file); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file, "image/jpeg"); mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); } 

My server side code is:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); InputStream in = request.getInputStream(); OutputStream out = new FileOutputStream("C:\\myfile.jpg"); IOUtils.copy(in, out); //The function is below out.flush(); out.close(); } 

IOUtils.copy Code:

 public static long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[4096]; long count = 0L; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } 
+6
source share
1 answer

You misinterpreted the problem. The image file is not empty, but the image file is corrupted because you save the entire array of HTTP requests as an image file instead of extracting the part containing the image from the body of a multi-page HTTP request.

You need HttpServletRequest#getPart() to get the body parts of a multi-page request. If you are already on Servlet 3.0 (Tomcat 7, Glassfish 3, etc.), First annotate your @MultipartConfig servlet

 @WebServlet("/GetPictureFromClient") @MultipartConfig public class GetPictureFromClient extends HttpServlet { // ... } 

then fix doPost() as follows to capture the part by its name, and then its body as input:

 InputStream in = request.getPart("userfile").getInputStream(); // ... 

If you are not already on Servlet 3.0, then get Apache Commons FileUpload . See Also this answer for a detailed example: How to upload files to a server using JSP / Servlet?

Oh, please get rid of the processRequest() method created by Netbeans. This is not the right way to delegate both doGet() and doPost() one processRequest() method, and it only confuses other developers and maintainers who donโ€™t use Netbeans.

+8
source

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


All Articles