'How to set @PartFilename of Entity field in @POST RESTEasy client method in runtime?

I am trying to write Resteasy Client Class to make simple POST multipart/form-data request to upload file. So POST request must contain Content-Disposition: form-data; name="files"; filename="myfile.txt" parameters. I am able to set this statically with @PartFilename annotation for field in entity (value) class. like this

public class UploadStreamMultipartBody {

    @FormParam("files")
    @PartFilename(value = "myfile.txt")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public InputStream file;

}

But I cannot realize how to make it dynamically so I can provide file name in RUNTIME 🤔 Cause annotation value is read once in compile time.



Solution 1:[1]

Use MultipartFormDataOutput instead.

Interface example:

    @POST
    @Path("/document-store")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    void uploadDocument(@MultipartForm MultipartFormDataOutput data);

Usage:

var form = new MultipartFormDataOutput();
form.addFormData(/*key=*/"file", inputStream, new MediaType("application", "pdf"), fileName);
documentStoreClient.uploadDocument(form);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zoltán