MultipartFile、File类对文件相关操作以及高并发处理方式

编码背景:为防止同一时间段用户批量导入相同excel文件数据以及大数据文件读取excel数据性能问题,修改为不实时导入,先保存前端传过来的MultipartFile类型的文件保存到服务器,待执行的任务在redis队列按照顺序读取文件保存excel数据到数据库。

/**
 * 导入excel文件
 */
public String importExcel(@RequestPart MultipartFile multipartFile, String fileName) {
        //保存excel文件到本地
        EasyExcelUtil.saveExcel(multipartFile, fileName);
        //线程执行读取excel文件进行保存到数据库
        new Thread(new Runnable() {
            @Override
            public void run() {
                getExcelData(fileName);
            }
        }).start();
        return MessageUtil.setComSuccessMess("导入成功");
    }

    /**
     * 读取excel文件数据,保存到数据库
     * @param fileName
     */
    private void getExcelData(String fileName) {
        String path = "D:\\excel\\" + LocalDate.now() + File.separator + fileName;
        try {
            InputStream inputStream = new FileInputStream(path);
            List list = EasyExcelUtil.readExcel(inputStream, GlobalUserExcelModel.class, 2);
            JSONArray array = JSONArray.fromObject(list);
            System.out.println("array="+array);
            //保存数据库
            //insertGlobalUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

EasyExcelUtil类:

/**
 * 从Excel中读取文件,读取的文件是一个DTO类,该类必须继承BaseRowModel
 * @date 2020-02-27
 */
public class EasyExcelUtil {

    public static  List readExcel(final InputStream inputStream, final Class clazz, int headLine) {
        if (null == inputStream) {
            throw new NullPointerException("the inputStream is null!");
        }
        EasyExcelListener listener = new EasyExcelListener();
        //读取xls 和 xlxs格式
        //如果POI版本为3.17,可以如下声明
        ExcelReader reader = new ExcelReader(new BufferedInputStream(inputStream), null, listener);
        //判断格式,针对POI版本低于3.17
        //ExcelTypeEnum excelTypeEnum = valueOf(inputStream);
        //ExcelReader reader = new ExcelReader(inputStream, excelTypeEnum, null, listener);
        //从第headLine行开始读取excel文件数据,headLine=1 excel文件从第二行开始, headLine=2 从第三行开始,以此类推
        reader.read(new Sheet(1, headLine, clazz));

        return listener.getData();
    }

    /**
     * 需要写入的Excel,有模型映射关系
     *
     * @param file  需要写入的Excel,格式为xlsx
     * @param list 写入Excel中的所有数据,继承于BaseRowModel
     */
    public static void writeExcel(final File file, List list) {
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS);
            //自定义样式excel
            //ExcelWriter writer = new ExcelWriter(null, out, ExcelTypeEnum.XLSX, true, new AfterWriteHandler());
            //写第一个sheet,  有模型映射关系
            Class t = list.get(0).getClass();
            com.alibaba.excel.metadata.Sheet sheet = new com.alibaba.excel.metadata.Sheet(1, 0, t);
            sheet.setSheetName("模板");
            writer.write(list, sheet);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据输入流,判断为xls还是xlsx,该方法原本存在于easyexcel 1.1.0 的ExcelTypeEnum中。
     * 如果POI版本为3.17以下,则FileMagic会报错,找不到该类,此时去到POI 3.17中将FileMagic抽取出来
     */
    public static ExcelTypeEnum valueOf(InputStream inputStream) {
        try {
            FileMagic fileMagic = FileMagic.valueOf(inputStream);
            if (FileMagic.OLE2.equals(fileMagic)) {
                return ExcelTypeEnum.XLS;
            }
            if (FileMagic.OOXML.equals(fileMagic)) {
                return ExcelTypeEnum.XLSX;
            }
            throw new IllegalArgumentException("excelTypeEnum can not null");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存excel文件到本地
     * @param multipartFile
     * @param fileName
     */
    public static void saveExcel(MultipartFile multipartFile, String fileName) {
        String dirPath = "D:\\excel\\" + LocalDate.now();
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        try {
            String filePath = dir + File.separator + fileName;
            File file = new File(filePath);
            //保存文件
            multipartFile.transferTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**下载
     * @Param 路径,文件名
     *
     * */
    public static void download(HttpServletResponse response, File file) throws IOException{
        FileInputStream fis = null;
        OutputStream os = null;
        try{
            fis = new FileInputStream(file);
            // 取得输出流
            os = response.getOutputStream();
            // 清空输出流
            response.reset();
            // 设定输出文件头
            response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
            response.setContentType("application/x-download");
            byte[] mybyte = new byte[8192];
            int len = 0;
            while ((len = fis.read(mybyte)) != -1) {
                os.write(mybyte, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                fis.close();
            }
            if (null != os) {
                os.close();
            }
        }
    }

}

EasyExcel引入的jar包



    com.alibaba
    easyexcel
    1.1.2-beta5

 

 

你可能感兴趣的:(JAVA)