package com.xcong.farmer.cms;
|
|
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.util.StrUtil;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
import org.junit.jupiter.api.Test;
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
|
/**
|
* @author wzy
|
* @date 2022-07-09
|
**/
|
@SpringBootTest
|
public class TemplateTest {
|
|
String baseUrl = "http://120.27.238.55:8000/cms/static/";
|
|
@Test
|
public void staticFileTest() throws IOException {
|
File file = new File("/Users/helius/Desktop/template-online/index-test.html");
|
Document parse = Jsoup.parse(file, "utf-8");
|
|
staticPathParser(parse, "img", "src");
|
staticPathParser(parse, "href", "link");
|
staticPathParser(parse, "script", "src");
|
|
FileOutputStream outputStream = new FileOutputStream(file);
|
outputStream.write(parse.html().getBytes());
|
outputStream.close();
|
}
|
|
public void staticPathParser(Document document, String tagName, String attrKey) {
|
Elements elements = document.getElementsByTag(tagName);
|
if (elements.isEmpty()) {
|
return;
|
}
|
|
for (Element element : elements) {
|
String attr = element.attr(attrKey);
|
if (StrUtil.isNotBlank(attr) && !attr.contains("http://") && !attr.contains("https://")) {
|
element.attr(attrKey, baseUrl + attr);
|
}
|
}
|
}
|
}
|