[Ten Wei: PPS33A integrity management] [] [continuously updated]
Description: The contents of the URL acquired through public numbers, text can be normal, but there are pictures of cross-domain access issues, micro-channel number does not allow cross-domain access public images, it needs to be public from the picture number stored locally, and then upload it to OSS and then the pictures in HTML replace all their OSS address on it
Here it is necessary to perform HTML DOM parsing in the background, you need to use 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;
com.iueang.util.OssUtil2 import;
com.iueang.util.UrlUtil import;
@Controller
public class TestUrl {
@RequestMapping("tohtml")
public String tohtml() {
return "html/index.html";
}
@RequestMapping("getHtml")
@ResponseBody
public Map<String,String> getHtml(String url){
// get the url generate text article
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) {
// Get the Picture Address
String imgUrl = element.attr("data-src");
// download pictures to your local
String filename=DownLoadImg.downloadPicture(imgUrl);
File file =new File("D:\\m2\\"+filename);
// uploaded 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<String,String> map=new HashMap<String, String>();
map.put("resultHtml", newsBody);
return map;
}
}
util Tools
GetBody class
com.iueang.util package;
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
Matches m = pattern.matcher (html);
while(m.find()){
return m.group(1);
}
return "";
}
}
OssUtil class
com.iueang.util package;
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 required, the final document referenced article
static String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
static String accessKeyId = "oss get";
static String accessKeySecert = "oss get";
static String bucketName = "yueang2";
/**
* Upload individual files to OSS
* File File object @param file to upload
* The file name @param objName upload contains folders, 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类
com.iueang.util package;
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返回