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

网站建设友链交换百度推广登陆平台

网站建设友链交换,百度推广登陆平台,贵阳网站制作免费,做网站需要做手机版吗问题需求 今天在做发送邮件功能的时候,发现邮件的附件部分,比如pdf文档,要求先把pdf转为base64,邮件才会发送。那接下来就先看看Java 如何把 pdf文档转为base64。 两种方式,一种是通过插件 jar 包的方式引入&#xf…

问题需求

今天在做发送邮件功能的时候,发现邮件的附件部分,比如pdf文档,要求先把pdf转为base64,邮件才会发送。那接下来就先看看Java 如何把 pdf文档转为base64。

两种方式,一种是通过插件 jar 包的方式引入,另外一种则是 通过原生的 文件流来读取pdf 并转为 byte 字节。

jar包引入

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.1</version>
</dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.1</version>
</dependency>
代码测试
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.io.RandomAccessBuffer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;public class PDFToBase64 {public static String convertPDFToBase64(Path pdfPath) throws IOException {try (PDDocument document = PDDocument.load(pdfPath.toFile())) {// 使用ByteArrayOutputStream来获取PDF的字节内容ByteArrayOutputStream outputStream = new ByteArrayOutputStream();document.save(outputStream);byte[] pdfBytes = outputStream.toByteArray();// 将字节转换为Base64字符串return Base64.getEncoder().encodeToString(pdfBytes);}}public static void main(String[] args) {try {Path pdfPath = Files.createTempFile("test", ".pdf");// 这里应该是你的PDF文件路径String base64String = convertPDFToBase64(pdfPath);System.out.println(base64String);} catch (IOException e) {e.printStackTrace();}}
}

在这里插入图片描述

原生 InputStream 实现

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;try {URL obj = new URL(url);HttpURLConnection con = (HttpURLConnection) obj.openConnection();// 添加请求头,如有必要con.setRequestMethod("GET");con.setRequestProperty("Accept", "application/pdf");int responseCode = con.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) { // 200InputStream inputStream = con.getInputStream();ByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] buffer = new byte[4096];int n;while ((n = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, n);}byte[] pdfBytes = outputStream.toByteArray();String base64Encoded = Base64.getEncoder().encodeToString(pdfBytes);log.info("base64==" + base64Encoded);return base64Encoded;} else {System.out.println("GET request not worked");}} catch (Exception e) {e.printStackTrace();}

在这里插入图片描述

http://www.hkea.cn/news/725131/

相关文章:

  • 网站开发开源架构抖音营销软件
  • 自己做的网站能放到网上么青岛seo经理
  • 营业推广策划方案邵阳网站seo
  • 手机网站横向切换kol合作推广
  • 专门做超市海报的网站宁波seo咨询
  • 仿网站上的焦点图在线看seo网站
  • 做网站的业务员艾滋病阻断药有哪些
  • web集团网站建设广告投放平台有哪些
  • 大连做网站建设广告资源对接平台
  • 做网站怎么写工作日志泉州网站seo公司
  • wordpress外链站内打开搜索引擎是什么意思啊
  • 做论坛网站需要什么备案新站seo优化快速上排名
  • 动漫网站html百度网盘搜索
  • 怎么看一个网站什么语言做的宝鸡seo培训
  • 数据库网站建设公司他达拉非片
  • 英文商城网站建设搜索引擎营销的特点
  • 易优建站系统图片百度搜索
  • 网站开发不用框架web网站设计
  • 技能网站建设项目需求武汉网络推广外包公司
  • 安卓市场下载手机版优化网站排名技巧
  • 建设网站平台哪个好互联网营销外包推广
  • 工商注册企业名称查询广东seo网站推广代运营
  • 中纪委网站两学一做征文资源平台
  • java高端网站建设现在广告行业好做吗
  • wordpress 制作下载优化关键词怎么做
  • 宁波网站建设哪个公司好百度爱采购推广怎么入驻
  • 重庆市建设工程信息网特种作业企业网站seo多少钱
  • 域名备案做电影网站制作免费个人网站
  • 公司网络营销方案优化设计七年级上册数学答案
  • 网站建设策划方案网址搜索引擎