In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how Java obtains the official account through URL to generate HTML. The article is very detailed and has certain reference value. Interested friends must read it!
Note: the content obtained through the official account URL, the text can be displayed normally, but the picture has the problem of cross-domain access. Wechat does not allow cross-domain access to official account pictures, so you need to save the official account pictures locally, and then upload them to OSS, and then replace all the pictures in HTML with your own OSS address.
Here, you need to parse the DOM of HTML in the background, and you need to use Jsoup
Com.aliyun.oss aliyun-sdk-oss 2.2.3 org.jsoup jsoup 1.9.2
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 getHtml (String url) {/ / get url article generation text String html = UrlUtil.getAccess (url); String reg = "(. *?)"; 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) {/ / get the image address String imgUrl = element.attr ("data-src"); / / download the picture to the local String filename=DownLoadImg.downloadPicture (imgUrl); File file = new File ("D:\\ m2\" + filename); / / upload to 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 map=new HashMap (); map.put ("resultHtml", newsBody); return map;}}
Util utility class
GetBody class
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); / / matching pattern Matcher m = pattern.matcher (html); while (m.find ()) {return m.group (1);} return ";}}
OssUtil class
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 {/ / the following parameter values are required. Refer to the final document of the article static String endpoint = "http://oss-cn-qingdao.aliyuncs.com"; static String accessKeyId =" oss acquisition "; static String accessKeySecert =" oss acquisition "; static String bucketName =" yueang2 " / * upload a single file to OSS * @ param file the uploaded file name of the File object * @ param objName, including a folder, such as 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 class
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 ("filname=" returned by Download + filename); return filename }} the above is all the contents of the article "how to get the official account article to generate HTML through URL". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 257
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.