Search in sources :

Example 1 with ImageLink

use of me.zhyd.hunter.entity.ImageLink in project OneBlog by zhangyd-c.

the class RemoverServiceImpl method parseImgForHtml.

/**
 * 解析Html中的img标签,将图片转存到七牛云
 *
 * @param spiderVirtualArticle spiderVirtualArticle
 * @param writerUtil           打印输出的工具类
 */
private String parseImgForHtml(VirtualArticle spiderVirtualArticle, HunterPrintWriter writerUtil) {
    if (null == spiderVirtualArticle) {
        return null;
    }
    String source = spiderVirtualArticle.getSource();
    Set<ImageLink> imageLinks = spiderVirtualArticle.getImageLinks();
    String html = spiderVirtualArticle.getContent();
    if (!CollectionUtils.isEmpty(imageLinks)) {
        writerUtil.print(String.format("检测到存在%s张图片,即将转存图片到云存储服务器中...", imageLinks.size()));
        Iterator<ImageLink> it = imageLinks.iterator();
        while (it.hasNext()) {
            ImageLink imageLink = it.next();
            String resImgPath = null;
            try {
                resImgPath = ImageDownloadUtil.saveToCloudStorage(imageLink.getSrcLink(), source);
                if (StringUtils.isEmpty(resImgPath)) {
                    writerUtil.print("图片转存失败,请确保云存储已经配置完毕!请查看控制台详细错误信息...");
                    continue;
                }
            } catch (Exception e) {
                writerUtil.print(e.getMessage());
                it.remove();
                continue;
            }
            html = html.replace(imageLink.getSrcLink(), resImgPath);
            writerUtil.print(String.format("<a href=\"%s\" target=\"_blank\">原图片</a> 已经转存到 <a href=\"%s\" target=\"_blank\">云存储</a>...", imageLink.getSrcLink(), resImgPath));
        }
    }
    return html;
}
Also used : ImageLink(me.zhyd.hunter.entity.ImageLink)

Aggregations

ImageLink (me.zhyd.hunter.entity.ImageLink)1