@Log("导出数据Html")
@ApiOperation("导出数据Html")
@GetMapping(value = "/downloadHtml")
public void exportBookMarkListHtml(HttpServletResponse response, BookMarkListQueryCriteria criteria) throws IOException {
List<BookMarkList> bookMarkList = bookMarkListService.getBookMarkList();
// 设置响应的内容类型和字符编码
response.setContentType("text/html;charset=UTF-8");
// 导出为 Google 书签 HTML
try {
exportToGoogleBookmarksHTML(bookMarkList, response);
System.out.println("Bookmarks exported successfully to bookmarks.html");
} catch (IOException e) {
System.err.println("Error exporting bookmarks: " + e.getMessage());
}
}
public static void exportToGoogleBookmarksHTML(List<BookMarkList> bookmarks, HttpServletResponse response) throws IOException {
try (BufferedWriter writer = new BufferedWriter(response.getWriter())) {
writer.write("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n");
writer.write("<!-- 小怪出品-->\n");
writer.write("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n");
writer.write("<TITLE>小怪书签</TITLE>\n");
writer.write("<H1>小怪书签</H1>\n");
writer.write("<DL><p>\n");
writer.write(" <DT><H3>小怪书签.html</H3>\n");
writer.write("<DL><p>\n");
writeBookmarks(writer, bookmarks);
writer.write("</DL><p>\n");
writer.write("</DL><p>\n");
}
}
private static void writeBookmarks(BufferedWriter writer, List<BookMarkList> bookmarks) throws IOException {
for (BookMarkList bookmark : bookmarks) {
if (bookmark.getBookMrakType() == 0) { // Folder
writer.write("<DT><H3>"+bookmark.getBookMrakName());
writer.write("</H3>\n");
if (bookmark.getChildren()!=null) {
writer.write("<DL><p>\n");
writeBookmarks(writer, bookmark.getChildren());
writer.write("</DL><p>\n");
}else {
writer.write("<DL><p>\n");
writer.write("</DL><p>\n");
}
} else if (bookmark.getBookMrakType() == 1) { // URL
writer.write("<DT><A HREF=\"" + bookmark.getBookMrakUrl() + "\" >" + bookmark.getBookMrakName() + "</A>\n");
}
}
}
BookMarkList 对象
public class BookMarkList implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "书签名称")
private String bookMrakName;
@ApiModelProperty(value = "书签地址")
private String bookMrakUrl;
@ApiModelProperty(value = "书签地址图标")
private String iconUrl;
@ApiModelProperty(value = "书签类型是0文件夹还是 1,url")
private Integer bookMrakType;
@ApiModelProperty(value = "书签上一级")
private Long pid;
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Timestamp createTime;
@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Timestamp updateTime;
@ApiModelProperty(value = "排序")
private Integer menuSort;
@ApiModelProperty(value = "子节点数目", hidden = true)
private Integer subCount = 0;
@TableField(exist=false)
private List<BookMarkList> children;
public Boolean getHasChildren() {
return subCount > 0;
}
public Boolean getLeaf() {
return subCount <= 0;
}
public String getLabel() {
return bookMrakName;
}
}