---
title: "java获取网上图片保存到本地"
author: "dreamShow"
date: "2011-08-31T14:11:38Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-08-31T14:11:38Z"
id: 1162350
url: "https://dreamshow.iteye.com/blog/1162350"
---

```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();
		}
	}
}


```