1、保存至服务器

  根据路径保存至项目所在服务器上。

         String imgUrl="";//图片地址
try {
// 构造URL
URL url = new URL(imgUrl);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream("c:\\image.jpg");//保存路径
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

2、保存至本地

  以浏览器下载的方式保存至本地。

         String imgUrl="";//URL地址
String fileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1);
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
URL url = new URL(imgUrl);
this.getServletResponse().setContentType("application/x-msdownload;");
this.getServletResponse().setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
this.getServletResponse().setHeader("Content-Length", String.valueOf(url.openConnection().getContentLength()));
is = new BufferedInputStream(url.openStream());
os = new BufferedOutputStream(this.getServletResponse().getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = is.read(buff, 0, buff.length))) {
os.write(buff, 0, bytesRead);
}
if (is != null)
is.close();
if (os != null)
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

根据URL下载图片至客户端、服务器实例的更多相关文章

  1. python实现通过URL下载图片到本地服务器

    import os import urllib.request image_url = 'http://img.jingtuitui.com/759fa20190115144450401.jpg' f ...

  2. C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...

  3. 从url下载图片--java与python实现方式比较

    从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载  一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...

  4. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  5. js 根据url 下载图片

    downloadIamge(imgsrc, name) {//下载图片地址和图片名 let image = new Image(); // 解决跨域 Canvas 污染问题 image.setAttr ...

  6. js 根据url 下载图片 前端js 实现文件下载

    1.H5 download属性 function downFile(content, filename) { // 创建隐藏的可下载链接 var eleLink = document.createEl ...

  7. QT通过url下载图片到本地

    /* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...

  8. 根据URL地址获取对应的HTML,根据对应的URL下载图片

    核心代码(获取HTML): #region 根据URL地址获取信息GET public static String GetResult(string url) { return GetResult(u ...

  9. 根据URL下载图片到本地

    /// <summary> /// 下载图片 /// </summary> /// <param name="picUrl">图片Http地址& ...

随机推荐

  1. 5分钟了解MySQL5.7的undo log在线收缩新特性

    Part1:写在最前 在MysQL5.6版本中,可以把undo log 回滚日志分离到一个单独的表空间里:其缺点是不能回收空间大小,until MysQL5.7,but MariadDB10.1暂不支 ...

  2. Lottie的使用

    一.简介 Lottie是Airbnb开源的一个面向IOS.Android.React Native的动画库,能分析Adobe After Effects导出的动画,并且能让原生App像使用静态素材一样 ...

  3. Django_'utf-8' codec can't decode 问题解决

    最近用vs2017新建django模板项目时,页面输入中文时导致编码错误,如下图: 几经排查,原来是对应的html文件保存的编码错误,重新用utf-8保存即可

  4. Python 解析构建数据大杂烩 -- csv、xml、json、excel

    Python 可以通过各种库去解析我们常见的数据.其中 csv 文件以纯文本形式存储表格数据,以某字符作为分隔值,通常为逗号:xml 可拓展标记语言,很像超文本标记语言 Html ,但主要对文档和数据 ...

  5. PIL遇到问题解决

    PIL 全称:Pillow 在使用PIL4.2.1版本读取jpeg文件时,报cannot identify image file,去github源查找原因:https://github.com/pyt ...

  6. SDWebImage 加载显示 WebP 与性能问题

    SDWebImage 加载显示 WebP 与性能问题 本文包含自定义下载操作 SDWebImageDownloaderOperation 与编码器 SDWebImageCoder.SDWebImage ...

  7. SpringMVC框架学习笔记(3)——controller配置汇总

    1.通过URL对应bean <!-- 配置handlerMapper 映射器 --> <bean class="org.springframework.web.servle ...

  8. 安装配置最强Spark IDE--IDEA

    1.安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示 ...

  9. happymall 第十一章订单表 数据表设计

    为订单号生成唯一索引,用用户id和订单号生成组合索引提高查询效率.

  10. Newbit 启用淘宝店域名

    自2016-10-19起,我们正式启用淘宝店的域名,newbit.taobao.com 店里提供所有课程当中用到硬件,ZigBee插件/贴片模块等, 我们将坚持给大家提供最具扩展性,最方便使用的开发工 ...