jar包发布读取资源文件
程序员文章站
2022-03-02 16:49:25
...
private void initProvinceCodeToRedis() {
try {
InputStream input = getClass().getClassLoader().getResourceAsStream("xlsx/provincial_code.xlsx");
String fileName = "code.xlsx";
File file = new File(fileName);
OutputStream os = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = input.read(buffer, 0, 1024)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
input.close();
List<String[]> strings = ImportExcelUtil.readExcel(file);
Pipeline pipeline = HashRedisUtils.getInstance().getPipeline();
StringBuilder key = new StringBuilder();
for (String[] string : strings) {
key.append(string[1]).append(string[3]);
pipeline.set(key.toString(), string[0]);
key.setLength(0);
}
pipeline.sync();
FileUtils.delete(fileName);
log.info("初始化省市区编码成功");
} catch (Exception e) {
log.error("初始化省市区编码失败" + e);
}
}