package com.smart.hnj.common.utils;
import java.io.*;
import java.util.Base64;
public class Base64Utils {
public static boolean base64ToWav(String base64, String path) {
Boolean flag = false;
File newFile = new File(path);
File parentFile = newFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
BufferedOutputStream bos = null;
FileOutputStream fos = null;
try {
if (!newFile.exists()) {
newFile.createNewFile();
}
byte[] bytes = Base64.getDecoder().decode(base64);
fos = new java.io.FileOutputStream(newFile);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
flag = true;
} catch (Exception e) {
flag = false;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
flag = false;
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
flag = false;
}
}
}
return flag;
}
public static File base64ToFile(String base64) {
if (base64 == null || "".equals(base64)) {
return null;
}
byte[] buff = Base64.getDecoder().decode(base64);
File file = null;
FileOutputStream out = null;
try {
file = File.createTempFile("tmp", null);
out = new FileOutputStream(file);
out.write(buff);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
public static String fileToBase64(File file) {
if (file == null) {
return null;
}
String base64 = null;
FileInputStream in = null;
try {
in = new FileInputStream(file);
byte[] buff = new byte[in.available()];
in.read(buff);
base64 = Base64.getEncoder().encodeToString(buff);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
}