Java通过URL获取公众号文章生成HTML的方法

所属分类: 软件编程 / java 阅读数: 65
收藏 0 赞 0 分享

说明:通过公众号URL获取的内容,文字可以正常显示,但是图片存在跨域访问的问题,微信不允许跨域访问公众号图片,所以需要将公众号图片从存入本地后,再上传至OSS,然后把HTML中的图片全部替换为自己的OSS地址就可以了

这里就需要在后台对HTML进行DOM的解析,需要用的Jsoup

<dependency>
  <groupId>com.aliyun.oss</groupId>
  <artifactId>aliyun-sdk-oss</artifactId>
  <version>2.2.3</version>
 
 </dependency>
 <dependency>
   <groupId>org.jsoup</groupId>
   <artifactId>jsoup</artifactId>
   <version>1.9.2</version>
 </dependency>

controller

package com.iueang.controller;
 
import java.io.File;
import java.util.HashMap;
import java.util.Map;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.iueang.util.DownLoadImg;
import com.iueang.util.GetBody;
import com.iueang.util.OssUtil2;
import com.iueang.util.UrlUtil;
@Controller
public class TestUrl {
 
 @RequestMapping("tohtml")
 public String tohtml() {
 return "html/index.html";
 }
 @RequestMapping("getHtml")
 @ResponseBody
 public Map<String,String> getHtml(String url){
 //获取url文章生成文本
 String html = UrlUtil.getAccess(url);
 String reg = "<html>(.*?)</html>";
 String head=GetBody.getSubUtilSimple(html, reg);
 String HTTPHOST="http://yueang2.oss-cn-qingdao.aliyuncs.com/testimg/";
 String newsBody=head;
 Document doc = Jsoup.parse(newsBody);
    Elements pngs = doc.select("img[data-src]");
    System.out.println(pngs);
    for (Element element : pngs) {
    //获取图片地址
     String imgUrl = element.attr("data-src");
     //下载图片到本地
     String filename=DownLoadImg.downloadPicture(imgUrl);
  File file =new File("D:\\m2\\"+filename);
  //上传至oss
  Boolean flag = OssUtil2.uploadFileToOss(file, "testimg/"+filename);
  if(flag) {
  file.delete();
  }
     String newsrc =HTTPHOST + filename;
     element.attr("src", newsrc);
    }
    newsBody = doc.toString();
    System.out.println(newsBody);
 Map<String,String> map=new HashMap<String, String>();
 map.put("resultHtml", newsBody);
 return map;
 
 }
}

util工具类

GetBody类

package com.iueang.util;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class GetBody {
 
 public static String getSubUtilSimple(String html, String reg) {
  Pattern pattern = Pattern.compile(reg);// 匹配的模式
     Matcher m = pattern.matcher(html);
     while(m.find()){
       return m.group(1);
     }
     return "";
 }
 
}

OssUtil类

package com.iueang.util;
 
import java.io.File;
import java.util.HashMap;
import java.util.Map;
 
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
 
public class OssUtil2 { 
 //以下几个参数值必填,参考文章最后文档
 static String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
 static String accessKeyId = "oss获取";
 static String accessKeySecert = "oss获取";
 static String bucketName = "yueang2";
 
 /**
 * 上传单个文件到OSS
 * @param file 要上传的文件File对象
 * @param objName 上传后的文件名,包含文件夹,比如 game/game/test.txt
 * @return
 */
 public static boolean uploadFileToOss(File file, String objName) {
   try {
     OSSClient ossClient = null;
     try {
       ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecert);
     }catch (Exception e){
       e.printStackTrace();
     }
     ObjectMetadata meta = new ObjectMetadata();
     ossClient.putObject(bucketName, objName, file, meta);
     ossClient.shutdown();
   } catch (Exception e) {
     e.printStackTrace();
     return false;
   }
   return true;
 }
}

DownLoadImg类

package com.iueang.util;
 
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
 
import sun.misc.BASE64Encoder;
public class DownLoadImg {
 public static String downloadPicture(String urlList) {
  String filename="iueang"+UUID.randomUUID().toString()+".png";
  String path="D:/m2/"+filename;
     URL url = null;
     try {
       url = new URL(urlList);
       DataInputStream dataInputStream = new DataInputStream(url.openStream());
       FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
       ByteArrayOutputStream output = new ByteArrayOutputStream();
 
       byte[] buffer = new byte[1024];
       int length;
 
       while ((length = dataInputStream.read(buffer)) > 0) {
         output.write(buffer, 0, length);
       }
       BASE64Encoder encoder = new BASE64Encoder();
       String encode = encoder.encode(buffer);
       fileOutputStream.write(output.toByteArray());
       dataInputStream.close();
       fileOutputStream.close();
     } catch (MalformedURLException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
     System.out.println("Download返回的filname="+filename);
 return filename;
   }
}
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多