public class Dawnload{
public static void main(String[] args) throws Exception {
download("http://pic31.nipic.com/20130803/11960919_221307676104_2.jpg", "1.jpg","E:/picture/");
}
//param:图片下载地址,图片存储名,图片存放地址
public static void download(String urlString, String filename,String savePath) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection connection = url.openConnection();
// 设置请求超时为5s
con.setConnectTimeout(5*1000);
// 输入流
InputStream inputStream = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int length;
// 输出的文件流
File file=new File(savePath);
if(!file.exists()){
file.mkdirs();
}
OutputStream outputStream = new FileOutputStream(file.getPath()+"\\"+filename);
// 开始读取
while ((length = inputStream.read(bs)) != -1) {
os.write(bs, 0, length);
}
// 完毕,关闭所有链接
outputStream.close();
inputStream.close(); }
}