Search code examples
httpcxfapache-httpclient-4.x

Java Apache HttpClient error uploading files with InputStream


Am having the same issue with inputstream. Can you please share more details about your fix please.

Thanks, Harsha

link to your question

Java Apache HttpClient error uploading files


Solution

  • The code of the extended org.apache.http.entity.mime.content.InputStreamBody will be something like this. You will need to somehow calculate the correct content length before creating the InputStreamBodyExtended

    public class InputStreamBodyExtended extends InputStreamBody {
    
      private long contentLength = -1; 
    
      public InputStreamBodyExtended(InputStream in, String filename, long contentLength) {
        super(in, filename);
        this.contentLength = contentLength;
      }
    
      public InputStreamBodyExtended(InputStream in, ContentType contentType, long contentLength) {
        super(in, contentType);
        this.contentLength = contentLength;
      }
    
      public InputStreamBodyExtended(InputStream in, ContentType contentType,
            String filename, long contentLength) {
        super(in, contentType, filename);
        this.contentLength = contentLength;
      }
    
      @Override
      public long getContentLength() {
        return contentLength;
      }
    

    }