How can I get a custom Content-Disposition line using Apache httpclient?

后端 未结 2 1990
抹茶落季
抹茶落季 2021-01-12 13:09

I am using the answer here to try to make a POST request with a data upload, but I have unusual requirements from the server-side. The server is a PHP script wh

相关标签:
2条回答
  • 2021-01-12 13:22

    As a cleaner alternative to creating an extra anonymous inner class and adding side effects to protected methods, use FormBodyPartBuilder:

    StringBody stuff = new StringBody("stuff");
    
    StringBuilder buffer = new StringBuilder();
        buffer.append("form-data; name=\"");
        buffer.append(getName());
        buffer.append("\"");
        buffer.append("; filename=\"-\"");
    String contentDisposition = buffer.toString();
    
    FormBodyPartBuilder partBuilder = FormBodyPartBuilder.create("file", stuff);
    partBuilder.setField(MIME.CONTENT_DISPOSITION, contentDisposition);
    
    FormBodyPart fbp = partBuilder.build();
    
    0 讨论(0)
  • 2021-01-12 13:34

    Try this

    StringBody stuff = new StringBody("stuff");
    FormBodyPart customBodyPart = new FormBodyPart("file", stuff) {
    
        @Override
        protected void generateContentDisp(final ContentBody body) {
            StringBuilder buffer = new StringBuilder();
            buffer.append("form-data; name=\"");
            buffer.append(getName());
            buffer.append("\"");
            buffer.append("; filename=\"-\"");
            addField(MIME.CONTENT_DISPOSITION, buffer.toString());
        }
    
    };
    MultipartEntity entity = new MultipartEntity();
    entity.addPart(customBodyPart);
    
    0 讨论(0)
提交回复
热议问题