Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:27:35+00:00 2026-06-08T07:27:35+00:00

I have a jersey client that need to upload a file big enough to

  • 0

I have a jersey client that need to upload a file big enough to require a progress bar.
The problem is that, for an upload that requires some minutes, i see the bytes transfered to go to 100% as soon as the application has started. Then it takes some minutes to print the “on finished” string.
It is as if the bytes were sent to a buffer, and i was reading the transfert-to-the buffer speed instead of the actual upload speed. This makes the progress bar useless.

This is the very simple code:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource("www.myrestserver.com/uploads");
WebResource.Builder builder = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE);

FormDataMultiPart multiPart = new FormDataMultiPart();
FileDataBodyPart fdbp = new FileDataBodyPart("data.zip", new File("data.zip"));
BodyPart bp = multiPart.bodyPart(fdbp);
String response = builder.post(String.class, multiPart);

To get progress state i’ve added a ContainerListener filter, obviouslt before calling builder.post:

final ContainerListener containerListener = new ContainerListener() {

        @Override
        public void onSent(long delta, long bytes) {
            System.out.println(delta + " : " + long);
        }

        @Override
        public void onFinish() {
            super.onFinish();
            System.out.println("on finish");
        }

    };

    OnStartConnectionListener connectionListenerFactory = new OnStartConnectionListener() {
        @Override
        public ContainerListener onStart(ClientRequest cr) {
            return containerListener;
        }

    };

    resource.addFilter(new ConnectionListenerFilter(connectionListenerFactory));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:27:38+00:00Added an answer on June 8, 2026 at 7:27 am

    it should be enough to provide you own MessageBodyWriter for java.io.File which fires some events or notifies some listeners as progress changes

    @Provider()
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public class MyFileProvider implements MessageBodyWriter<File> {
    
        public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return File.class.isAssignableFrom(type);
        }
    
        public void writeTo(File t, Class<?> type, Type genericType, Annotation annotations[], MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
            InputStream in = new FileInputStream(t);
            try {
                int read;
                final byte[] data = new byte[ReaderWriter.BUFFER_SIZE];
                while ((read = in.read(data)) != -1) {
                    entityStream.write(data, 0, read);
                    // fire some event as progress changes
                }
            } finally {
                in.close();
            }
        }
    
        @Override
        public long getSize(File t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return t.length();
        }
    }
    

    and to make your client application uses this new provider simply:

    ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(MyFileProvider.class);
    

    or

    ClientConfig config = new DefaultClientConfig();
    MyFileProvider myProvider = new MyFileProvider ();
    cc.getSingletons().add(myProvider);
    

    You would have to also include some algorithm to recognize which file is transfered when receiving progress events.

    Edited:

    I just found that by default HTTPUrlConnection uses buffering. And to disable buffering you could do couple of things:

    1. httpUrlConnection.setChunkedStreamingMode(chunklength) – disables buffering and uses chunked transfer encoding to send request
    2. httpUrlConnection.setFixedLengthStreamingMode(contentLength) – disables buffering and but ads some constraints to streaming: exact number of bytes must be sent

    So I suggest the final solution to your problem uses 1st option and would look like this:

    ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(MyFileProvider.class);
    URLConnectionClientHandler clientHandler = new URLConnectionClientHandler(new HttpURLConnectionFactory() {
         @Override
         public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
               HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setChunkedStreamingMode(1024);
                    return connection;
                }
    });
    Client client = new Client(clientHandler, config);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jersey client that is getting JSON from a source that I
I have a Jersey client that is successfully calling a REST service and populating
I am writing a Java client that needs to upload a file to a
I need to profile a server-client Java application (based on Jersey/REST FYI). I have
I have an Jersey API that returns Odata standard responses and consumes the same.
Using Jersey, I want to be able to have a GET request, that would
I have written a REST web service with Jersey Server (that totally rocks !).
I have Jersey client with a lot of functionality and now requirements are changed
I have a Jersey REST service with a resource class that calls methods on
I have the following scenario: Server: Jetty (with configured JAAS) Client: Jersey invoked via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.