欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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);
        }
    }

 

相关标签: java