Base64与File之间的相互转化方式

 更新时间:2022年02月09日 10:12:47   作者:魔道不误砍柴功  
这篇文章主要介绍了Base64与File之间的相互转化方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Base64与File的相互转化

问题

最近遇到一个上传文件的问题,前端使用了另一种传值,就是Base64字符串传给后台 ,一开始没有对其进行解码操作,存入数据库时就超长了,今天这里提供一种base64和file之间相互转化的工具类,以便日后参考

/**
     *
     * @param path
     * @return String
     * @description 将文件转base64字符串
     * @date 2018年3月20日
     * @author changyl
     * File转成编码成BASE64
     */
 
    public static  String fileToBase64(String path) {
        String base64 = null;
        InputStream in = null;
        try {
            File file = new File(path);
            in = new FileInputStream(file);
            byte[] bytes=new byte[(int)file.length()];
            in.read(bytes);
            base64 = Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
//BASE64解码成File文件
    public static void base64ToFile(String destPath,String base64, String fileName) {
        File file = null;
        //创建文件目录
        String filePath=destPath;
        File  dir=new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            file=new File(filePath+"/"+fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

需要注意

标红的base64在这里需要去掉

baseStr = baseStr.replace("data:image/jpeg;base64,", "");//base64解密部分乱码问题(“+” 号,在urlecode编码中会被解码成空格)

将Base64转为文件并保存

public static void base64ToFile(String base64, String fileName, String savePath) {
        File file = null;
        //创建文件目录
        String filePath = savePath;
        File dir = new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
                dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            file=new File(filePath + fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解Springboot自定义异常处理

    详解Springboot自定义异常处理

    本篇文章主要介绍了详解Springboot自定义异常处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Spring中的ImportBeanDefinitionRegistrar接口详解

    Spring中的ImportBeanDefinitionRegistrar接口详解

    这篇文章主要介绍了Spring中的ImportBeanDefinitionRegistrar接口详解,ImportBeanDefinitionRegistrar接口是也是spring的扩展点之一,它可以支持我们自己写的代码封装成BeanDefinition对象,注册到Spring容器中,功能类似于注解@Service @Component,需要的朋友可以参考下
    2023-09-09
  • 使用@Validated和@Valid 解决list校验的问题

    使用@Validated和@Valid 解决list校验的问题

    这篇文章主要介绍了使用@Validated和@Valid 解决list校验的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java中unsafe操作实例总结

    Java中unsafe操作实例总结

    本篇文章给大家分享了关于Java中unsafe操作的相关知识点以及相关的实例代码,有需要的朋友可以学习参考下。
    2018-03-03
  • 详细分析java线程wait和notify

    详细分析java线程wait和notify

    本篇文章是对java多线程wait()和notify()进行了详细的分析介绍,需要了解的朋友参考下
    2015-07-07
  • mybatis中使用not in与 in的写法说明

    mybatis中使用not in与 in的写法说明

    这篇文章主要介绍了mybatis中使用not in与 in的写法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 一文详解Spring 中的顺序问题

    一文详解Spring 中的顺序问题

    本文主要介绍了Spring 中的顺序问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • JAVA简单选择排序算法原理及实现

    JAVA简单选择排序算法原理及实现

    选择排序(Selection Sort )分为两种 简单选择排序(Simple Selection Sort) 和树形选择排序
    2014-01-01
  • 教你利用springboot集成swagger并生成接口文档

    教你利用springboot集成swagger并生成接口文档

    有很多小伙伴不会利用springboot集成swagger并生成接口文档,今天特地整理了这篇文章,文中有非常详细的代码图文介绍及代码示例,对不会这个方法的小伙伴们很有帮助,需要的朋友可以参考下
    2021-05-05
  • Spring-基于Spring使用自定义注解及Aspect实现数据库切换操作

    Spring-基于Spring使用自定义注解及Aspect实现数据库切换操作

    这篇文章主要介绍了Spring-基于Spring使用自定义注解及Aspect实现数据库切换操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09

最新评论