base64分块解码

package org.example;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String fileDir0 = "C:\\Users\\Public\\Downloads\\decodebase64\\"; // 替换为实际文件的路径
        String fileDir1 = null;
        FileOutputStream fileOutputStream = null;
        String fileName = null;
        while (true){
            System.out.println("------------------------------------");
            System.out.println("请输入base64文件地址");
            String filePath = scanner.nextLine();
            System.out.println("请输入解码的文件格式 0.txt 1.jpg 2.ttf 3.mp4");
            String extension = scanner.nextLine();
            if ("0".equals(extension)){
                extension = ".txt";
            } else if ("1".equals(extension)) {
                extension = ".jpg";
            } else if ("2".equals(extension)) {
                extension = ".ttf";
            } else if ("3".equals(extension)) {
                extension = ".mp4";
            }
            try {
            	//假如有路径两边有"就去掉
                if ('\"' == (filePath.charAt(0)) && '\"' == (filePath.charAt(filePath.length()-1))){
                    filePath = filePath.substring(1, filePath.length() - 1);
                }
                File file = new File(filePath);
                fileName = file.getName();
                fileDir1 = fileDir0 + fileName.substring(0, fileName.indexOf(".")) + "\\"; // 替换为实际文件的路径
                File f = new File(fileDir1);
                if (!f.exists()){
                    f.mkdirs();
                }
                try (FileInputStream inputStream = new FileInputStream(filePath)) {
                    // 创建Base64.Decoder对象
                    Base64.Decoder decoder = Base64.getDecoder();

                    byte[] buffer = new byte[1200];//解码块大小必须是4的倍数
                    int bytesRead;

                    // 逐块读取文件并解码
                    fileOutputStream = new FileOutputStream(fileDir1 + fileName.substring(0, fileName.indexOf(".")) + extension);
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        byte[] bytes = new byte[bytesRead];
                        System.arraycopy(buffer, 0, bytes, 0, bytesRead);
                        // 解码Base64编码的数据
                        byte[] decodedData = decoder.decode(bytes);
                        fileOutputStream.write(decodedData);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("文件保存在" + fileDir1);
        }

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值