图片转base64 并根据格式加前缀

简介: 图片转base64 并根据格式加前缀
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>

运行结果太多了 就截图吧;


相关文章
|
开发者
处理base64图片数据的方式
处理base64图片数据的方式
111 4
|
前端开发 API
图片或文件Blob、File、Base64之间的相互转换
在做前端项目的时候,遇到图片的上传或者下载时,不可避免的会遇到Blob、File、Base64三种类型的转换。那么今天就总结下,三者之间的相互转换。 首先我们看看base64 ,File 对象,Blob 对象长什么样的,怎么来的。
481 1
|
人工智能 Java 对象存储
Java获取阿里云图片临时URL与图片文件转换Base64编码方法
在使用阿里云人工智能产品服务时,有部分服务需要上传的参数中包含文件URL,当我们没有开通OSS服务时,可以使用临时URL服务、或部分服务支持Base64编码格式,此文章为生成临时URL-JavaSDK方案与图片文件转换Base64编码方案。
1994 0
如何根据文件夹中文件,生成对应名字的图片,名称一样的路径,这里用到了变量,将集合定义在外面,字符串拼接,正则表达式截取.jpg文件
如何根据文件夹中文件,生成对应名字的图片,名称一样的路径,这里用到了变量,将集合定义在外面,字符串拼接,正则表达式截取.jpg文件
|
4月前
|
JavaScript
Base64如何切割后面的内容,如何提取data:image/png;base64,之后的内容,Vue中.split中截取的数组如何使用数组进行获取
Base64如何切割后面的内容,如何提取data:image/png;base64,之后的内容,Vue中.split中截取的数组如何使用数组进行获取
|
6月前
将图片(路径)转换为Base64 和 将base64转换为file类型
将图片(路径)转换为Base64 和 将base64转换为file类型
后端返回的base64流形式的图片,怎么放在src中
后端返回的base64流形式的图片,怎么放在src中
164 0
*.pvr.ccz文件还原成png格式
*.pvr.ccz文件还原成png格式
222 0
|
JavaScript
js:通过正则表达式从base64编码的图片中获取扩展名
js:通过正则表达式从base64编码的图片中获取扩展名
285 0
|
Web App开发 前端开发