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

  • Home
  • SEARCH
  • 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 8146749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:06:28+00:00 2026-06-06T14:06:28+00:00

For downloading stuff I work with the apache classes HTTPResponse HTTPClient etc. I check

  • 0

For downloading stuff I work with the apache classes HTTPResponse HTTPClient etc.
I check for a valid download like this:

entity.writeTo(new FileOutputStream(outfile));
        if(outfile.length()!=entity.getContentLength()){
            long fileLength = outfile.length();
            outfile.delete();
            throw new Exception("Incomplete download, "+fileLength+"/"
                    +entity.getContentLength()+" bytes downloaded");

        }

But it seems that the exception is never triggered. How to properly handle this? Is entity.getContentLength the length of the file on server or the amount of data received?

  • 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-06T14:06:29+00:00Added an answer on June 6, 2026 at 2:06 pm

    The file request should always come with a MD5 checksum. If you have an MD5 header then all you need to do is check that against the files generated MD5. Then your done, its better to do it this way as you can have a file with the same number of bytes but one byte gets garbled in transmission.

            entity.writeTo(new FileOutputStream(outfile));
            String md5 = response.getHeaders("Content-MD5")[0].getValue();
            byte[] b64 = Base64.decode(md5, Base64.DEFAULT);
            String sB64 = IntegrityUtils.toASCII(b64, 0, b64.length);
            if (outfile.exists()) {
                String orgMd5 = null;
                try {
                    orgMd5 = IntegrityUtils.getMD5Checksum(outfile);
                } catch (Exception e) {
                        Log.d(TAG,"Exception in file hex...");
                }
                if (orgMd5 != null && orgMd5.equals(sB64)) {
                    Log.d(TAG,"MD5 is equal to files MD5");
                } else {
                    Log.d(TAG,"MD5 does not equal files MD5");
                }
            }
    

    Add this class to your project:

    public class IntegrityUtils {
    public static String toASCII(byte b[], int start, int length) {
        StringBuffer asciiString = new StringBuffer();
    
        for (int i = start; i < (length + start); i++) {
            // exclude nulls from the ASCII representation
            if (b[i] != (byte) 0x00) {
                asciiString.append((char) b[i]);
            }
        }
    
        return asciiString.toString();
    }
    
    public static String getMD5Checksum(File file) throws Exception {
        byte[] b = createChecksum(file);
        String result = "";
    
        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }
    
    public static byte[] createChecksum(File file) throws Exception {
        InputStream fis = new FileInputStream(file);
    
        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance("MD5");
        int numRead;
    
        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
    
        fis.close();
        return complete.digest();
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When downloading a file with HTTPUrlConnection is there a way to check if the
i am downloading an application in Android using the DownloadManager class. When the download
I am downloading an Android application to the path: /mnt/sdcard/download/App.apk using the DownloadManager class.
I'm trying to update the progress bar in a AsyncTask while downloading some stuff..
I'm working on a WPF download manager which should support downloading multiple files at
I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,
I own a website which contain a lot of freeware stuff to download on
I admit, I'm new at this whole Android stuff. I am trying to make
Downloading the Ninject.MVC3 package from NuGet creates this App_Start folder with a simple class
For downloading a number of images I'm making DownloadDataAsync calls to separate instances of

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.