根据图片url写入图片到制定位置

来源:互联网 发布:广州小孩学编程 编辑:程序博客网 时间:2024/06/05 12:38
package com.file;


import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;


public class URLDownload {

public static void download(String urlString, String filename) throws Exception {
// 构造URL
   URL url = new URL(urlString);
   // 打开连接
   URLConnection con = url.openConnection();
   // 输入流
   InputStream is = con.getInputStream();
   // 1K的数据缓冲
   byte[] bs = new byte[1024];
   // 读取到的数据长度
   int len;
   // 输出的文件流
   OutputStream os = new FileOutputStream(filename);
   // 开始读取
   while ((len = is.read(bs)) != -1) {
     os.write(bs, 0, len);
   }
   // 完毕,关闭所有链接
   os.close();
   is.close();
}

public static void main(String[] args) throws Exception {
download("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg","D:\\aa\\a.jpg");
}
}
0 0
原创粉丝点击