package **.tool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
@Slf4j
public class FileUtil {
/**
* 根据base64判断图片是否为tiff
*
* @param base64
* @return
*/
public static boolean checkImageBase64Format(String base64) {
byte[] b = java.util.Base64.getDecoder().decode(base64);
try {
// 判断是否为tiff格式
if ((b[0] & 0xFF) == 0x49 && (b[1] & 0xFF) == 0x49 && (b[2] & 0xFF) == 0x2A) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据BASE64字符串生成图片文件,此方法生成tiff格式
* @param base64
* @param fileName
* @param dictionary
*/
/* public static void base64ToFile(String base64, String fileName, String dictionary) {
File file =null;
File dir=new File(dictionary);
// 无目录的情况下创建一个目录,会受权限影响,最好是已存在的目录
if(!dir.exists() && dir.isDirectory()) {
dir.mkdirs();
}
java.io.FileOutputStream fos=null;
BufferedOutputStream bos=null;
try {
byte [] bytes=java.util.Base64.getDecoder().decode(base64);
file=new File(dictionary+fileName);//目录+文件名作为输出文件的全路径
fos= new java.io.FileOutputStream(file);
bos=new BufferedOutputStream(fos);
bos.write(bytes);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(bos!=null) {
try {
bos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fos!=null) {
try {
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}*/
/**
* 将tiff图片转化为jpg,生成新的文件
*
* @param oldPath 原图片的全路径
* @param newPath 生成新的图片的全路径
*/
public static void tiffToJpg(String oldPath, String newPath) {
try {
BufferedImage bufferegImage = ImageIO.read(new File(oldPath));
ImageIO.write(bufferegImage, "jpg", new File(newPath));//可以是png等其它图片格式
} catch (Exception e) {
e.printStackTrace();
log.error("tif转jpg转化图片错误,错误原因:" + e.getMessage()+"路径:"+oldPath);
}
}
/**
* 将任意图片文件转为base64,读字节是最快的方式
*
* @param filePath 图片文件的全路径
* @return
*/
public static String imageToBase64(String filePath) {
byte[] data = null;
try {
InputStream in = new FileInputStream(filePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
log.error("图片转base64错误,错误原因:" + e.getMessage()+"路径"+filePath);
return null;
}
return new String(Base64.encodeBase64(data));
}
/**
* 将base64字符串,生成文件
*/
public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
dir.mkdirs();
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] bfile = decoder.decodeBuffer(fileBase64String);
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
return file;
} catch (Exception e) {
e.printStackTrace();
log.error("base64转图片错误,错误原因:" + e.getMessage()+"路径:"+filePath);
return null;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
文件处理类
于 2022-08-30 09:16:37 首次发布