package org.springblade.modules.appmenu.util;

import java.io.*;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * 文件转base64
 */
public class Base64Utils {
	public static String file2Base64(File file){
		    String picture="";
			try {
				FileInputStream fileInputStream = new FileInputStream(file);
				byte[] buffer = new byte[(int)file.length()];
				fileInputStream.read(buffer);
				fileInputStream.close();
				Base64.Encoder e = Base64.getEncoder();
				String base64String = e.encodeToString(buffer);
				return base64String;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return picture;
	}
	public static String file2Base64(InputStream inputStream){
		String picture="";
		try {
			byte[] bytes = readInputStream(inputStream);
			Base64.Encoder e = Base64.getEncoder();
			String base64String = e.encodeToString(bytes);
			return base64String;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return picture;
	}
	public static byte[] readInputStream(InputStream inStream) throws Exception{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len=inStream.read(buffer)) != -1 ){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}
	public static void main(String[] args) {
		try {
			System.out.println(URLEncoder.encode("D:\\test\\apk\\20211210175114apkunappstore_com.zkjc.policedemo_release_v9.0.1_20210909.apk", "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}
}
