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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:34:56+00:00 2026-06-02T19:34:56+00:00

Some PHP sites use a page to act as a middle man for handling

  • 0

Some PHP sites use a page to act as a middle man for handling file downloads.

With a browser this works transparently. There seems to a be a slight pause while the php page processes the request.

However, attempting a download through Java using a URL or HttpURLConnection returns a plain html page. How could I get the file downloads working in the same way?

Edit: Here is an example link:

http://depot.eice.be/index.php?annee_g=jour&cours=poo

Edit: Here is some of the code I’ve been testing:

// This returns an HTML page

private void downloadURL(String theURL) {
    URL url;
    InputStream is = null;
    DataInputStream dis;
    String s;
    StringBuffer sb = new StringBuffer();

    try {
        url = new URL(theURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        conn.connect();

        if (conn.getResponseCode()!=HttpURLConnection.HTTP_OK)
            return;


        InputStream in = conn.getInputStream();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int i;
        while ((i = in.read()) != -1) {
            bos.write(i);
        }

        byte[] b = bos.toByteArray();

        FileOutputStream fos = new FileOutputStream( getNameFromUrl( theURL ) );
        fos.write(b);
        fos.close();
        conn.disconnect();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

// This will throw Exceptions if the URL isn't in the expected format

public String getNameFromUrl(String url) {

    int slashIndex = url.lastIndexOf('/');
    int dotIndex = url.lastIndexOf('.');

    System.out.println("url:" + url + "," + slashIndex + "," + dotIndex);

    if (dotIndex == -1) {
        return url.substring(slashIndex + 1);
    } else {
        try {
            return url.substring(slashIndex + 1, url.length());
        } catch (StringIndexOutOfBoundsException e) {
            return "";

        }
    }
}
  • 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-02T19:34:57+00:00Added an answer on June 2, 2026 at 7:34 pm

    I think I’ve found a solution using HttpUnit. The source of the framework is available if you wish to see how this is handled.

    public void downloadURL(String url) throws IOException {
    
        WebConversation wc = new WebConversation();
        WebResponse indexResp = wc.getResource(new GetMethodWebRequest(url));
        WebLink[] links = new WebLink[1];
        try {
            links = indexResp.getLinks();
        } catch (SAXException ex) {
            // Log
        }
    
        for (WebLink link : links) {
            try {
                link.click();
            } catch (SAXException ex) {
                // Log
            }
            WebResponse resp = wc.getCurrentPage();
            String fileName = resp.getURL().getFile();
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
            System.out.println("filename:" + fileName);
            File file = new File(fileName);
            BufferedInputStream bis = new BufferedInputStream(
                    resp.getInputStream());
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(file.getName()));
            int i;
            while ((i = bis.read()) != -1) {
                bos.write(i);
            }
            bis.close();
            bos.close();
        }
        System.out.println("Done downloading.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use a custom 404 page in a PHP application. On that 404 page,
After looking through some sites (eg https://www.owasp.org/index.php/Main_Page ) I found no EXPLICIT mention of
I did some looking around at how other sites might handle this and didn't
I have confused for this login method, especially for different access page for some
In one of my sites, I need to use PHP 'foreach' to keep including
So I'm doing some maintenance on a PHP site that is using $_SESSION variables.
I some php code which gets info from a mysql database. The problem is
I have some PHP code that generates dynamic tables of data on the fly.
So I have some PHP code that looks like: $message = 'Here is the
I have some PHP code that is designed to make a spreadsheet with formulas

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.