`
dreamShow
  • 浏览: 10482 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

java获取网上图片保存到本地

    博客分类:
  • Java
 
阅读更多
package com.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;

public class DownloadImg {
	public static void main(String[] args) {

		try {
			URL url = new URL(
					"http://hiphotos.baidu.com/zmj%D1%BE%D1%BE/pic/item/36e2d2571e69da8ab645aef1.jpg");
			java.io.BufferedInputStream bis = new BufferedInputStream(url
					.openStream());
			byte[] bytes = new byte[100];
			OutputStream bos = new FileOutputStream(new File(
					"E:/36e2d2571e69da8ab645aef1.jpg"));
			int len;
			while ((len = bis.read(bytes)) > 0) {
				bos.write(bytes, 0, len);
			}
			bis.close();
			bos.flush();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics