title: passing InputStream as parameter to web service
body: <p>I am trying to upload file but i am not doing it through html form. QueryParam and PathParam can't be used. So can anyone tell how to pass stream.</p>
  
  <p>My HttPClient looks like:</p>
  
  <pre><code>try
      {
          HttpClient httpclient = new DefaultHttpClient();
          InputStream stream=new FileInputStream(new File("C:/localstore/ankita/Desert.jpg"));
          String url="http://localhost:8080/Cloud/webresources/fileupload";
          HttpPost httppost = new HttpPost(url);
          HttpResponse response = httpclient.execute(httppost);
      }
      catch(Exception e){}
  </code></pre>
  
  <p>and my web service class looks somewhat like:</p>
  
  <pre><code>@Path("/fileupload")
  public class UploadFileService {
  
  @POST
  @Consumes(MediaType.APPLICATION_OCTET_STREAM)
  
  public Response uploadFile(InputStream in) throws IOException
  {     
      String uploadedFileLocation = "c://filestore/Desert.jpg" ;
  
      // save it
      saveToFile(in, uploadedFileLocation);
  
      String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
  
      return Response.status(200).entity(output).build();
  
  }
  
  // save uploaded file to new location
  private void saveToFile(InputStream uploadedInputStream,String uploadedFileLocation) 
  {
      try {
          OutputStream out = null;
          int read = 0;
          byte[] bytes = new byte[1024];
  
          out = new FileOutputStream(new File(uploadedFileLocation));
          while ((read = uploadedInputStream.read(bytes)) != -1) 
          {
              out.write(bytes, 0, read);
          }
          out.flush();
          out.close();
      } catch (IOException e) 
      {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone help?? </p>
  
  <pre><code> String url="http://localhost:8080/Cloud/webresources/fileupload";
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(url);
          InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(new File("C:/localstore/ankita/Desert.jpg")), -1);
          reqEntity.setContentType("binary/octet-stream");
          reqEntity.setChunked(true); // Send in multiple parts if needed
          httppost.setEntity(reqEntity);
          HttpResponse response = httpclient.execute(httppost);   
  </code></pre>
  
  <p>How web service will look like?</p>