Search code examples
javahttpcontent-disposition

Java Apache set additional parameters in "Content-Disposition:"


I am using java Apache 5.3.1, I am trying to send a multipart with XML, and need the following "Content-Disposition:" set -

Content-Disposition: form-data; name="xml"; filename="test.xml"

If I use this I get most if it -

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addPart("xml", new StringBody(xml, ContentType.APPLICATION_XML));

but that only produces

Content-Disposition: form-data; name="xml"

How do I get it to add the 'filename="test.xml"' part? Thanks


Solution

  • You have to use builder.addBinaryBody and it will add the filename.

    builder.addBinaryBody(
      "xml",
      xml.getBytes(),
      ContentType.APPLICATION_XML,"test.xlm");