cancel
Showing results for 
Search instead for 
Did you mean: 

A java program that isn't behaving as it should be... please help.

Former Member
0 Kudos

I am trying to get the following code to work but it isn't playing ball.

I basically want it to download the file from the following url:

http://www.ecb.int/stats/eurofxref/eurofxref.zip

But all this seems to do is create the empty zip...

Could someone please let me know why they think this is not downloading the actual zip?

import java.io.*;
import java.net.*;

/*
 * Command line program to download data from URLs and save
 * it to local files. Run like this:
 * java FileDownload http://schmidt.devlib.org/java/file-download.html
 * @author Marco Schmidt
 */
public class FileDownload {
	public static void download(String address, String localFileName) {
		OutputStream out = null;
		URLConnection conn = null;
		InputStream  in = null;
		try {
			URL url = new URL(address);
			out = new BufferedOutputStream(
				new FileOutputStream(localFileName));
			conn = url.openConnection();
			in = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int numRead;
			long numWritten = 0;
			while ((numRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, numRead);
				numWritten += numRead;
			}
			System.out.println(localFileName + "\t" + numWritten);
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException ioe) {
			}
		}
	}

	public static void download(String address) {
		int lastSlashIndex = address.lastIndexOf('/');
		if (lastSlashIndex >= 0 &&
		    lastSlashIndex < address.length() - 1) {
			download(address, address.substring(lastSlashIndex + 1));
		} else {
			System.err.println("Could not figure out local file name for " +
				address);
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			download(args<i>);
		}
	}
}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello Mendez,

it is hard to say what the problem is in your case, but my guess is that you are trying to download from some location where you don't normally connect to the internet directly but access URLs via a proxy server.

In that case you would have to replace the fourth line in the try-block with code like this


conn = url.openConnection(new Proxy(Proxy.Type.HTTP, 
                                    new InetSocketAddress("<proxyHostName>", <proxyPort>)));

where proxyHostName and proxyPort will have to be set to the values that apply to you.

Let me know if this works. If it doesn't, do you get any error message?

Regards,

Jens

Former Member
0 Kudos

hey yes this is correct I am sat behind a proxy.

Only we use the automatic configuration script option in IE.

http://proxy-np.came.corp/proxy-camenet-eu.pac

Could you please let me know how I would use this?

Thank you very much for your help.

Former Member
0 Kudos

>

> hey yes this is correct I am sat behind a proxy.

>

> Only we use the automatic configuration script option in IE.

>

> http://proxy-np.came.corp/proxy-camenet-eu.pac

>

> Could you please let me know how I would use this?

>

> Thank you very much for your help.

Well, or course I can't read that file, but if you can, then find the function FindProxyForURL(); that should return the host name and port of a proxy server.

Good luck and kind regards,

Jens