• Java,获取文件的Base64字符串,解码Base64字符串还原文件


    在jdk1.8以前,获取文件Base64字符串需要用到第三方库,从1.8开始,Java中引入了Base64相关的类

    以下是代码示例

    获取文件的Base64编码字符串

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Base64;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Base64Encoder {
    
        private static final Logger LOGGER = Logger.getLogger("Base64Encoder");
    
        public static void main(String[] args) {
            System.out.print("Enter an input filename: ");
            Scanner inputFromConsole = new Scanner(System.in);
            String filename = inputFromConsole.nextLine();
            FileOutputStream outputToFile = null;
            try (FileInputStream inputFromFile = new FileInputStream(filename)) {
                System.out.print("Enter an encoded filename: ");
                filename = inputFromConsole.nextLine();
                outputToFile = new FileOutputStream(filename);
                byte[] src = new byte[inputFromFile.available()];
                inputFromFile.read(src);
                byte[] encodedBytes = Base64.getEncoder().encode(src);
                outputToFile.write(encodedBytes);
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            } finally {
                if (outputToFile != null) {
                    try {
                        outputToFile.close();
                    } catch (IOException ex) {
                        LOGGER.log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    解码Base64字符串还原文件

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import static java.lang.System.out;
    import java.util.Base64;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Base64Decoder {
    
        private static final Logger LOGGER = Logger.getLogger("Base64Decoder");
    
        public static void main(String[] args) {
            if (args.length == 2) {
                try (FileInputStream encoded = new FileInputStream(args[0]); FileOutputStream decoded = new FileOutputStream(args[1])) {
                    byte[] buffer = new byte[encoded.available()];
                    encoded.read(buffer);
                    Base64.Decoder base64Decoder = Base64.getMimeDecoder();
                    decoded.write(base64Decoder.decode(buffer));
                } catch (IOException ex) {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
            } else {
                out.println("usage:
    	java Base64Decoder <encoded filename> <decoded filename>");
                out.println("for example:
    	java Base64Decoder MyPic.jpg.txt MyPic.jpg");
            }
        }
    }
  • 相关阅读:
    python pip install 报错(SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))')排查
    JMeter中验证参数空格时,要勾选编码
    共阴数码管静态显示
    蜂鸣器与继电器的基本控制
    Linux常用命令(粤嵌)
    LED指示灯的基本控制
    单片机设计与开发——概述
    交叉编译器的安装方法
    正则表达式校验金额最多两位小数的实数
    thinkphp自定义分类样式
  • 原文地址:https://www.cnblogs.com/buyishi/p/8407227.html
Copyright © 2020-2023  润新知