Downloading and storing images

So, I'm using the following method to grab images from the web and then store them on my computer. I found the code online and I just barely understand how it works. The problem (for me) is that is stores the image in the working directory, and i need it to go to a specified file. Is there a way, with this code, to specify a file where the image will be saved?

Thanks in advance!

import java.io.*;

import java.net.*;

publicclass image

{

publicstaticvoid imageDL(String imageURL)

throws MalformedURLException,IOException,FileNotFoundException

{

OutputStream out =null;

URLConnection conn =null;

InputStream in =null;

int imgNameIndex = imageURL.lastIndexOf('/');

if (imgNameIndex >= 0 && imgNameIndex < imageURL.length() - 1)

{

try{

URL url =new URL(imageURL);

out =new BufferedOutputStream(new FileOutputStream(imageURL.substring(imgNameIndex + 1)));

conn = url.openConnection();

in = conn.getInputStream();

byte[] buffer =newbyte[1024];

int numRead;

long numWritten = 0;

while ((numRead = in.read(buffer)) != -1)

{

out.write(buffer, 0, numRead);

numWritten += numRead;

}

System.out.println(imageURL.substring(imgNameIndex + 1) +"\t" + numWritten);

}

catch (Exception exception)

{

exception.printStackTrace();

}

}

else

{

System.err.println("Could not figure out local file name for " + imageURL);

}

in.close();

out.close();

}

}

[2893 byte] By [monk_ha] at [2007-11-27 9:01:43]
# 1
http://java.sun.com/javase/6/docs/api/java/io/FileOutputStream.html
BigDaddyLoveHandlesa at 2007-7-12 21:31:37 > top of Java-index,Java Essentials,Java Programming...
# 2

replace

out = new BufferedOutputStream(new FileOutputStream(imageURL.substring(imgNameIndex + 1)));

with

out = new BufferedOutputStream(new FileOutputStream("path_of_file"));

pbulgarellia at 2007-7-12 21:31:37 > top of Java-index,Java Essentials,Java Programming...
# 3

Hey guys, thanks for the quick replies.

But i'm confused, if I choose a filepath does that take away my ability to name the file. Because as it is, the image is downloaded and named the same as it is on the web. But it seems like the previous suggestion eliminates that naming convention...or am I totally mistaken?

Thanks

monk_ha at 2007-7-12 21:31:37 > top of Java-index,Java Essentials,Java Programming...
# 4
> But i'm confused, if I choose a filepath does that take away my ability to name the file.What have you tried? What problems have you encountered?Don't post a question unless you have tried it.
camickra at 2007-7-12 21:31:37 > top of Java-index,Java Essentials,Java Programming...
# 5

Forget the name of the file in the web.

You get the binary data and save it locally with any name you want.

Use it to delete any file that exists with the same name you choose, before you try to save

File file = new File("image.ext");

if (file.exists())

file.delete();

file.createNewFile();

good luck

pbulgarellia at 2007-7-12 21:31:37 > top of Java-index,Java Essentials,Java Programming...