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();
}
}

