当前位置: 首页 > wzjs >正文

重庆建设集团广州seo好找工作吗

重庆建设集团,广州seo好找工作吗,wordpress怎么做网站,做网站要找什么富文本编辑器的上传的图片默认转为base64,但是如果需要保存到数据库,base64的数据就太大了,所以一般都会长传到文件服务器,然会返回图片url。 但是当我们需要把富文本编辑器的内容导出为html时,因为图片时url,当浏览器…

富文本编辑器的上传的图片默认转为base64,但是如果需要保存到数据库,base64的数据就太大了,所以一般都会长传到文件服务器,然会返回图片url。

但是当我们需要把富文本编辑器的内容导出为html时,因为图片时url,当浏览器离线浏览时就无法显示了,所以,在导出时,如果是图片,就把图片转为base64,这样即使在离线的情况下也能显示图片了。
在这里插入图片描述

package com.chen.utils;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;import com.jfinal.kit.PathKit;import cn.hutool.core.util.IdUtil;/**
* @author 作者 @Chenxc:
* 
*/
public class Export2HtmlUtil {private static final String localPath = PathKit.getWebRootPath()+File.separator+"genhtml";private static final String uploadPath = PathKit.getWebRootPath()+File.separator+"upload";/****把html字符串转成文件*/public static File HtmlToFile(String html) {if(null == html) {return null;}String uuid = IdUtil.simpleUUID();String htmlFilePath = localPath+File.separator+uuid+".html";String convertedHtml;try {convertedHtml = convertHtmlImagesToBase64(html);Path path = Paths.get(htmlFilePath);Files.write(path, convertedHtml.getBytes("utf-8"));return path.toFile();} catch (IOException e) {System.err.println("Failed to HtmlToFile: ");e.printStackTrace();return null;}}private static String convertHtmlImagesToBase64(String html) throws IOException {Document doc = Jsoup.parse(html);Elements imgs = doc.select("img");for (Element img : imgs) {String imageUrl = img.attr("src");if (isRemoteUrl(imageUrl)) {try {String base64Data = fetchImageAsBase64(imageUrl);img.attr("src", base64Data);} catch (IOException e) {System.err.println("Failed to process image: " + imageUrl);e.printStackTrace();}}else if(isLocalUrl(imageUrl)){try {String substring = imageUrl.substring(imageUrl.lastIndexOf("/")+1);String path = uploadPath+File.separator+substring;String base64Data = convertLocalImgToBase64WithPrefix(path);img.attr("src", base64Data);} catch (Exception e) {System.err.println("Failed to process image: " + imageUrl);e.printStackTrace();}}}return doc.outerHtml();}private static String convertLocalImgToBase64WithPrefix(String filePath) throws Exception {byte[] imageBytes = readFileBytes(filePath);String mimeType = Files.probeContentType(Paths.get(filePath));return "data:" + mimeType + ";base64," +Base64.getEncoder().encodeToString(imageBytes);}private static String fetchImageAsBase64(String imageUrl) throws IOException {URL url = new URL(imageUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(5000);connection.setReadTimeout(5000);try (InputStream inputStream = connection.getInputStream();ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {if (connection.getResponseCode() != 200) {throw new IOException("HTTP error: " + connection.getResponseCode());}String contentType = connection.getContentType();if (contentType == null || !contentType.startsWith("image/")) {contentType = getContentTypeFromExtension(imageUrl);}byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}byte[] imageBytes = outputStream.toByteArray();String base64 = Base64.getEncoder().encodeToString(imageBytes);return "data:" + contentType + ";base64," + base64;} finally {connection.disconnect();}}private static String getContentTypeFromExtension(String imageUrl) {if (imageUrl.toLowerCase().endsWith(".png")) return "image/png";if (imageUrl.toLowerCase().endsWith(".jpg") || imageUrl.toLowerCase().endsWith(".jpeg")) return "image/jpeg";if (imageUrl.toLowerCase().endsWith(".gif")) return "image/gif";if (imageUrl.toLowerCase().endsWith(".webp")) return "image/webp";return "application/octet-stream";}private static byte[] readFileBytes(String filePath) throws Exception {Path path = Paths.get(filePath);return Files.readAllBytes(path);}private static boolean isRemoteUrl(String url) {return url.startsWith("http://") || url.startsWith("https://");}private static boolean isLocalUrl(String url) {return url.startsWith("/upload") || url.startsWith("upload");}
}
http://www.dtcms.com/wzjs/230260.html

相关文章:

  • 网站域名如何起seo怎么优化步骤
  • 新民正规网站建设价格咨询武汉网站关键词推广
  • pc 移动端网站建设电商培训心得体会
  • 网站开发与设计公司app开发用什么软件
  • 建什么类型个人网站百度如何快速收录网站
  • 小说网站wordpress百度竞价排名公式
  • 怎么让自己的电脑做网站服务器百度招聘平台
  • 校园网站建设报告网络推广员是干什么的
  • 潍坊最新消息西宁网站seo
  • 网站免费建站pixiv app百度推广广告收费标准
  • 网站首页html安徽网络推广
  • 苹果网站用什么做的百度搜索广告怎么收费
  • 进口国际博览会上海在线优化seo
  • 网站到期续费要多少钱黄页污水
  • seo是啥职业百度推广怎么优化
  • 手机网站创建站点成功百度平台推广该怎么做
  • 嘉兴网站设计公司免费的短视频app大全
  • 5个不好的网站软件发布网
  • 第二章 营销型网站建设测验智推教育seo课程
  • 网站添加搜索关键字优化大师的功能有哪些
  • 亿玫网站建设班级优化大师使用指南
  • 大学生做企业网站谷歌推广教程
  • 做渔家乐推广的有哪些好网站2022最新引流推广平台
  • 义乌建设局网站域名搜索引擎
  • 做付费视频网站免费站推广网站2022
  • 河南平台网站建设设计百度竞价托管费用
  • 买的服务器做两个网站月嫂免费政府培训中心
  • 鬼畜做的青龙游戏网站宁波网络推广平台
  • 做二维码推送网站cpc广告点击日结联盟
  • 网站建设 的介绍数字营销成功案例