Main方法
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
public class PicBase64Generator {
// 项目在硬盘上的基础路径
private static final String PROJECT_PATH = System.getProperty("user.dir");
// 资源文件路径
private static final String RESOURCES_PATH = "/src/main/resources";
// 图片Base64字符串解析头
private static final String HEAD = "data:image/png;base64,";
public static void main(String[] args) {
Base64.Encoder encoder = Base64.getEncoder();
Utils utils = new Utils();
String filePath = PROJECT_PATH + RESOURCES_PATH + "/properties/vehicle-base64.properties"; // 定义配置文件存放路径
String picDir = "/img/vehicle/"; // 图片存放相对路径
// 获取文件夹下所有图片名称
String[] picName = utils.getFileNames(picDir);
if (utils.createFile(filePath)) { // 生成properties配置文件
List<String> data = new ArrayList<>();
for (String pic : picName) { // 遍历图片
byte[] bytes = utils.getFile(picDir + pic);
if (bytes == null) {
data.add(pic.split("\\.")[0] + "=" + "" + "\r\n"); // 构造配置文件内容形如:"bmw="
} else {
String encodedText = encoder.encodeToString(bytes); // 转换图片为base64编码
data.add(pic.split("\\.")[0] + "=" + HEAD + encodedText + "\r\n"); // 构造配置文件内容形如:"bmw=data:image/png;base64,xxxxx.."
}
}
// 将内容写入配置文件
utils.writeFileContent(filePath, data);
}
}
}
Utils 工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Utils {
/**
* 日志对象
*/
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
/**
* 获取目录下文件名列表
* @param path 文件目录
* @return 文件名列表
*/
public String [] getFileNames(String path)
{
File file = new File(this.getClass().getResource(path).getPath());
return file.list();
}
/**
* 获取文件字节数据
* @param filePath 文件路径,示例:/img/vehicle/190.png
* @return 文件字节数据
*/
public byte[] getFile(String filePath) {
File f;
try {
f = new File(this.getClass().getResource(filePath).getPath());
} catch (Exception e) {
LOGGER.error(Constant.LOG_FOMAT_TYPE_TWO,
ErrorCode.FILE_LOAD_ERROR.getErrorCode(),
ErrorCode.FILE_LOAD_ERROR.getMessage(),
e.getMessage());
return null;
}
LOGGER.info("file path: " + f.getAbsolutePath());
Path path = Paths.get(f.getAbsolutePath());
byte[] data;
try {
data = Files.readAllBytes(path);
} catch (IOException e) {
LOGGER.error(Constant.LOG_FOMAT_TYPE_TWO,
ErrorCode.FILE_LOAD_ERROR.getErrorCode(),
ErrorCode.FILE_LOAD_ERROR.getMessage(),
e.getMessage());
return null;
}
return data;
}
/**
* 创建文件
* @param fileName 文件路径
* @return true or false
*/
public boolean createFile(String fileName){
boolean bool = false;
File file = new File(fileName);
try {
if (file.exists()) {
file.delete();
bool = file.createNewFile();
} else {
bool = file.createNewFile();
}
} catch (Exception e) {
LOGGER.error(Constant.LOG_FOMAT_TYPE_TWO,
ErrorCode.CREATE_FILE_FAILED.getErrorCode(),
ErrorCode.CREATE_FILE_FAILED.getMessage(),
e.getMessage());
}
return bool;
}
/**
* 写入数据到新文件
* @param filepath 文件路径,包括文件名
* @param data 写入数据
*/
public void writeFileContent(String filepath, List<String> data){
try (FileOutputStream fos = new FileOutputStream(new File(filepath));
PrintWriter pw = new PrintWriter(fos)) {
for (String newStr : data) {
pw.write(newStr.toCharArray());
pw.flush();
}
} catch (Exception e) {
LOGGER.error(Constant.LOG_FOMAT_TYPE_TWO,
ErrorCode.FILE_WRITE_FAILED.getErrorCode(),
ErrorCode.FILE_WRITE_FAILED.getMessage(),
e.getMessage());
}
}
}
Properties工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
/**
* 日志对象
*/
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);
/**
* 获取Properties对象
* @param filePath 文件路径,示例:/properties/vehicle-base64.properties
* @return Properties对象
*/
public Properties getProperties(String filePath) {
InputStream in = this.getClass().getResourceAsStream(filePath);
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
LOGGER.error("[error code]{} - [msg]{}",
ErrorCode.FILE_LOAD_ERROR.getErrorCode(),
e.getMessage());
throw BaseException.of(ErrorCode.FILE_LOAD_ERROR.of());
}
return properties;
}
}
获取Poperties文件内容
PropertiesUtil propertiesUtil = new PropertiesUtil();
Properties properties = propertiesUtil.getProperties("/properties/vehicle-base64.properties");
String str = properties.getProperty(“bmw”);
---------------------
转载,仅作分享,侵删
作者:咸煌
原文:https://blog.csdn.net/qq_21084687/article/details/85157452
|
|