Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 322
Dima Fomin Avatar asked Sep 05 '25 19:09

Dima Fomin


1 Answers

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);
like image 83
Zoltán Avatar answered Sep 09 '25 19:09

Zoltán