0

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

Community
  • 1
  • 1
sreeharsha
  • 23
  • 3
  • 1
    possible duplicate of [Java Apache HttpClient error uploading files](http://stackoverflow.com/questions/20262769/java-apache-httpclient-error-uploading-files) – LunaCodeGirl Sep 15 '15 at 22:17
  • Possible duplicate of [Android project using httpclient --> http.client (apache), post/get method](https://stackoverflow.com/questions/874227/android-project-using-httpclient-http-client-apache-post-get-method) – Gert Arnold Nov 02 '17 at 22:24

4 Answers4

1

There is another simple way we can override InputStreamBody.getContentLength without a need to create our own ContentBody implementation if you know your contentLength-

InputStreamBody inputStreamBody = new InputStreamBody(inputStream, ContentType.APPLICATION_OCTET_STREAM, fileName){
                @Override
                public long getContentLength(){return contentLength;}
            };

MultipartEntityBuilder.create()
            .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
            .addPart("dataAsStream", inputStreamBody)
            .build();
0

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;
  }

}

Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
  • This is exactly what i have done. I forgot to answer my question. But this definitely works. The problem is as mentioned in the above link the content length is unknown if the attachment is inputstream. Extending InputstreamReader and overriding the getcontentlength should solve this. – sreeharsha Sep 17 '15 at 15:19
0

An other option is org.apache.http.entity.mime.content.ByteArrayBody, if don't know what is the size beforehand (!!! You have to be sure that the content of the inputStream will fit into the memory of JVM):

InputStream inputStream = // get your input stream somehow
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;
    byte buff[] = new byte[4096];
    while( -1 != (i = inputStream.read(buff))){
        baos.write(buff, 0, i);
    }
    ByteArrayBody bab = new ByteArrayBody(baos.toByteArray(), "fileName1");
Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
0

Here is how I solved it.

public class CustomInputStreamBody extends InputStreamBody {
    private InputStream inputStream;
    private BufferedReader bufferedReader = null;
    StringBuilder stringBuilder = null;
    public CustomInputStreamBody(InputStream in,ContentType contentType){
        super(in,contentType);
        this.inputStream=in;
    }
    @Override
    public long getContentLength() {
        int length=0;
        byte[] bytes=null;
        try {

            bytes = IOUtils.readBytesFromStream(inputStream);
            // iterate to get the data and append in StringBuilder
            System.out.println("___________"+bytes.length);
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
        return bytes.length;
    }
Pang
  • 9,564
  • 146
  • 81
  • 122
sreeharsha
  • 23
  • 3