代码在主方法中运行良好,但是当我尝试给出其自己的方法时,在调用该方法时,出现关于IOException的编译器错误

cody_e

我预先感谢每个人对此的关注,希望这是一个容易回答的问题。我正在学习Java,并且在http://code.runnable.com/Uu83dm5vSScIAACw/download-a-file-from-the-web-for-java-files-和互联网上找到了一段不错的代码-保存

我对其进行了修改,以基本上调用Google静态地图,然后将其另存为jpg。使用此代码,它可以完美地工作

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");

    }

}

现在,这段代码可以在main方法中很好地运行。我想做的是将其放在自己的方法中,然后从主要方法中调用它,例如:

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");

    }

}

如果没有getMap(),它将编译良好。在主方法中,但是当我调用该方法时,出现以下编译器错误,“ DownloadFile3.java:13:错误:未报告的异常IOException;必须捕获或声明为抛出”

我已经尝试过使用try和catch语句,并且已经对此进行了数天的研究。我真的很为难,而我猜想这对于这里的一个更有经验的程序员可能是显而易见的。我怎么能使它在main方法中正常工作,但在它自己的方法中却无法工作,并给我该错误消息?我是错误地调用该方法还是发生了什么事?我对此表示感谢。

ChiefTwo铅笔

您在这里有几种选择。任何一个...

public static void main(String[] args) throws IOException
{
    getMap();
}

或者

public static void main(String[] args)
{
    try {
        getMap();
    } catch (IOException ioe) {
        // do stuff
    }
}

或捕获并处理getMap()方法本身中的任何异常我个人的选择将是第二或第三选择。我不喜欢关于mainwiththrows子句。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章