package com.cms.server.third.viva.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cms.server.dao.ContentArticleDao;
import com.cms.server.dao.ContentTextDao;
import com.cms.server.domain.ContentArticle;
import com.cms.server.domain.ContentText;
import com.cms.server.util.CommonUtil;
import com.cms.server.util.MD5Util;
import com.cms.server.util.StaticUtil;
/**
* 文章处理类
* @author kosei
*
*/
@Service
public class ContentAriticleService {
private Logger logger = Logger.getLogger(getClass());// 日志
@Autowired
private ContentArticleDao contentArticleDao;
@Autowired
private ContentTextDao contentTextDao;
@Autowired
private CommonUtil commonUtil;
@Value("${apache_img_url}")
private String apacheImgUrl;
@Value("${apache_img_base}")
private String apacheImgBase;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("unused")
@Transactional
public String saveAndUpdateContentAriticle(String jsonString) throws Exception{
ObjectMapper mapper = new ObjectMapper();
JsonNode nameNode = mapper.readValue(jsonString, JsonNode.class);
String feedlistImg ="";
//获取结点data的集合
//JsonNode nameNode =rootNode.path("data");
Long articleId = Long.parseLong(nameNode.get("article_id").asText());
String articleSourceUrl = nameNode.get("article_source_url").asText();
String articleTitle = nameNode.get("article_title").asText();
JsonNode imgList = nameNode.get("feedlist_img");
String sourceTitle = nameNode.get("source_title").asText();
String articleContent = nameNode.get("article_content").asText();
Date articleTime =format.parse(nameNode.get("article_time").asText());
System.out.println(articleTime);
Date updateTime = format.parse(nameNode.get("update_time").asText());
Long templateId = Long.parseLong(nameNode.get("template_id").asText());
String providerId = nameNode.get("provider_id").asText();
String providerName = nameNode.get("provider_name").asText();
String sortId = nameNode.get("sort_id").asText();
String sourceType = nameNode.get("article_type").asText();
Integer manualRecommend = Integer.parseInt(nameNode.get("recommend").asText());
Long magazineId = Long.parseLong(nameNode.get("magazine_id").asText());
String contentBody = updateHtmlTag(articleContent,"img","src",articleId);
for(int i = 0;i<imgList.size();i++){
String url = imgList.get(i).asText();
String lastIndex = url.substring(url.lastIndexOf("."), url.length());
getImageByUrl(url,articleId);
url = getImgUrl(url,articleId).append(MD5Util.string2MD5(url)).toString()+lastIndex;
feedlistImg+=url+",";
}
feedlistImg = feedlistImg.substring(0, feedlistImg.length()-1);
ContentArticle contentArticle = null;
ContentText contentText = null;
if(magazineId!=null){
List<ContentArticle> articleList = contentArticleDao.findContentArticleByMagazineId(magazineId);
List<ContentText> textList = new ArrayList<ContentText>();
if(articleList.size()>0){
for(int i=0; i<articleList.size();i++){
Long artId =articleList.get(i).getArticleId();
if(artId!=null&&!"".equals(artId)){
ContentText context = contentTextDao.findOne(artId);
if(context!=null){
textList.add(context);
}
}
}
if(textList.size()>0){
contentTextDao.delete(textList);
}
contentArticleDao.delete(articleList);
}
}
if(articleId!=null){
contentArticle = contentArticleDao.findOne(articleId);
contentText = contentTextDao.findOne(articleId);
}else{
articleId=commonUtil.get18TimeAndRandom();
System.out.println(articleId);
}
if(contentArticle!=null){
contentArticle.setArticleTitle(articleTitle);
contentArticle.setArticleSourceUrl(articleSourceUrl);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setFeedlistImg(feedlistImg);
contentArticle.setSourceTitle(sourceTitle);
contentArticle.setUpdateTime(updateTime);
contentArticle.setArticleTime(articleTime);
contentArticle.setTemplateId(templateId);
contentArticle.setProviderId(providerId);
contentArticle.setProviderName(providerName);
contentArticle.setSortId(sortId);
contentArticle.setSourceType(sourceType);
contentArticle.setManualRecommend(manualRecommend);
contentArticle.setMagazineId(magazineId);
}else{
contentArticle = new ContentArticle();
contentArticle.setArticleId(articleId);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setArticleSourceUrl(articleSourceUrl);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setFeedlistImg(feedlistImg);
contentArticle.setSourceTitle(sourceTitle);
contentArticle.setUpdateTime(updateTime);
contentArticle.setArticleTime(articleTime);
contentArticle.setTemplateId(templateId);
contentArticle.setProviderId(providerId);
contentArticle.setProviderName(providerName);
contentArticle.setSortId(sortId);
contentArticle.setSourceType(sourceType);
contentArticle.setManualRecommend(manualRecommend);
contentArticle.setMagazineId(magazineId);
}
if(contentText!=null){
contentText.setArticleContent(contentBody);
}else{
contentText = new ContentText();
contentText.setArticleId(articleId);
contentText.setArticleContent(contentBody);
}
try{
contentArticleDao.save(contentArticle);
contentTextDao.save(contentText);
return "success";
}catch(Exception e){
return "fail";
}
}
//return jsonString;
/**
* 根据图片网上url创建图片文件路径
*/
public StringBuffer getImgUrl(String imgurl,Long articleId){
StringBuffer url = new StringBuffer(apacheImgUrl)
.append(StaticUtil.URL_)
.append(commonUtil.nowDate()).append(StaticUtil.URL_).append(commonUtil.getThree(articleId))
.append(StaticUtil.URL_).append(articleId).append(StaticUtil.URL_);
return url;
}
/**
* 根据文章url从网上获取图片
* @param imgUrl :网上图片url
* @param newUrl :下载在本地的路径
* @throws Exception
*/
public void getImageByUrl(String imgUrl,Long articleId) throws Exception{
URL url = new URL(imgUrl);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
String lastIndex = imgUrl.substring(imgUrl.lastIndexOf("."), imgUrl.length());
//new一个文件对象用来保存图片
StringBuffer newImgUrl =new StringBuffer(apacheImgBase).append(File.separator)
.append(commonUtil.nowDate()).append(File.separator).append(commonUtil.getThree(articleId))
.append(File.separator).append(articleId).append(File.separator).append(MD5Util.string2MD5(imgUrl)).append(lastIndex);
File imageFile = new File(newImgUrl.toString());
if(!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
if(!imageFile.exists()){
imageFile.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
}
public byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
/**
* @param htmlStr html文本
* @param searchTag 要修改的目标标签
* @param searchAttrib 目标标签中的属性
* @param newStr 修改值
*/
public String updateHtmlTag(String htmlStr, String searchTag,
String searchAttrib,Long articleId) {
String regxpForTag ="<\\s*" + searchTag + "\\s+([^>]*)\\s*>"; //获取需要替换的标签
String regxpForTagAttrib = searchAttrib + "\\s*=\\s*[\"|']([^\"|']+)[\"|']"; //获取需要替换的属性
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(htmlStr);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer("<"+searchTag +" ");
// System.out.println(matcherForTag.group(1));
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
System.out.println(matcherForAttrib.group(1));
String url = matcherForAttrib.group(1);
String lastIndex = url.substring(url.lastIndexOf("."), url.length());
url = getImgUrl(url,articleId).append(MD5Util.string2MD5(url)).toString()+lastIndex; //url路径拼接
matcherForAttrib.appendReplacement(sbreplace, searchAttrib+"=\""+url+"\"");
}
// matcherForTag.appendReplacement(sb, sbreplace.toString());
matcherForAttrib.appendTail(sbreplace);
matcherForTag.appendReplacement(sb, sbreplace.toString()+">");
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
System.out.println(sb.toString());
return sb.toString();
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cms.server.dao.ContentArticleDao;
import com.cms.server.dao.ContentTextDao;
import com.cms.server.domain.ContentArticle;
import com.cms.server.domain.ContentText;
import com.cms.server.util.CommonUtil;
import com.cms.server.util.MD5Util;
import com.cms.server.util.StaticUtil;
/**
* 文章处理类
* @author kosei
*
*/
@Service
public class ContentAriticleService {
private Logger logger = Logger.getLogger(getClass());// 日志
@Autowired
private ContentArticleDao contentArticleDao;
@Autowired
private ContentTextDao contentTextDao;
@Autowired
private CommonUtil commonUtil;
@Value("${apache_img_url}")
private String apacheImgUrl;
@Value("${apache_img_base}")
private String apacheImgBase;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("unused")
@Transactional
public String saveAndUpdateContentAriticle(String jsonString) throws Exception{
ObjectMapper mapper = new ObjectMapper();
JsonNode nameNode = mapper.readValue(jsonString, JsonNode.class);
String feedlistImg ="";
//获取结点data的集合
//JsonNode nameNode =rootNode.path("data");
Long articleId = Long.parseLong(nameNode.get("article_id").asText());
String articleSourceUrl = nameNode.get("article_source_url").asText();
String articleTitle = nameNode.get("article_title").asText();
JsonNode imgList = nameNode.get("feedlist_img");
String sourceTitle = nameNode.get("source_title").asText();
String articleContent = nameNode.get("article_content").asText();
Date articleTime =format.parse(nameNode.get("article_time").asText());
System.out.println(articleTime);
Date updateTime = format.parse(nameNode.get("update_time").asText());
Long templateId = Long.parseLong(nameNode.get("template_id").asText());
String providerId = nameNode.get("provider_id").asText();
String providerName = nameNode.get("provider_name").asText();
String sortId = nameNode.get("sort_id").asText();
String sourceType = nameNode.get("article_type").asText();
Integer manualRecommend = Integer.parseInt(nameNode.get("recommend").asText());
Long magazineId = Long.parseLong(nameNode.get("magazine_id").asText());
String contentBody = updateHtmlTag(articleContent,"img","src",articleId);
for(int i = 0;i<imgList.size();i++){
String url = imgList.get(i).asText();
String lastIndex = url.substring(url.lastIndexOf("."), url.length());
getImageByUrl(url,articleId);
url = getImgUrl(url,articleId).append(MD5Util.string2MD5(url)).toString()+lastIndex;
feedlistImg+=url+",";
}
feedlistImg = feedlistImg.substring(0, feedlistImg.length()-1);
ContentArticle contentArticle = null;
ContentText contentText = null;
if(magazineId!=null){
List<ContentArticle> articleList = contentArticleDao.findContentArticleByMagazineId(magazineId);
List<ContentText> textList = new ArrayList<ContentText>();
if(articleList.size()>0){
for(int i=0; i<articleList.size();i++){
Long artId =articleList.get(i).getArticleId();
if(artId!=null&&!"".equals(artId)){
ContentText context = contentTextDao.findOne(artId);
if(context!=null){
textList.add(context);
}
}
}
if(textList.size()>0){
contentTextDao.delete(textList);
}
contentArticleDao.delete(articleList);
}
}
if(articleId!=null){
contentArticle = contentArticleDao.findOne(articleId);
contentText = contentTextDao.findOne(articleId);
}else{
articleId=commonUtil.get18TimeAndRandom();
System.out.println(articleId);
}
if(contentArticle!=null){
contentArticle.setArticleTitle(articleTitle);
contentArticle.setArticleSourceUrl(articleSourceUrl);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setFeedlistImg(feedlistImg);
contentArticle.setSourceTitle(sourceTitle);
contentArticle.setUpdateTime(updateTime);
contentArticle.setArticleTime(articleTime);
contentArticle.setTemplateId(templateId);
contentArticle.setProviderId(providerId);
contentArticle.setProviderName(providerName);
contentArticle.setSortId(sortId);
contentArticle.setSourceType(sourceType);
contentArticle.setManualRecommend(manualRecommend);
contentArticle.setMagazineId(magazineId);
}else{
contentArticle = new ContentArticle();
contentArticle.setArticleId(articleId);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setArticleSourceUrl(articleSourceUrl);
contentArticle.setArticleTitle(articleTitle);
contentArticle.setFeedlistImg(feedlistImg);
contentArticle.setSourceTitle(sourceTitle);
contentArticle.setUpdateTime(updateTime);
contentArticle.setArticleTime(articleTime);
contentArticle.setTemplateId(templateId);
contentArticle.setProviderId(providerId);
contentArticle.setProviderName(providerName);
contentArticle.setSortId(sortId);
contentArticle.setSourceType(sourceType);
contentArticle.setManualRecommend(manualRecommend);
contentArticle.setMagazineId(magazineId);
}
if(contentText!=null){
contentText.setArticleContent(contentBody);
}else{
contentText = new ContentText();
contentText.setArticleId(articleId);
contentText.setArticleContent(contentBody);
}
try{
contentArticleDao.save(contentArticle);
contentTextDao.save(contentText);
return "success";
}catch(Exception e){
return "fail";
}
}
//return jsonString;
/**
* 根据图片网上url创建图片文件路径
*/
public StringBuffer getImgUrl(String imgurl,Long articleId){
StringBuffer url = new StringBuffer(apacheImgUrl)
.append(StaticUtil.URL_)
.append(commonUtil.nowDate()).append(StaticUtil.URL_).append(commonUtil.getThree(articleId))
.append(StaticUtil.URL_).append(articleId).append(StaticUtil.URL_);
return url;
}
/**
* 根据文章url从网上获取图片
* @param imgUrl :网上图片url
* @param newUrl :下载在本地的路径
* @throws Exception
*/
public void getImageByUrl(String imgUrl,Long articleId) throws Exception{
URL url = new URL(imgUrl);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
String lastIndex = imgUrl.substring(imgUrl.lastIndexOf("."), imgUrl.length());
//new一个文件对象用来保存图片
StringBuffer newImgUrl =new StringBuffer(apacheImgBase).append(File.separator)
.append(commonUtil.nowDate()).append(File.separator).append(commonUtil.getThree(articleId))
.append(File.separator).append(articleId).append(File.separator).append(MD5Util.string2MD5(imgUrl)).append(lastIndex);
File imageFile = new File(newImgUrl.toString());
if(!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
if(!imageFile.exists()){
imageFile.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
}
public byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
/**
* @param htmlStr html文本
* @param searchTag 要修改的目标标签
* @param searchAttrib 目标标签中的属性
* @param newStr 修改值
*/
public String updateHtmlTag(String htmlStr, String searchTag,
String searchAttrib,Long articleId) {
String regxpForTag ="<\\s*" + searchTag + "\\s+([^>]*)\\s*>"; //获取需要替换的标签
String regxpForTagAttrib = searchAttrib + "\\s*=\\s*[\"|']([^\"|']+)[\"|']"; //获取需要替换的属性
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(htmlStr);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer("<"+searchTag +" ");
// System.out.println(matcherForTag.group(1));
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
System.out.println(matcherForAttrib.group(1));
String url = matcherForAttrib.group(1);
String lastIndex = url.substring(url.lastIndexOf("."), url.length());
url = getImgUrl(url,articleId).append(MD5Util.string2MD5(url)).toString()+lastIndex; //url路径拼接
matcherForAttrib.appendReplacement(sbreplace, searchAttrib+"=\""+url+"\"");
}
// matcherForTag.appendReplacement(sb, sbreplace.toString());
matcherForAttrib.appendTail(sbreplace);
matcherForTag.appendReplacement(sb, sbreplace.toString()+">");
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
System.out.println(sb.toString());
return sb.toString();
}
}