package com.example.democrud.democurd.uuid; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64Encoder; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class base { public static void main(String[] args) { String le = "http://192.168.0.117:18080/file/tmp/116972201185280.png"; String les = "G:/VueD/client/public/file/tmp/116972201185280.png"; String ss = getImgFileToBase64(les); getswitch(les,ss); } /** * imgFile 图片本地存储路径 */ public static String getImgFileToBase64(String imgFile) { //将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream inputStream = null; byte[] buffer = null; //读取图片字节数组 try { inputStream = new FileInputStream(imgFile); int count = 0; while (count == 0) { count = inputStream.available(); } buffer = new byte[count]; inputStream.read(buffer); } catch ( IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { // 关闭inputStream流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 对字节数组Base64编码 return new BASE64Encoder().encode(buffer); } /** *filename文件名 base64 */ public static String getswitch(String filename,String base64) { String str = filename.substring(filename.lastIndexOf(".") + 1); System.out.println(str); //png String pngPrefix = "data:image/png;base64,"; //jpg String jpegPrefix = "data:image/jpeg;base64,"; //docx String docxPrefix = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document ;base64,"; //doc String docPrefix = "data:application/msword;base64,"; //pdf String pdfPrefix = "data:application/pdf;base64,"; String filebase64 = null; if (StringUtils.isNotBlank(str)) { switch (str) { case "png": filebase64 = pngPrefix.concat(base64); break; case "jpg": filebase64 = jpegPrefix.concat(base64); break; case "docx": filebase64 = docxPrefix.concat(base64); break; case "doc": filebase64 = docPrefix.concat(base64); break; case "pdf": filebase64 = pdfPrefix.concat(base64); break; default: break; } System.out.println(filebase64); } return filebase64; } }
POM有需要的可以看下;
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.DemoCrud</groupId> <artifactId>democurd</artifactId> <version>0.0.1-SNAPSHOT</version> <name>democurd</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis整合Springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- StringUtils整合Springboot --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>16.8.0</version> </dependency> <!-- <dependency>--> <!-- maven 添加json--> <!-- <groupId>net.sf.json-lib</groupId>--> <!-- <artifactId>json-lib</artifactId>--> <!-- <version>2.4</version>--> <!-- </dependency>--> <!--jquery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.0</version> </dependency> <!--bootstrap-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> <!--配置文件注入时使用后会有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <!--添加poi-ooxml依赖包--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.11</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>com.documents4j</groupId> <artifactId>documents4j-local</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>com.documents4j</groupId> <artifactId>documents4j-transformer-msoffice-word</artifactId> <version>1.0.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency><!--导入分页插件--> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <!-- <version>1.2.3</version>--> <version>1.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.40</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
运行结果太多了 就截图吧;