package com.ruoyi.utils;
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
import com.ruoyi.common.utils.uuid.Seq;
|
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.io.FilenameUtils;
|
|
import javax.xml.bind.DatatypeConverter;
|
import java.io.*;
|
import java.net.URL;
|
|
public class Base64Util {
|
|
/**
|
* @Description: 文件转为base64字符串。filePath:文件路径
|
* @Param: [filePath]
|
* @return: java.lang.String
|
* @Date: 2020/12/25
|
*/
|
public static String fileToBase64(String filePath) throws IOException {
|
File file = new File(filePath);
|
FileInputStream inputFile = null;
|
byte[] buffer = null;
|
try {
|
inputFile = new FileInputStream(file);
|
buffer = new byte[(int) file.length()];
|
inputFile.read(buffer);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (null != inputFile) {
|
inputFile.close();
|
}
|
}
|
byte[] bs = Base64.encodeBase64(buffer);
|
return new String(bs);
|
}
|
|
/**
|
* base64字符串转文件
|
* @param base64Str
|
* @param targetFilePath
|
* @return
|
*/
|
public static void base64ToFile(String base64Str, String targetFilePath) {
|
byte[] buffer = Base64.decodeBase64(base64Str);
|
FileOutputStream out = null;
|
try {
|
out = new FileOutputStream(targetFilePath);
|
out.write(buffer);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if (null != out) {
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
/**
|
* 将图片转化为base64
|
* @param src
|
* @return
|
* @throws Exception
|
*/
|
public static String imgToBase64(String src) throws Exception {
|
URL url = new URL(src);
|
InputStream in = null;
|
byte[] data = null;
|
try {
|
in = url.openStream();//远程文件
|
data = new byte[in.available()];
|
in.read(data);
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
Base64 encoder = new Base64();
|
return encoder.encodeToString(data);
|
}
|
|
/**
|
* Base64转换为图片服务
|
* targetPath 输出视频文件路径,不需要文件名
|
* */
|
public static void base64ToImg(String base64,String targetPath){
|
File file = null;
|
FileOutputStream fops = null;
|
base64 = base64.replace("data:image/jpeg;base64,","");
|
byte[] buff = DatatypeConverter.parseBase64Binary(base64);
|
try {
|
fops = new FileOutputStream(new File(targetPath));
|
fops.write(buff);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
System.out.println("--------------------------------"+"图片转换完成"+"--------------------------------");
|
}
|
|
/**
|
* Base64转换为图片存在服务器中
|
* */
|
public static String base64ToJpg(String base64,String name){
|
//文件路径与存储路径
|
String fileName = StringUtils.format("{}/{}_{}.jpg", DateUtils.datePath(), name, Seq.getId(Seq.uploadSeqType));
|
String filePath = RuoYiConfig.getUploadPath() + "/" + fileName;
|
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
|
String currentDir = StringUtils.substring(RuoYiConfig.getUploadPath(), dirLastIndex);
|
String url = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
|
|
// 参数avatar接收base64字符串
|
// 1.截取掉base64开头的data:image/png;base64,
|
base64 = base64.replace("data:image/jpeg;base64,","");
|
// 2.base64转换为bytes类型
|
byte[] buff = DatatypeConverter.parseBase64Binary(base64);
|
// 3.创建一张图片,将bytes类型的数据写入图片中
|
FileOutputStream fops = null;
|
try {
|
File desc = new File(filePath);
|
if (!desc.exists()) {
|
if (!desc.getParentFile().exists()) {
|
desc.getParentFile().mkdirs();
|
}
|
}
|
fops = new FileOutputStream(desc);
|
fops.write(buff);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return url;
|
}
|
|
|
|
}
|