您的位置:首页 > 理论基础 > 计算机网络

网络资源(图片)下载到本地

2017-01-22 16:11 260 查看
headImgUrl = request.getSession().getAttribute("headImgUrl").toString();// 微信头像
// 把微信头像下载到本地
// 构造URL
if(headImgUrl != null){
URL imageUrl = new URL(headImgUrl);
// 打开连接
URLConnection con = imageUrl.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
File file = new File(this.fileService.getAbsPath()
+ "/upload/wechatCode/headImageUrl/");
if (!file.exists()) {
file.mkdirs();
}
// 输出的文件流
OutputStream os = new FileOutputStream(this.fileService.getAbsPath()
+ "/upload/wechatCode/headImageUrl/" + openid + ".jpg");
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息