Demo
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
boolean result = base64StrToImage(readTxtFile("C:/710207F5-510F-4afd-A0E3-4016663BAFEC.txt"), "C:/001.jpeg");
System.out.println(result);
boolean result2 = base64StrToImage(imageToBase64Str("C:/001.jpeg"),"C:/002.jpg");
System.out.println(result2);
}
/**
* 读取文件
* @param filePath
* @return
*/
public static String readTxtFile(String filePath) {
StringBuffer sbf = new StringBuffer();
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
//判断文件编码
String encoding = resolveFileCharset(filePath);
fis = new FileInputStream(filePath);
isr = new InputStreamReader(fis, encoding);
br = new BufferedReader(isr);
String str;
while ((str = br.readLine()) != null) {
sbf.append(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fis != null) {
fis.close();
}
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fis = null;
isr = null;
br = null;
}
}
return sbf.toString();
}
/**
* 判断文件编码
*
* @param fileName
* @return
*/
public static String resolveFileCharset(String fileName) {
String fileCharCode = "GBK";
FileInputStream fis = null;
BufferedInputStream br = null;
try {
fis = new FileInputStream(fileName);
br = new BufferedInputStream(fis);
int p = (br.read() << 8) + br.read();
//其中的
switch (p) {
case 0xefbb:
fileCharCode = "UTF-8";
break;
case 0xfffe:
fileCharCode = "Unicode";
break;
case 0xfeff:
fileCharCode = "UTF-16BE";
break;
case 0x5c75:
fileCharCode = "ANSI|ASCII";
break;
default:
fileCharCode = "GBK";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fis = null;
br = null;
}
}
return fileCharCode;
}
/**
* base64编码字符串转换为图片
*
* @param imgStr base64编码字符串
* @param path 图片路径
* @return
*/
public static boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null) {
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
//去除base64前缀
String regEx = "data:image/\\w+;base64,";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(imgStr);
imgStr = matcher.replaceAll("");
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
//文件夹不存在则自动创建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
/**
* 图片转base64字符串
*
* @param imgFile 图片路径
* @return
*/
public static String imageToBase64Str(String imgFile) {
InputStream is = null;
byte[] data = null;
try {
is = new FileInputStream(imgFile);
data = new byte[is.available()];
is.read(data);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
}
网友评论