java读取网页图片路径并下载到本地

最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且

将图片的路径保存到数据库,示例代码如下:

package com.cellstrain.icell.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date; public class DownloadImage { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
download("https://www.mybiosource.com/images/tds/protocol_images/1000000-6999999/MBS2031060_SDS.jpg","E:\\upload\\SDSPage");
} public static String download(String urlPath,String savePath) throws Exception {
// 构造URL
URL url = new URL(urlPath);
// 打开连接
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(5*1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf=new File(savePath);
if(!sf.exists()){
sf.mkdirs();
}
int randomNo=(int)(Math.random()*1000000);
String filename=urlPath.substring(urlPath.lastIndexOf("/")+1,urlPath.length());//获取服务器上图片的名称
filename=new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+randomNo+filename;//时间+随机数防止重复
OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
String virtualPath="/upload/SDSPage/"+filename;//存入数据库的虚拟路径
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
return virtualPath;
} }
在E盘的upload文件下的SDSPage文件夹下就可以看到下载好的图片,将路径存到数据库后直接用el表达式就可以显示图片了,前提
是tomcat得配置好虚拟路径哦,如果不清楚怎么配置虚拟路径的可以查看http://www.cnblogs.com/qianzf/p/6781143.html
原文博客的链接地址:https://cnblogs.com/qzf/

java读取网页图片路径并下载到本地的更多相关文章

  1. 通过CURL抓取页面中的图片路径并下载到本地

    1.首页是图片处理页面downpic.php <?phpfunction getImage($url,$filename="") { if($url=="" ...

  2. 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件

    今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...

  3. Java Springboot 根据图片链接生成图片下载链接 及 多个图片打包zip下载链接

    现有一些图片在服务器上的链接,在浏览器中打开这些链接是直接显示在浏览器页面的形式. 现在需要生成这些图片的单独下载以及打包下载链接,即在浏览器中打开下载链接后弹出下载框提示下载.由于前端存在跨域问题, ...

  4. JAVA-替换html中图片的路径-从html代码中提取图片路径并下载(完整版)

    transHtml方法实现提取网络图片中得图片路径,将其重新下载到本地,并替换html中原来得路径 package com.googosoft.until; import java.io.Buffer ...

  5. java读取项目根路径下和任意磁盘位置下的properties文件

    1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作. 具体代码如下: package com.xuanen.util; import java.util.Properties ...

  6. php动态获取网页图片路径~

    <?phpheader("Content-type:text/html;charset=utf-8"); 请求的url $url = 'http://dsc.taobaocd ...

  7. JAVA 导出 Excel, 将Excel下载到本地

    昨天说了如何将数据导出成 excel 的两种方式,今天完善一下将 java 导出(文件在服务器)并下载到本地 1. 触发导出 ajax 代码 $.ajax({ type: "POST&quo ...

  8. Java已知图片路径下载图片到本地

    public static void main(String[] args) { FileOutputStream fos = null; BufferedInputStream bis = null ...

  9. java 读取文件的路径

    1. 通用定位到用户目录下:   String userDir = System.getProperty("user.dir"); 2. web项目定位到WEB-INF/class ...

随机推荐

  1. docker 带参数启动 配合springboot profile

    dockerfile FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD test-push-service--SNAPSHOT.jar app. ...

  2. TP5常量

    预定义常量 EXT 类库文件后缀(.php) THINK_VERSION 框架版本号 路径常量 DS 当前系统的目录分隔符 THINK_PATH 框架系统目录 ROOT_PATH 框架应用根目录 AP ...

  3. mybatis - maven - eclipse 坑爹问题: No suitable driver found for http://maven.apache.org

    坑爹的问题,调查了1天 一直以为是驱动问题,根源却在url上:No suitable driver found for http://maven.apache.org 根源: 1.在jdbc.prop ...

  4. 创建类type (底层代码)

    类的创建时是用type 实现的 def __init__(self, name): self.name = name def fun(self): print("%s is talking' ...

  5. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  6. 10 python 封装----@property的用法

    1.基本概念 在python中用双下划线开头的方式将属性隐藏起来(设置成私有的) #其实这仅仅这是一种变形操作 #类中所有双下划线开头的名称如__x都会自动变形成:_类名__x的形式: class A ...

  7. ABAP-Generate subroutine

    1.定义 data:zprog like abapsource occurs with header line, prog() type c, msg() type c. 2.动态语句 zprog-l ...

  8. eclipse从svn导入maven项目变成普通项目解决办法

    右击项目-->configure-->Convert to Maven Project

  9. arguments.callee 属性 递归调用 & caller和callee的区别

    arguments.callee   在函数内部,有两个特殊的对象:arguments 和 this.其中, arguments 的主要用途是保存函数参数, 但这个对象还有一个名叫 callee 的属 ...

  10. 树莓派3用create_ap变身无线AP

    1.git clone https://github.com/oblique/create_ap.git2.cd create_ap3.sudo make install就这样安装好了4.接下来安装依 ...