zoukankan      html  css  js  c++  java
  • 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");
            }
        }
    }
  • 相关阅读:
    js 格式化相关的时间
    JCE无限制权限策略文件
    Java设计模式之《观察者模式》及应用场景
    Idea for Mac 过期 IntelliJ IDEA 2017 完美注册方法(附idea for Mac破解方法)
    Macbook系统环境安装wget的2个方法
    Mac上brew&thrift安装 以及在thrift架构下,自己新作了maven的小例 Demo
    idea编译器光标变为insert状态
    配置自己的Maven方式并使用Maven 运行项目Idea的maven的项目
    SourceTree 如何下载git 管理的代码-如何创建分支,删除分支,提交代码,回退代码
    ultraEdit MAC 破解方法
  • 原文地址:https://www.cnblogs.com/buyishi/p/8407227.html
Copyright © 2011-2022 走看看