public class UploadUtil {
//这里的pathName 必须具体到某一张图片或者某一个文件的名字,如 D:\\xxx\\xxx.jpg
public static void storeImageAtLocal(String base64ImgData,String pathName){
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(base64ImgData);
/*
* 字节流转文件
*/
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pathName);
fos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//本地图片转base64编码数据
public static String getImageBase64Data(String localPath){
File file = new File(localPath); //URL url = new URL(src);
byte[] data = null;
try {
InputStream in = new FileInputStream(file); //InputStream in = url.openStream();
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(data);
}
}
Java工具类:base64格式的数据与本地文件的相互转换
于 2022-08-08 19:27:10 首次发布