日期:2014-05-20  浏览次数:20957 次

java怎么通过一个地址获取一张图片并把图片保存到本地磁盘里面
java怎么通过一个地址获取一张图片并把图片保存到本地磁盘里面

------解决方案--------------------
Java code
        URL url = new URL("http://rewalls.com/pic/201112/1920x1200/reWalls.com-57365.jpg");

        // 图片不是原来的了。
        ImageIO.write(ImageIO.read(url),"jpg",new File("image-01.jpg"));

        //
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
            input = new BufferedInputStream(url.openStream());
            output = new BufferedOutputStream(new FileOutputStream("/tmp/image-03.jpg"));
            for (int d = input.read(); d != -1; d = input.read()) {
                output.write(d);
            }
        } catch (Exception e) {

        } finally {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }

        // java 7
        try (InputStream in = url.openStream()) {
                Files.copy(in, Paths.get("/tmp","image-02.jpg"));
            } catch (Exception e) {

        }