I thank everyone in advance for looking at this and hopefully this is any easy question to answer. I am in the process of learning Java and found a cool piece of code on the internet from http://code.runnable.com/Uu83dm5vSScIAACw/download-a-file-from-the-web-for-java-files-and-save
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class DownloadFile2 {
public static void main(String[] args) throws IOException {
String fileName = "mapimg.jpg"; //The file that will be saved on your computer
URL link = new URL("http://maps.googleapis.com/maps/api/staticmap? center=44.667066,+-90.173632&zoom=13&scale=false&size=600x300&maptype=roa dmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Cl abel:0%7C44.667066,+-90.173632"); //The file that you want to download
//Code to download
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
//End download code
System.out.println("Finished");
}
}
Now this code runs great in the main method. What I am trying to do is put it in its own method and then call it from the main method like such:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class DownloadFile3 {
public static void main(String[] args)
{
getMap();
}
public static void getMap() throws IOException
{
String fileName = "mapimg.jpg"; //The file that will be saved on your computer
URL link = new URL("http://maps.googleapis.com/maps/api/staticmap? center=44.667066,+-90.173632&zoom=13&scale=false&size=600x300&maptype=roa dmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Cl abel:0%7C44.667066,+-90.173632"); //The file that you want to download
//Code to download
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
//End download code
System.out.println("Finished");
}
}
It will compile fine without the getMap(); in the main method, but when I call that method, I get the following compiler error, "DownloadFile3.java:13: error: unreported exception IOException; must be caught or declared to be thrown"
I have tried using the try and catch statements and have researched this for days. I'm really stumped on this one and I'm guessing it's probably glaring obvious to a more experienced programmer on here. How come I can get it to work fine in the main method, but in its own method it will not work and give me that error message? Am I calling the method incorrectly or what is going on? I appreciate any help on this.
You have a few choices here. Either...
public static void main(String[] args) throws IOException
{
getMap();
}
or
public static void main(String[] args)
{
try {
getMap();
} catch (IOException ioe) {
// do stuff
}
}
or catch and handle any exceptions in the getMap()
method itself. My personal choice would be the second or third option; there's something I don't like about having a main
with a throws
clause.