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

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("存储数据库成功!");
}
}

通用的SpringBoot集成的文件上传与下载的更多相关文章

  1. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  2. SpringBoot项目实现文件上传和邮件发送

    前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...

  3. SpringBoot整合阿里云OSS文件上传、下载、查看、删除

    1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...

  4. springboot+web文件上传和下载

    一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...

  5. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  6. 七、springBoot 简单优雅是实现文件上传和下载

    前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...

  7. Springboot如何启用文件上传功能

    网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...

  8. 19、文件上传与下载/JavaMail邮件开发

    回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...

  9. Http服务器实现文件上传与下载(三)

    一.引言 在前2章的内容基本上已经讲解了整个的大致流程.在设计Http服务器时,我设计为四层的结构,最底层是网络传输层,就是socket编程.接着一层是请求和响应层,叫做Request和Respons ...

  10. 基于hap的文件上传和下载

    序言 现在,绝大部分的应用程序在很多的情况下都需要使用到文件上传与下载的功能,在本文中结合hap利用spirng mvc实现文件的上传和下载,包括上传下载图片.上传下载文档.前端所使用的技术不限,本文 ...

随机推荐

  1. Event-Stream技术

    服务端 websocket和event-stream的优缺点 WebSocket和Event-Stream(Server-Sent Events)都是实现实时通信的技术,但是它们各自有不同的优缺点. ...

  2. 前端学习openLayers配合vue3(获取矢量图的个数,省份的个数)

    矢量图层绘制了一个中国地图,我们获取一下矢量图层的个数 关键代码 map .getLayers()//获取所有图层 .item(1)//获取矢量图层 .getSource() .on("ch ...

  3. w3cschool-Nginx 入门指南

    https://www.w3cschool.cn/nginx/ Nginx 的特点 Nginx 做为 HTTP 服务器,有以下几项基本特性: 处理静态文件,索引文件以及自动索引:打开文件描述符缓冲. ...

  4. w3cschool-微信小程序开发文档-指南

    https://www.w3cschool.cn/weixinapp/9wou1q8j.html https://www.w3cschool.cn/miniappbook/ 微信小程序 小程序简介 小 ...

  5. linux:搭建 WordPress 个人站点

    参考:链接 介绍 WordPress 是一款使用 PHP 语言开发的博客平台,您可使用通过 WordPress 搭建属于个人的博客平台.本文以 CentOS 6.5 操作系统为例,手动搭建 WordP ...

  6. [记录点滴]Spring Boot Admin源码分析笔记

    [记录点滴]Spring Boot Admin源码分析笔记 0x00 摘要 本文是过去使用Spring Boot Admin时候分析源码的笔记.虽然比较简单,但是也可以看出Spring Boot Ad ...

  7. 耳分解、双极定向和 P9394 Solution

    耳分解 设无向图 \(G'(V',E')\subset G(V,E)\),简单路径或简单环 \(P:x_1\to \dots \to x_k\) 被称为 \(G\) 关于 \(G'\) 的耳,当且仅当 ...

  8. RocketMQ实战—9.营销系统代码初版

    大纲 1.基于条件和画像筛选用户的业务分析和实现 2.全量用户促销活动数据模型分析以及创建操作 3.Producer和Consumer的工程代码实现 4.基于抽象工厂模式的消息推送实现 5.全量用户促 ...

  9. 5. Docker 本地镜像发布到阿里云

    5. Docker 本地镜像发布到阿里云 @ 目录 5. Docker 本地镜像发布到阿里云 1. 本地镜像发布到阿里云流程 最后: 1. 本地镜像发布到阿里云流程 镜像的生成方法: 基于当前容器创建 ...

  10. 管理虚拟机(virsh)

    [root@kvm1 qemu]# virsh --help 开启和关闭 [root@kvm1 qemu]# virsh virsh # help list virsh # list virsh # ...