用java将网络上一张图片或一个mp3或一个视频的信息保存到本地

解决方案 »

  1.   


    public static void main(String[] args) throws Exception {
    save("http://i0.sinaimg.cn/dy/2012/0817/U7351P1DT20120817150912.jpg",
    "./temp.jpg");
    } public static void save(String path, String fileName) throws Exception {
    URL url = new URL(path);
    OutputStream out = new FileOutputStream(new File(fileName));
    InputStream input = url.openStream();
    byte[] buf = new byte[1024 * 512];
    int temp = 0;
    while ((temp = input.read(buf)) != -1) {
    out.write(buf, 0, temp);
    }
    input.close();
    out.close();
    }