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
时间: 2024-08-03 20:05:26

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

java读取jpg图片旋转按比例缩放

1 //入口 2 public static BufferedImage constructHeatWheelView(int pageWidth, int pageHeight, DoubleHolder scaleHolder) throws ValidateException{ 3 4 BufferedImage bi = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_INT_RGB); 5 Graphics2D g

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

今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个网站,这个网站仅仅只是内部使用的,网站的内容就是我的另外一个程序(就叫A程序吧)的打包发布的文件放进去.然后在客户端启动我的A程序之前检查是否有新版本文件发布.如果有,我根据网页源代码的信息和本地文件信息进行比较,决定是否下载.如果有下载,下载完成后执行A程序的.exe文件启动A程序.大致的要求就是

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

1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作. 具体代码如下: package com.xuanen.util; import java.util.Properties; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; public class PropertyUtil {

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

<?phpheader("Content-type:text/html;charset=utf-8"); 请求的url $url = 'http://dsc.taobaocdn.com/i8/560/330/566337787959/TB1eUs_LlLoK1RjSZFu8qtn0Xla.desc%7Cvar%5Edesc%3Bsign%5Eeba34dfbbd144cadd988b77fa55a102e%3Blang%5Egbk%3Bt%5E1552268585'; 定义一个数

Java将网页上的js下载下来。

1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 import com.hanweb.common.util.File

Java 读取网页源代码

package com.sphere.service; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class QueryService { /** * 发起http get请

java实现从url路径中下载pdf文档到本地

package com.cellstrain.icell.util; import java.io.*;import java.net.*; public class DownloadPdf { /** * 从网络Url中下载文件 * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static void downLoadByUrl(String urlStr,String fil

java读取网页

package cn.stat.p4.ipdemo; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class iedemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String url_str=

Android 4.4从图库选择图片,获取图片路径并裁剪

转自:http://blog.csdn.net/tempersitu/article/details/20557383 最近在做一个从图库选择图片或拍照,然后裁剪的功能.本来是没问题的,一直在用 [java] view plaincopy Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 的方式来做,是调用系统图库来做,但是发现如