欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Base64與File之間的相互轉(zhuǎn)化方式

 更新時(shí)間:2022年02月09日 10:12:47   作者:魔道不誤砍柴功  
這篇文章主要介紹了Base64與File之間的相互轉(zhuǎn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Base64與File的相互轉(zhuǎn)化

問題

最近遇到一個(gè)上傳文件的問題,前端使用了另一種傳值,就是Base64字符串傳給后臺(tái) ,一開始沒有對(duì)其進(jìn)行解碼操作,存入數(shù)據(jù)庫時(shí)就超長(zhǎng)了,今天這里提供一種base64和file之間相互轉(zhuǎn)化的工具類,以便日后參考

/**
     *
     * @param path
     * @return String
     * @description 將文件轉(zhuǎn)base64字符串
     * @date 2018年3月20日
     * @author changyl
     * File轉(zhuǎn)成編碼成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;
        //創(chuàng)建文件目錄
        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();
                }
            }
        }
    }

需要注意

標(biāo)紅的base64在這里需要去掉

baseStr = baseStr.replace("data:image/jpeg;base64,", "");//base64解密部分亂碼問題(“+” 號(hào),在urlecode編碼中會(huì)被解碼成空格)

將Base64轉(zhuǎn)為文件并保存

public static void base64ToFile(String base64, String fileName, String savePath) {
        File file = null;
        //創(chuàng)建文件目錄
        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();
                }
            }
        }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Springboot自定義異常處理

    詳解Springboot自定義異常處理

    本篇文章主要介紹了詳解Springboot自定義異常處理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Spring中的ImportBeanDefinitionRegistrar接口詳解

    Spring中的ImportBeanDefinitionRegistrar接口詳解

    這篇文章主要介紹了Spring中的ImportBeanDefinitionRegistrar接口詳解,ImportBeanDefinitionRegistrar接口是也是spring的擴(kuò)展點(diǎn)之一,它可以支持我們自己寫的代碼封裝成BeanDefinition對(duì)象,注冊(cè)到Spring容器中,功能類似于注解@Service @Component,需要的朋友可以參考下
    2023-09-09
  • 使用@Validated和@Valid 解決list校驗(yàn)的問題

    使用@Validated和@Valid 解決list校驗(yàn)的問題

    這篇文章主要介紹了使用@Validated和@Valid 解決list校驗(yàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中unsafe操作實(shí)例總結(jié)

    Java中unsafe操作實(shí)例總結(jié)

    本篇文章給大家分享了關(guān)于Java中unsafe操作的相關(guān)知識(shí)點(diǎn)以及相關(guān)的實(shí)例代碼,有需要的朋友可以學(xué)習(xí)參考下。
    2018-03-03
  • 詳細(xì)分析java線程wait和notify

    詳細(xì)分析java線程wait和notify

    本篇文章是對(duì)java多線程wait()和notify()進(jìn)行了詳細(xì)的分析介紹,需要了解的朋友參考下
    2015-07-07
  • mybatis中使用not?in與?in的寫法說明

    mybatis中使用not?in與?in的寫法說明

    這篇文章主要介紹了mybatis中使用not?in與?in的寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 一文詳解Spring?中的順序問題

    一文詳解Spring?中的順序問題

    本文主要介紹了Spring?中的順序問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • JAVA簡(jiǎn)單選擇排序算法原理及實(shí)現(xiàn)

    JAVA簡(jiǎn)單選擇排序算法原理及實(shí)現(xiàn)

    選擇排序(Selection Sort )分為兩種 簡(jiǎn)單選擇排序(Simple Selection Sort) 和樹形選擇排序
    2014-01-01
  • 教你利用springboot集成swagger并生成接口文檔

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

    有很多小伙伴不會(huì)利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼圖文介紹及代碼示例,對(duì)不會(huì)這個(gè)方法的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫切換操作

    Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫切換操作

    這篇文章主要介紹了Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫切換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評(píng)論