今天遇到个奇怪的需求,不怕需求很奇怪,就怕奇怪的需求!
有个历史项目,新增个需求,在Kingeditor富文本编辑器上传视频,Kingeditor都很多年没更新了,原本也只支持flash播放器,不支持video视频,想了半天,干脆在后台做数据替换,然后就出现了下面的方案
本次使用Jsoup做核心处理。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Tessst {
public static void main(String[] args) {
String data = "<embed src=\"/scktxy/uploadfile/media/20200423/20200423172708_239.mp4\" type=\"video/x-ms-asf-plugin\" width=\"550\" height=\"400\" autostart=\"false\" loop=\"true\" />";
// 处理内容中的视频元素
Document doc = Jsoup.parse(data);
Elements elements = doc.select("embed[src]");//获取到的值为所有的<embed src="...">
for (Element element : elements) {
element.tagName("video"); // 把embed标签名改为video
element.attr("width", element.attr("width")+"px"); //把原有的width换成video支持的width
element.attr("height", element.attr("height")+"px"); //把原有的height换成video支持的height
element.attr("controls", "controls"); //去掉原有无用的controls标签-可选
element.removeAttr("type"); //去掉原有无用的type标签-可选
element.removeAttr("autostart"); //去掉原有无用的autostart标签-可选
element.removeAttr("loop"); //去掉原有无用的loop标签-可选
element.attr("style", "outline:none"); //还可以添加点元素
}
System.out.println("替换前:"+data);
data = doc.html();
System.out.println("替换后:"+data);
}
}
输出内容:
替换前:<embed src="/scktxy/uploadfile/media/20200423/20200423172708_239.mp4" type="video/x-ms-asf-plugin" width="550" height="400" autostart="false" loop="true" />
替换后:<html>
<head></head>
<body>
<video src="/scktxy/uploadfile/media/20200423/20200423172708_239.mp4" width="550px" height="400px" controls="controls" style="outline:none"></video>
</body>
</html>