新聞中心
這篇文章主要介紹Java如何通過URL獲取公眾號文章生成HTML,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的利津網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
說明:通過公眾號URL獲取的內(nèi)容,文字可以正常顯示,但是圖片存在跨域訪問的問題,微信不允許跨域訪問公眾號圖片,所以需要將公眾號圖片從存入本地后,再上傳至OSS,然后把HTML中的圖片全部替換為自己的OSS地址就可以了
這里就需要在后臺對HTML進(jìn)行DOM的解析,需要用的Jsoup
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;@Controllerpublic class TestUrl { @RequestMapping("tohtml") public String tohtml() { return "html/index.html"; } @RequestMapping("getHtml") @ResponseBody public 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; }}
以上是“Java如何通過URL獲取公眾號文章生成HTML”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:Java如何通過URL獲取公眾號文章生成HTML
標(biāo)題來源:http://www.dlmjj.cn/article/ppjdio.html