title: send files(images) from android device to webservice on the server written using REST
body: <p>I want to send images from android device to my web application running on server with tomcat. please help me in writing small code for sending image to a REST web service running on Web server. Please provide me the sample code if possible. I am stuck with what method to use. Any help would be greatly appreciated. thanks in advance.</p>
  
  <p>Edit: The answer for this question is as follows</p>
  
  <pre><code>while(it.hasNext()){
               File file = new File((new StringBuilder()).append(Environment.getExternalStorageDirectory()).append(File.separator).append("jcms").append(File.separator).append("Customer_").append( customer.getId()).toString());
               File[] listOfFiles = file.listFiles(); 
  
               for(int i=0;i&lt;listOfFiles.length;i++){
                   JSONObject message = new JSONObject();
                   File fil=listOfFiles[i];
                   FileInputStream imageInFile = new FileInputStream(fil);
                   byte imageData[] = new byte[(int)fil.length()];
                   imageInFile.read(imageData);
                   String imageDataString = encodeImage(imageData);
  
  
  
                   URL url=new URL(ClearCustomersContract.CLEAR_SERVER_URL);
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                   connection.setDoOutput(true);
                   connection.setRequestProperty("Content-Type", "application/json");
                   connection.setRequestMethod("POST");
                   connection.setConnectTimeout(5000);
                   connection.setReadTimeout(5000);
                   OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                   out.write(imageDataString);
                   out.close();
  
                   BufferedReader in = new BufferedReader(new InputStreamReader(
                           connection.getInputStream()));
                   while (in.readLine() != null) {
                   }
                   in.close();    
               }
          }
  </code></pre>
  
  <p>And The REST Webservice on server side is like</p>
  
  <pre><code>@Override
  @POST
  @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_OCTET_STREAM})
  @Path("/getData")
  public Response getAllTheSyncData(InputStream incomingData) {
      StringBuilder sb = new StringBuilder();
      try {
          BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
          String line = null;
          while ((line = in.readLine()) != null) {
              sb.append(line);
          }
      } catch (Exception e) {
          System.out.println("Error Parsing: - ");
      }
      return Response.status(200).entity("Success").build();
  }
  </code></pre>
  
  <p>and this is how we convert the string back to image.</p>
  
  <p>byte[] imageByteArray = decodeImage(jsonObj.get("imageData").toString());</p>
  
  <pre><code>               imageOutFile = new FileOutputStream(
                          "C:/Users/SUNILKUMAR/Desktop/result.jpg");
  
              // Write a image byte array into file system
  
              imageOutFile.write(imageByteArray);
  
  
              imageOutFile.close();
  </code></pre>