IMPORTANT: You MUST set CHUNKED settings to avoid OutOfMemoryError: Java heap space caused by sun.net.www.http.PosterOutputStream used by Jersey implementation.

import org.glassfish.jersey.client.ClientProperties;
 
public class MyClient {
 
    private static final int BUFFER_SIZE = 1024;
 
    public Response uploadFile(final File file) throws GeneralSecurityException, IOException {
 
        WebTarget target = ClientBuilder.newClient()
                .property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024)
                .property(ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED")
                .target(baseURL)
                .path("myservice").path("file")
                .queryParam("fileName", file.getName());
 
        StreamingOutput out = new StreamingOutput() {
 
            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
 
                byte[] chunk = new byte[BUFFER_SIZE];
                try (FileInputStream is = new FileInputStream(file)) {
 
                    int available;
                    while ((available = is.available()) > 0) {
                        if (available >= BUFFER_SIZE) {
                            is.read(chunk);
                            output.write(chunk);
                        } else {
                            output.write(is.read());
                        }
                    }
                }
            }
        };
 
        Response response = target.request().post(Entity.text(out));
        return response;
    }
}