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 7929827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:13:00+00:00 2026-06-03T20:13:00+00:00

I am trying to implement async file upload with progress with sonatype async http

  • 0

I am trying to implement async file upload with progress with sonatype async http client – https://github.com/sonatype/async-http-client.

I tried the method suggested in the docs. Using transfer listener.
http://sonatype.github.com/async-http-client/transfer-listener.html

I implemented onBytesSent of TransferListener interface (just as test):

public void onBytesSent(ByteBuffer byteBuffer) {

     System.out.println("Total bytes sent - ");
     System.out.println(byteBuffer.capacity());
}

Then in another thread(because I don’t want to block the app) I tried to do the following:

TransferCompletionHandler tl = new TransferCompletionHandler();
tl.addTransferListener(listener);

asyncHttpClient.preparePut(getFullUrl(fileWithPath))
      .setBody(new BodyGenerator() {
           public Body createBody() throws IOException {
                return new FileBodyWithOffset(file, offset);
           }
       })
       .addHeader(CONTENT_RANGE, new ContentRange(offset, localSize).toString())
       .execute(handler).get();

Everything is fine. File is uploaded correctly and very fast. But the issue is – I am getting messages from onBytesSent in TransferListener only AFTER the upload is finished. For exmaple the upload is completed in 10 minutes. And during that 10 minutes I get nothing. And only after that everything is printed on the console.

I can’t figure out what is wrong with this code. I just tried to follow the docs.

I tried to execute the above code in the main thread and it didn’t work either.

Maybe it is a wrong way to implement upload progress listener using this client?

  • 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-03T20:13:02+00:00Added an answer on June 3, 2026 at 8:13 pm

    I will answer it myself. I did not manage to resolve the issue with TransferListener. So I tried the other way.

    I had put the progress logick inside Body interface implementation (inside read method):

    public class FileBodyWithOffset implements Body {
    
        private final ReadableByteChannel channel;
    
        private long actualOffset;
    
        private final long contentLength;
    
        public FileBodyWithOffset(final File file, final long offset) throws IOException {
    
            final InputStream stream = new FileInputStream(file);
    
            this.actualOffset = stream.skip(offset);
    
            this.contentLength = file.length() - offset;
    
            this.channel = Channels.newChannel(stream);
        }
    
    
        public long getContentLength() {
    
            return this.contentLength;
        }
    
        public long read(ByteBuffer byteBuffer) throws IOException {
    
            System.out.println(new Date());
    
            actualOffset += byteBuffer.capacity();
    
            return channel.read(byteBuffer);
        }
    
        public void close() throws IOException {
    
            channel.close();
        }
    
        public long getActualOffset() {
    
            return actualOffset;
        }
    }
    

    Maybe it is a dirty trick, but at least it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a pattern I read from Don Syme's blog ( https://blogs.msdn.microsoft.com/dsyme/2010/01/09/async-and-parallel-design-patterns-in-f-parallelizing-cpu-and-io-computations/
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I'm trying to implement the registration plugin on my application as documented here: http://developers.facebook.com/docs/plugins/registration/advanced/
Following this question , I am trying to implement an async method using the
I am new to Objective C and I am trying to implement an async
I'm trying to implement the progress notification mechanism when copying files. I'm doing this
I'm trying to implement async on Android but it keeps crashing my app, the
I'm using Microsoft Sync Framework 2.0 in trying to implement a file sync app.
I am trying to implement a ListView with async image loading inside a Fragment.
Am trying to implement a generic way for reading sections from a config file.

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.