pom
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
主要类
package com.microstone.app.util.html2json.api;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.microstone.app.util.html2json.core.HtmlToJson;
import java.util.Collections;
public class IndexController extends Controller {
private Params getParams(){
String type=getPara("type",Params.TYPE_HTML);
Boolean highlight=getParaToBoolean("highlight",true);
Boolean linenums=getParaToBoolean("linenums",true);
String baseUri=getPara("host",getPara("imghost"));
Params params=new Params();
params.setHighlight(highlight);
params.setLinenums(linenums);
params.setType(type);
params.setBaseUri(baseUri);
return params;
}
public void index(){
String text=getPara("text");
Params params=getParams();
long startTime=System.currentTimeMillis();
String resultJson= HtmlToJson.by(text,params).get();
long endTime=System.currentTimeMillis();
long useTime=endTime-startTime;
System.out.println("耗时:"+useTime);
if(StrKit.isBlank(resultJson)){
renderJson(Collections.emptyList());
}else{
renderJson(resultJson);
}
}
}
package com.microstone.app.util.html2json.api;
public class Params {
public static final String TYPE_HTML = "html";
public static final String TYPE_MD = "md";
public static final String TYPE_MARKDOWN = "markdown";
private String type;
private Boolean highlight;
private Boolean linenums;
private String baseUri;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getHighlight() {
return highlight;
}
public void setHighlight(Boolean highlight) {
this.highlight = highlight;
}
public Boolean getLinenums() {
return linenums;
}
public void setLinenums(Boolean linenums) {
this.linenums = linenums;
}
public String getBaseUri() {
return baseUri;
}
public void setBaseUri(String baseUri) {
this.baseUri = baseUri;
}
}
package com.microstone.app.util.html2json.config;
import com.jfinal.config.*;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.PropKit;
import com.jfinal.render.ViewType;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import com.microstone.app.util.html2json.api.IndexController;
public class MainConfig extends JFinalConfig {
@Override
public void configConstant(Constants me) {
PropKit.use("config.properties");
me.setDevMode(PropKit.getBoolean("devMode"));
me.setViewType(ViewType.JFINAL_TEMPLATE);
me.setJsonFactory(FastJsonFactory.me());
}
@Override
public void configRoute(Routes me) {
me.add("/", IndexController.class);
}
@Override
public void configPlugin(Plugins me) {
}
@Override
public void configInterceptor(Interceptors me) {
}
@Override
public void configHandler(Handlers me) {
}
@Override
public void configEngine(Engine me) {
}
}
package com.microstone.app.util.html2json.core;
import com.alibaba.fastjson.JSONArray;
import com