JSoup快速入门-java解析html源码
安装-运行时依赖关系
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
1. 载入文件
从URL加载文档,使用Jsoup.connect()方法从URL加载HTML。
try
{
Document document = Jsoup.connect("http://www.yiibai.com").get();
System.out.println(document.title());
}
catch (IOException e)
{
e.printStackTrace();
}
2. 从文件加载文档
使用Jsoup.parse()方法从文件加载HTML。
{
Document document = Jsoup.parse( new File( "D:/temp/index.html" ) , "utf-8" );
System.out.println(document.title());
}
catch (IOException e)
{
e.printStackTrace();
}
3. 从String加载文档
使用Jsoup.parse()方法从字符串加载HTML。
try
{
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document document = Jsoup.parse(html);
System.out.println(document.title());
}
catch (IOException e)
{
e.printStackTrace();
}
4. 在HTML页面中获取表单属性
在网页中获取表单输入元素非常简单。 使用唯一ID查找FORM元素; 然后找到该表单中存在的所有INPUT元素。
Document doc = Jsoup.parse(new File("c:/temp/yiibai-index.html"),"utf-8");
Element formElement = doc.getElementById("loginForm");
Elements inputElements = formElement.getElementsByTag("input");
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
System.out.println("Param name: "+key+" \nParam value: "+value);
}
5.获取URL的元信息
元信息包括Google等搜索引擎用来确定网页内容的索引为目的。 它们以HTML页面的HEAD部分中的一些标签的形式存在。 要获取有关网页的元信息,请使用下面的代码。
try
{
Document document = Jsoup.parse(new File("C:/Users/zkpkhua/Desktop/yiibai-index.html"), "utf-8");
String description = document.select("meta[name=description]").get(0).attr("content");
System.out.println("Meta description : " + description);
String keywords = document.select("meta[name=keywords]").first().attr("content");
System.out.println("Meta keyword : " + keywords);
}
catch (IOException e)
{
e.printStackTrace();
}
Httpclient
https://www.cnblogs.com/bingyimeiling/p/11820583.html
json序列化返回金额两位数
public class DataSerializerUtils extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if(value!=null && !"".equals(value)) {
jsonGenerator.writeString(value.setScale(2,BigDecimal.ROUND_HALF_DOWN)+"");
}else {
jsonGenerator.writeString(value+"");
}
}
}
在具体字段上加注解即可
@ApiModelProperty(value = "支付金额", name = "payAmount")
@JsonSerialize(using = DataSerializerUtils.class)
private BigDecimal payAmount;
springboot|springmvc文件下载
@RequestMapping(value = "downPhotoById")
public void downPhotoByStudentId(String id, final HttpServletResponse response){
PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
byte[] data = entity.getPhotoData();
String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
}
两个时间之间的相差秒数,小时
Duration duration = Duration.between(payOrderDO.getCreateTime().toInstant().atZone( ZoneId.systemDefault() ).toLocalDateTime(), LocalDateTime.now());
long days = duration.toDays();
long hours = duration.toHours();
long millis = duration.toMillis()/1000;