通用的SpringBoot集成的文件上传与下载

废话不多说--直接看代码

controller

package com.webank.wedatasphere.qualitis.controller.thymeleaf;

import com.webank.wedatasphere.qualitis.handler.CommonExcelService;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectFileRepository;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectMenuRepository;
import com.webank.wedatasphere.qualitis.project.entity.Project;
import com.webank.wedatasphere.qualitis.project.entity.ProjectFile;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMenu;
import com.webank.wedatasphere.qualitis.response.BiaoZunExcelDTO;
import com.webank.wedatasphere.qualitis.response.Resp;
import com.webank.wedatasphere.qualitis.util.CreateTreeUtils;
import com.webank.wedatasphere.qualitis.util.LocalTimeUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CommonController {

    /**
     * 菜单实现
     */
    @Autowired
    private ProjectMenuRepository projectMenuRepository;


    /**
     * 菜单实现
     */
    @Autowired
    private ProjectFileRepository projectFileRepository;


    private String fileSavePath;

    @Autowired
    private Environment environment;

    @Autowired
    private CommonExcelService commonExcelService;

    @PostConstruct
    public void loadEvn() {
        // 切换代码执行环境
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            fileSavePath = environment.getProperty("file.save.path_windows");
        } else {
            fileSavePath = environment.getProperty("file.save.path_linux");
        }
    }

    @RequestMapping(value = {"/getMenuList"}, method = RequestMethod.GET, name = "获取菜单列表·")
    @ResponseBody
    public Resp<?> getMenuList(HttpServletRequest request) {
        List<ProjectMenu> all = projectMenuRepository.findAll();
        List<ProjectMenu> tree = CreateTreeUtils.createTree(all);
        // 生成目录树
        return Resp.Ok(tree);
    }


    /**
     * 菜单目录
     *
     * @return
     */
    @RequestMapping(value = {"/menuIndex"}, method = RequestMethod.GET)
    public ModelAndView menuIndex() {
        ModelAndView modelAndView = new ModelAndView("layui-admin/index2");
        List<ProjectMenu> all = projectMenuRepository.findAll();
        List<ProjectMenu> tree = CreateTreeUtils.createTree(all);
        modelAndView.addObject("resourceGroups", tree);
        return modelAndView;
    }

    /**
     * 导入
     *
     * @param request  request
     * @param response response
     */
    @RequestMapping(value = {"/insertFile"}, method = {RequestMethod.GET, RequestMethod.POST}, name = "文件上传·")
    @ResponseBody
    public Resp<?> insertFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            return Resp.error("文件名不能为空");
        }
        // 校验目录库
        String currentTime = LocalTimeUtils.getCurrentTimeSimple();
        File file = new File(fileSavePath + "/" + currentTime);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        String filePath = "";
        // 文件保留路径
        String fileName = fileSavePath + "/" + currentTime + "/" + originalFilename;
        File dest = new File(fileName);
        ProjectFile projectFile = new ProjectFile();
        try {
            // 保存本地文件
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), dest);
            // 文件回调组装入库
            filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + fileName;
            // 文件上传到目录库
            projectFile.setFileName(originalFilename);
            projectFile.setFilePath(fileName);
            projectFile.setName(filePath);
            projectFile.setCreateTime(currentTime);
            projectFileRepository.save(projectFile);
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.error(e.getMessage());
        }
        // 返回文件路径
        return Resp.Ok(projectFile);
    }

    /**
     * 导入数据库
     *
     * @param request  request
     * @param response response
     */
    @RequestMapping(value = {"/excelSaveDb"}, method = {RequestMethod.GET, RequestMethod.POST}, name = "文件上传·")
    @ResponseBody
    public Resp<?> excelSaveDb(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            return Resp.error("文件名不能为空");
        }
        if (!originalFilename.contains(".xls")) {
            return Resp.error("文件格式错误:必须要是XLS或者XLSX格式");
        }
        // 校验目录库
        String currentTime = LocalTimeUtils.getCurrentTimeSimple();
        File file = new File(fileSavePath + "/" + currentTime);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        String filePath = "";
        // 文件保留路径
        String fileName = fileSavePath + "/" + currentTime + "/" + originalFilename;
        File dest = new File(fileName);
        ProjectFile projectFile = new ProjectFile();
        try {
            // 保存本地文件
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), dest);
            // 文件回调组装入库
            filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + fileName;
            // 文件上传到目录库
            projectFile.setFileName(originalFilename);
            projectFile.setFilePath(fileName);
            projectFile.setName(filePath);
            projectFile.setCreateTime(currentTime);
            projectFileRepository.save(projectFile);
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.error(e.getMessage());
        }
        // 读取成为Excel入库
        commonExcelService.handlerBiaoZunUpload(fileName);
        // 返回文件路径
        return Resp.Ok(projectFile);
    }

    /**
     * 导出
     *
     * @param request  request
     * @param response response
     */
    @RequestMapping(value = "/downloadFile", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseEntity<Resource> export(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String fileId = request.getParameter("fileId");
        if (StringUtils.isNotBlank(fileId)) {
            ProjectFile one = projectFileRepository.getOne(Long.parseLong(fileId));
            String filePath = one.getFilePath();
            String fileName = one.getFileName();
            // 创建FileSystemResource对象
            Resource resource = new FileSystemResource(Paths.get(filePath).toFile());
            // 设置响应头信息
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 返回文件作为响应体
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(resource);

        }
        return ResponseEntity.ok().body(null);
    }

    /**
     * 导出Excel
     *
     * @param projects  request
     * @param response response
     */
    @RequestMapping(value = "/exportExcel", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void exportExcel(HttpServletResponse response,@RequestBody List<Project> projects) throws IOException {
        List<BiaoZunExcelDTO> list = new ArrayList<>(projects.size());
        for (Project project : projects) {
            BiaoZunExcelDTO biaoZunExcelDTO = new BiaoZunExcelDTO();
            biaoZunExcelDTO.setSjzlbzml(project.getSjzlbzml());
            biaoZunExcelDTO.setCnName(project.getCnName());
            biaoZunExcelDTO.setCkwx(project.getCkwx());
            biaoZunExcelDTO.setSyfw(project.getSyfw());
            biaoZunExcelDTO.setFbzt(project.getFbzt());
            biaoZunExcelDTO.setSjzllx(project.getSjzllx());
            list.add(biaoZunExcelDTO);
        }
        // 导出数据位Excel
       commonExcelService.handlerBiaoZunDownload(response.getOutputStream(),list);
    }
}

Service

package com.webank.wedatasphere.qualitis.handler;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.webank.wedatasphere.qualitis.project.constant.ExcelSheetName;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectMuLuRepository;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectRepository;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMuLu;
import com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject;
import com.webank.wedatasphere.qualitis.response.BiaoZunExcelDTO;
import com.webank.wedatasphere.qualitis.response.Resp;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class CommonExcelService {

    private Logger logger = LoggerFactory.getLogger(CommonExcelService.class);
    @Autowired
    private ProjectRepository repository;

    @Autowired
    private ProjectMuLuRepository projectMuLuRepository;

    /**
     * 标准的导入Excel
     *
     * @param fileSavePath
     * @return
     */
    public Resp<?> handlerBiaoZunUpload(String fileSavePath) {
        // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
        try {
            logger.info("附件上传excel===》" + fileSavePath);
            FileInputStream fileInputStream = new FileInputStream(fileSavePath);
            ExcelReader excelReader = new ExcelReader(fileInputStream, null, new ExcelBiaoZunListener(repository, projectMuLuRepository));
            List<Sheet> sheets = excelReader.getSheets();
            Sheet sheet = sheets.get(0);
            sheet.setClazz(BiaoZunExcelDTO.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
            logger.info("读取完成excel===》" + fileSavePath);
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.error(e.getMessage());
        }
        return Resp.Ok();
    }

    /**
     * 标准的导出Excel
     *
     * @param outputStream
     * @return
     */
    public void handlerBiaoZunDownload(OutputStream outputStream, List<BiaoZunExcelDTO> list) throws IOException {
        // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
        try {
            List<ProjectMuLu> muLuRepositoryAll = projectMuLuRepository.findAll();
            for (BiaoZunExcelDTO biaoZunExcelDTO : list) {
                for (ProjectMuLu muLu : muLuRepositoryAll) {
                    if (StringUtils.isNotBlank(biaoZunExcelDTO.getSjzlbzml()) && biaoZunExcelDTO.getSjzlbzml().equals(muLu.getId().toString())) {
                        biaoZunExcelDTO.setSjzlbzml(muLu.getTitle());
                    }
                }
            }
            ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
            Sheet templateRuleSheet = new Sheet(1, 0, BiaoZunExcelDTO.class);
            templateRuleSheet.setSheetName("导出标准");
            List<List<String>> heads = new ArrayList<>();
            List<String> head1 = new ArrayList<>();
            List<String> head2 = new ArrayList<>();
            List<String> head3 = new ArrayList<>();
            List<String> head4 = new ArrayList<>();
            List<String> head5 = new ArrayList<>();
            List<String> head6 = new ArrayList<>();
            head1.add("数据质量名称");
            head2.add("适用范围");
            head3.add("数据质量类型");
            head4.add("参考文献");
            head5.add("所属质量标准目录");
            head6.add("发布状态");
            heads.add(head1);
            heads.add(head2);
            heads.add(head3);
            heads.add(head4);
            heads.add(head5);
            heads.add(head6);
            templateRuleSheet.setHead(heads);
            writer.write(list, templateRuleSheet);
            writer.finish();
            logger.info("写入完成excel===》");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
    }

}


很老很老的easyexcel的API

package com.webank.wedatasphere.qualitis.handler;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectMuLuRepository;
import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectRepository;
import com.webank.wedatasphere.qualitis.project.entity.Project;
import com.webank.wedatasphere.qualitis.project.entity.ProjectMuLu;
import com.webank.wedatasphere.qualitis.response.BiaoZunExcelDTO;
import com.webank.wedatasphere.qualitis.util.LocalTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ExcelBiaoZunListener extends AnalysisEventListener<BiaoZunExcelDTO> {

   private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 100;


    /**
     * 缓存的数据
     */
    private List<BiaoZunExcelDTO> cachedDataList = new ArrayList<>(BATCH_COUNT);
    /**
     * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
     */
    private ProjectRepository projectRepository;

    private ProjectMuLuRepository projectMuLuRepository;

    private List<ProjectMuLu> muLuRepositoryAll;

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     *
     * @param projectRepository
     */
    public ExcelBiaoZunListener(ProjectRepository projectRepository,ProjectMuLuRepository projectMuLuRepository) {
        this.projectRepository = projectRepository;
        this.projectMuLuRepository = projectMuLuRepository;
        this.muLuRepositoryAll = this.projectMuLuRepository.findAll();
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#()}
     * @param context
     */
    @Override
    public void invoke(BiaoZunExcelDTO data, AnalysisContext context) {
        logger.info("解析到一条数据:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = new ArrayList<>(BATCH_COUNT);
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
        saveData();
        logger.info("所有数据解析完成!");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        logger.info("{}条数据,开始存储数据库!", cachedDataList.size());
        for (BiaoZunExcelDTO biaoZunExcelDTO : cachedDataList) {
            if (StringUtils.isBlank(biaoZunExcelDTO.getCnName())){
                continue;
            }
            for (ProjectMuLu muLu : muLuRepositoryAll) {
                if (muLu.getTitle().equals(biaoZunExcelDTO.getSjzlbzml())) {
                    biaoZunExcelDTO.setSjzlbzml(muLu.getId().toString());
                    break;
                }
            }
            // 处理数据
            Project project = new Project();
            project.setCnName(biaoZunExcelDTO.getCnName());
            project.setFbzt(biaoZunExcelDTO.getFbzt());
            project.setSjzlbzml(biaoZunExcelDTO.getSjzlbzml());
            project.setCkwx(biaoZunExcelDTO.getCkwx());
            project.setSyfw(biaoZunExcelDTO.getSyfw());
            project.setSjzllx(biaoZunExcelDTO.getSjzllx());
            project.setCreateTime(LocalTimeUtils.getCurrentTime());
            project.setCreateUser("admin");
            project.setCreateUserFullName("admin(管理员)");
            project.setDescription(biaoZunExcelDTO.getCkwx());
            project.setName(biaoZunExcelDTO.getCnName());
            project.setProjectType(1);
            project.setModifyTime(LocalTimeUtils.getCurrentTime());
            projectRepository.save(project);
        }
        logger.info("存储数据库成功!");
    }
}

posted on 2024-02-26 11:35  白嫖老郭  阅读(34)  评论(0编辑  收藏  举报

导航