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

厦门做网站维护的公司0基础做网站工具

厦门做网站维护的公司,0基础做网站工具,软件外包价格,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/a/513824.html

相关文章:

  • 个人营业执照怎么申请天津网站建设网站排名优化
  • 珠海网站建设企业seo外链工具软件
  • 俄文网站推广建设网站的意义 作用是什么
  • 【LeetCode】82. 删除排序链表中的重复元素 II
  • 网站推广由什么样的人来做如何推广网站话术
  • 做网站含备案费么软件开发工具免费
  • 网站建设模块需求深圳物流公司哪家便宜又好
  • 电子商务网站推广的主要方法有什么好的网站建设的书
  • 有哪些好的做兼职网站有哪些wordpress带会员vip主题
  • 创建一个网站所需的成本中山网站建设熊掌号
  • 建设银行的网站查询密码创意网络广告
  • 小型手机网站建设企业如乐建站之家
  • 襄阳宜城网站建设进入wordpress
  • 免费的网站怎么建做推广任务的网站
  • 东莞手机建网站上海58招聘网最新招聘
  • 外贸网站建设内容包括哪些电子商务网站策划书2000字
  • 小说网站wordpress制作一个网站难吗
  • 网站一般怎么推广html网站 怎么做seo
  • 网站后台英文一个网站一年的费用
  • 建设科技网络网站的意义和目的wordpress怎么让文章只显示摘要
  • 网站建设书店目标客户分析太原推广型网站开发
  • 免费下载建筑图集规范的网站快速将网站seo
  • 二次元网站设计绍兴网站建设网站
  • 个人建设什么网站好初中学习网站大全免费
  • 济南做网站建设中小企业网站功能模块及数据库表
  • 官方网站开发商网络新闻专题做的最好的网站
  • 容桂网站建设公司网站开发 无代码
  • 婚礼效果图网站h5制作工具免费版
  • 如何取一个大气的名字的做网站北京国际化品牌设计
  • 如何查询网站服务商中小企业建站的方法