工具类inputstream转为MultipartFile

 /**
     * inputStream转MultipartFile
     * @param inputStream
     * @param fileName
     * @return
     */
    public static FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = factory.createItem("file", MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int read = 0;
        OutputStream os = null;
        byte[] buffer = new byte[10 * 1024 * 1024];
        try {
            os = fileItem.getOutputStream();
            while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                os.write(buffer, 0, read);
            }
            inputStream.close();
        } catch (IOException e) {
            log.error("os write exception", e);
            throw new IllegalArgumentException("文件流输出失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("stream os close exception", e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("stream inputStream close exception", e);
                }
            }
        }
        return fileItem;
    }

    /**
     * inputStream转MultipartFile
     * @param inputStream
     * @param fileName
     * @return
     */
    public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        return new CommonsMultipartFile(fileItem);
    }

 

 

 

根据 filePath文件地址读取Minio文件服务器上的excel文件

public void importData(String filePath) throws Exception {
        //读取Excel文件中的数据,redListener网上搜索以下案例就行,用于EasyExcel转换实体对象
        StudentReadListener readListener = new StudentReadListener();
        String fileUrlPath = filePath.replace("/mrLi","");
        InputStream in = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(minioConfig.getBucketName())
                        .object(fileUrlPath)
                        .build());
        //无法直接读取minio文件流,转为MultipartFile文件
//        MultipartFile file = FileUtils.getMultipartFile(in,DateUtils.getNowDate()+"temp.xlsx");
        EasyExcel.read(in, StudentExcelDTO.class, readListener).sheet().doRead();
//得到excel文件转换后的数据内容 List
<StudentExcelDTO> excelDTOList = readListener.getStudentExcelDTOList();

 

posted on 2023-06-16 14:19  茫无所知  阅读(574)  评论(0编辑  收藏  举报