如何实现自动缓存文章的图片?

前沿:我相信大家多多少少应该发过一些文章,或者写过一些博客。对于文章的图片各个平台是如何处理的呢。如:QQ空间会把所有文章的图片缓存到qq服务器上。那么如何实现这一功能呢?大家有没有思考过?

QQ图片20160708172547

好!下面我们带着问题来思考这个问题“如何实现自动缓存文章的图片?” 首先,自动就必须无人工去干预,必须程序自动检测文章是否包含图片。其次,缓存图片那就必须下载图片,下载图片如何防止爬虫!带着这个思路我们把整理称如下步骤

1.程序定时跑任务,如每30秒扫面一下文章里面是否又需要替换的图片(非本站图片)

2.解析文章里面的图片url(需要用到正则或者html解析工具,如jsoup)

3.自动下载到本地服务器上(需要网络连接相关工具包,对于某些网站,可能需要用过代理)

4.替换原文章里面的url(正则替换或者字符串替换)

1.程序定时跑任务

<bean id="downloadImageTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
 <property name="jobDetail" ref="downloadImageCron" />
 <property name="cronExpression" value="0/30 * * * * ?" /> 
 </bean>

2.解析文章里面的图片url

public void addImageUrl(Document doc,BsArticle bsarticle ){
 Elements elements = doc.select("div.article_body").select("img");
 for(Element element:elements){
 String imageUrl =element.attr("src");
 BsReplaceimage bsReplaceimage = new BsReplaceimage();
 bsReplaceimage.setOldImageurl(processsImage(imageUrl));
 bsReplaceimage.setProcessFlag(0);
 bsReplaceimage.setArticleId(bsarticle.getId());
 try {
 replaceImageService.addReplaceImage(bsReplaceimage);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 }

3.自动下载到本地服务器上

/**
* 下载图片
* 把从服务器获得图片的输入流InputStream写到本地磁盘
* @Method_Name : downImage
* @param urlString
* @param filename
* @param savePath
* @return : void
* @Creation Date : 2016年7月8日 上午11:06:25
* @version : v1.00
* @Author : liuhaihua
* @Update Date :
* @Update Author :
*/
public static String downImage(String oldurlString, String filename,String savePath) {
String newurlString ="";
InputStream inputStream = getInputStream(oldurlString);
byte[] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
String absolute_path ="/alidata1/ftp"+savePath;
File sf=new File(absolute_path);
if(!sf.exists()){
sf.mkdirs();
}
newurlString =savePath+"/"+filename+"."+parseSuffix(oldurlString);
fileOutputStream = new FileOutputStream(sf.getPath()+"/"+filename+"."+parseSuffix(oldurlString));

while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return newurlString;
}

/**
* 从服务器获得一个输入流(本例是指从服务器获得一个image输入流)
* @Method_Name : getInputStream
* @param URL_PATH
* @return
* @return : InputStream
* @Creation Date : 2016年7月8日 上午11:07:00
* @version : v1.00
* @Author : liuhaihua
* @Update Date :
* @Update Author :
*/
public static InputStream getInputStream(String URL_PATH) {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(URL_PATH);
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}

4.替换原文章里面的url

System.out.println("oldurl--------------------:"+oldurl);
System.out.println("newurl--------------------:"+newurl);
String newcontent = content.replace(oldurl, newurl);

PS:如果您想和业内技术大牛交流的话,请加qq群(521249302)或者关注微信公众 号(AskHarries),谢谢!

转载于:https://my.oschina.net/liuhaihua/blog/708637

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值