Java通過URL獲取公眾號文章生成HTML的方法
說明:通過公眾號URL獲取的內(nèi)容,文字可以正常顯示,但是圖片存在跨域訪問的問題,微信不允許跨域訪問公眾號圖片,所以需要將公眾號圖片從存入本地后,再上傳至OSS,然后把HTML中的圖片全部替換為自己的OSS地址就可以了
這里就需要在后臺對HTML進(jìn)行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 {
//以下幾個參數(shù)值必填,參考文章最后文檔
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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:基于java socket實現(xiàn) 聊天小程序
欄 目:Java
下一篇:SpringBoot Session共享實現(xiàn)圖解
本文標(biāo)題:Java通過URL獲取公眾號文章生成HTML的方法
本文地址:http://www.jygsgssxh.com/a1/Java/8789.html
您可能感興趣的文章
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10基于Java驗證jwt token代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題


閱讀排行
本欄相關(guān)
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗證jwt token代碼實例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


