Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Saving a URL file to the right directory..help pls

807600Dec 1 2007 — edited Dec 1 2007
Java Gurus

The following code downloads a file from the internet and saves it to the same directory as my .class files.
What I want is to save the file in a FOLDER where my .class files are, pretty simple? I can't work out how to do it!

In the code I created a folder DOWNLOAD where I want the file to go, but currently the file is saved alongside the DOWNLOAD folder, not IN the DOWNLOAD folder.

How do I perform this simple task?

Thanks in advance for any help provided.

Jas
import java.io.*;
import java.net.*;
import java.lang.*;
import javax.swing.*;
/*
 * Program to download data from URLs and save it to locally
 */
public class FileDownload {
	public static void download(String address, String localFileName) {
		System.out.println("In Download 1");
		String Savehere = ("F:/JAVA1/DOWNLOAD/");

		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) {
		System.out.println("In Download 2");

		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) {
		System.out.println("START PROGRAM");
		
		// Create directory for downloaded files
		String strDirectory = "DOWNLOAD";
		boolean success = (new File(strDirectory)).mkdir();
		if (success) {
			System.out.println("Created Directory: " + strDirectory);
		}
		else {
			System.out.println("COULD NOT CREATE DIRECTORY");
		}

		// Download file from internet
		download("http://www.3dchem.com/imagesofmolecules/CyclosporinA.jpg");

		System.out.println("END PROGRAM");
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 29 2007
Added on Dec 1 2007
2 comments
159 views