Java对图片Base64转码——HTML对Base64解码「Java加强版」( 二 )


System.out.printf("输入导出地址(两个文件都会在这个目录下):");
String toBothPath = scanner.next() ;
base64StringToImage(getImageBinary(path), toBothPath);
if(base64StringToTxt(getImageBinary(path), toBothPath).equals("success")){
System.out.println();
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** 导出成功 **\n");
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** Thanks!!! **\n");
System.out.printf("\t\t↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n");
}
} else {
System.out.println();
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** 您输入的编号无效!!! 程序意外退出了!!! **\n");
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** Thanks!!! **\n");
System.out.printf("\t\t↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n");
}
} else {
System.out.println();
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** Thanks!!! **\n");
System.out.printf("\t\t↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n");
}
}
}
static String getImageBinary(String path){
File f = new File(path);
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
// e.printStackTrace();
System.out.println();
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** 您输入的地址无效!!! 程序意外退出了!!! **\n");
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** Thanks!!! **\n");
System.out.printf("\t\t↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n");
}
return "" ;
}
static String base64StringToImage(String base64String, String path){
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(bais);
File w2 = new File(path + "/wjp.png");
ImageIO.write(bi1, "jpg", w2);
} catch (Exception e) {
// e.printStackTrace();
System.out.println();
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** 您输入的地址无效!!! 程序意外退出了!!! **\n");
System.out.printf("\t\t*********************************************\n");
System.out.printf("\t\t** Thanks!!! **\n");
System.out.printf("\t\t↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\n");
return "err" ;
}
return "success" ;
}
static String base64StringToTxt(String base64String, String path){
File filename = new File(path + "/wjp.txt");
RandomAccessFile mm = null ;
try {
mm = new RandomAccessFile(filename,"rw");
try {
mm.writeBytes(base64String);
} catch (IOException e) {
//e.printStackTrace();
System.out.println();
System.out.printf("\t\t*********************************************\n");

推荐阅读