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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:30:16+00:00 2026-06-17T11:30:16+00:00

I want to download a HTTP query with java, but the file I download

  • 0

I want to download a HTTP query with java, but the file I download has an undetermined length when downloading.

I thought this would be quite standard, so I searched and found a code snippet for it: http://snipplr.com/view/33805/

But it has a problem with the contentLength variable. As the length is unknown, I get -1 back. This creates an error. When I omit the entire check about contentLength, that means I always have to use the maximum buffer.

But the problem is that the file is not ready yet. So the flush gets only partially filled, and parts of the file get lost.

If you try downloading a link like http://overpass-api.de/api/interpreter?data=area%5Bname%3D%22Hoogstade%22%5D%3B%28++node%28area%29%3B++%3C%3B%29+%3Bout+meta+qt%3B with that snippet, you’ll notice the error, and when you always download the maximum buffer to omit the error, you end up with a corrupt XML file.

Is there some way to only download the ready part of the file? I would like if this could download big files (up to a few GB).

  • 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-17T11:30:17+00:00Added an answer on June 17, 2026 at 11:30 am

    This should work, i tested it and it works for me:

    void downloadFromUrl(URL url, String localFilename) throws IOException {
        InputStream is = null;
        FileOutputStream fos = null;
    
        try {
            URLConnection urlConn = url.openConnection();//connect
    
            is = urlConn.getInputStream();               //get connection inputstream
            fos = new FileOutputStream(localFilename);   //open outputstream to local file
    
            byte[] buffer = new byte[4096];              //declare 4KB buffer
            int len;
    
            //while we have availble data, continue downloading and storing to local file
            while ((len = is.read(buffer)) > 0) {  
                fos.write(buffer, 0, len);
            }
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }
        }
    }
    

    If you want this to run in background, simply call it in a Thread:

    Thread download = new Thread(){
        public void run(){
            URL url= new URL("http://overpass-api.de/api/interpreter?data=area%5Bname%3D%22Hoogstade%22%5D%3B%0A%28%0A++node%28area%29%3B%0A++%3C%3B%0A%29+%3B%0Aout+meta+qt%3B");
            String localFilename="mylocalfile"; //needs to be replaced with local file path
            downloadFromUrl(url, localFilename);
        }
    };
    download.start();//start the thread
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to download some files from this site: http://www.emuparadise.me/soundtracks/highquality/index.php But I only want
I want to download a .zip file which is available at URL http://www.nseindia.com/content/equities/cmbhav.htm by
I want to download this picture using Ruby. How do I do that? http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on
I am using this plugin http://mods.mybb.com/download/online-today-1.2.2 the below part is query $queries[] = $db->simple_select(
I want to download this project: http://trac.sitecore.net/AdvancedDatabaseCrawler/browser/Branches/v2/ How should I download the whole project?
I want to download http://www.wordwebonline.com/search.pl?w=humane this webpage on UNIX. I tried to use wget
I want to download a large file and concurrently handle other things. However, luasocket.http
I want to download a music file by this way: require 'open-uri' source_url =
I want to download a file directly from a url (for example: http://iranfairco.com/icon/Iranfairco.png )

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.