java中File流转Base64

java中File流转Base64


package com.demo;

import com.sun.deploy.util.StringUtils;
import sun.misc.*;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class MyDemoApplication {
    public static void main(String[] args) throws IOException {
        File file = new File("d:\\测试11111.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte [] bytes = new byte[fileInputStream.available()]; // 给 byte 预留的空间
        fileInputStream.read(bytes); // 读取到 byte 里面
        BASE64Encoder base64Encoder = new BASE64Encoder();
        // 得到文件 之后转成beye 然后使用base64转码
        String encode = base64Encoder.encode(bytes);// 转码
        System.out.println(encode);

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(encode);
        // 创建一个 空的文件写入到 这个新的文件中去
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\new1111.txt"));
        fileOutputStream.write(bytes1);
        fileOutputStream.flush();
        fileOutputStream.close();

    }
}

你可能感兴趣的:(java,java)