通用的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("存储数据库成功!");
}
}
通用的SpringBoot集成的文件上传与下载的更多相关文章
- SpringBoot下文件上传与下载的实现
原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- SpringBoot整合阿里云OSS文件上传、下载、查看、删除
1. 开发前准备 1.1 前置知识 java基础以及SpringBoot简单基础知识即可. 1.2 环境参数 开发工具:IDEA 基础环境:Maven+JDK8 所用技术:SpringBoot.lom ...
- springboot+web文件上传和下载
一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- 七、springBoot 简单优雅是实现文件上传和下载
前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...
- Springboot如何启用文件上传功能
网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...
- 19、文件上传与下载/JavaMail邮件开发
回顾: 一. 监听器 生命周期监听器 ServletRequestListener HttpSessionListener ServletContextListener 属性监听器 ServletRe ...
- Http服务器实现文件上传与下载(三)
一.引言 在前2章的内容基本上已经讲解了整个的大致流程.在设计Http服务器时,我设计为四层的结构,最底层是网络传输层,就是socket编程.接着一层是请求和响应层,叫做Request和Respons ...
- 基于hap的文件上传和下载
序言 现在,绝大部分的应用程序在很多的情况下都需要使用到文件上传与下载的功能,在本文中结合hap利用spirng mvc实现文件的上传和下载,包括上传下载图片.上传下载文档.前端所使用的技术不限,本文 ...
随机推荐
- Log4net的使用教程
不怎么爱写日志,(就是比较懒),后来遇上bug了找不到问题,才决定好好使用日志 其实说实话,log4配置的那些东西记不太住,但也不需要记住什么.百度就有现成的,自己跟着网上配置好之后,稍微记录一下,为 ...
- [转]CSS、LESS和SASS(SCSS)的区别以及Ruby Sass、LibSass、Node Sass、Dart Sass之间的关系
随着前端开发的不断发展,CSS也逐渐延伸出了很多新的语言,less和Sass就是其中两种,下面我们就一起来看看它们到底有何区别. SASS和LESS SASS(英文全称:Syntactically A ...
- ThreeJs-11精通着色器编程(重难点)
着色器语言编程比较重要,后面的几个章节都会围绕这个来做特效 一.初识着色器语言 首先什么叫做着色器,他是一种语言,首先需要设置为着色器材质,然后在材质里面书写一些语言,可以告诉他顶点,然后去自定义一些 ...
- Verilog5_有限状态机
一.有限状态机(Finite State Machine, FSM)基本概念 有限状态机是由寄存器组和组合逻辑构成的硬件时序电路: 其状态只能在同一时钟跳变沿从一个状态转向另一个状态: ...
- CDS标准视图:功能位置信息 I_FunctionalLocationData
视图名称:I_FunctionalLocationData 视图类型:基础视图 视图代码: 点击查看代码 @EndUserText.label: 'Functional Location Data' ...
- c# C#设置WebBrowser使用Edge内核
开始尝试是用 Microsoft.Toolkit.Forms.UI.Controls.WebView,后来发现一大堆问题,还要求WIN10 SDK的版本之类的. 网上看到的简单的解决办法(只需要修改注 ...
- TCP的网络编程基础
服务器建立 ServerSocket 对象ServerSocket 对象负责等待客户端请求建立套接字连接,类似邮局某个窗口中的业务员.也就是说, 服务器必须事先建立一个等待客户请求建立套接字的 连接 ...
- C#中DevExpress的GridControl相关表格的属性总结
这里用到了Dev中的GridControl表格做数据查询展示,要求字号大一些,单行选中深色显示 // grid1 this.grid1.Dock = System.Windows.Forms.Dock ...
- 移动端如何自动适配px
<script type="text/javascript"> (function(doc, win) { var docEl = doc.documentElemen ...
- 更快更省更好用!天翼云云原生一体机iStack打通物云最后一公里!
近年来,随着企业数字化转型的深入,从传统 IT 架构向云原生架构转型,已经成为企业谋求更高质量发展的必由之路.然而,云原生技术复杂度高,运维成本高,且技术工具间的集成度不足.打破云原生技术应用门槛,以 ...