//转base64
@Test
public String fileToBase64() {
String imgFilePath = "d:/file.pdf";
String[] res = imgFilePath.split("\\.");
String pos = res[res.length - 1];
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
String data1 = Base64Utils.encodeToString(data);
System.out.println(data1);
return data1;
}
//转pdf
@Test
public void sfad(){
String s = this.fileToBase64();
byte[] decode = Base64Decoder.decode(s);
File file = new File("d:/file1.pdf");
try {
FileOutputStream fop = new FileOutputStream(file);
try {
fop.write(decode);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
java之pdf和base64互转
最新推荐文章于 2024-03-18 15:40:43 发布