文档库--下载和导入接口

This commit is contained in:
zhn
2022-02-25 10:11:30 +08:00
parent bc8e8c3f2a
commit c62cd6b5df
4 changed files with 252 additions and 7 deletions
@@ -20,6 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -136,7 +137,12 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
@Override
public List<OSSFile> getFileInfosByConnectId(String connectId) {
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OSSFile::getConnectId,connectId);
if(connectId.contains(",")){
wrapper.eq(OSSFile::getConnectId, Arrays.asList(connectId.split(",")));
}else{
wrapper.eq(OSSFile::getConnectId,connectId);
}
List<OSSFile> list = this.list(wrapper);
return list;
}
@@ -1,11 +1,13 @@
package com.jero.modules.document.controller;
import cn.hutool.json.JSON;
import cn.hutool.core.util.ZipUtil;
import com.aliyuncs.utils.IOUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.base.controller.JeroController;
import com.jero.common.system.query.QueryGenerator;
import com.jero.modules.document.entity.BussDocumentLibraryEO;
@@ -13,19 +15,41 @@ import com.jero.modules.document.service.IBussDocumentLibraryEOService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
/**
* @Description: 文档库信息表
* @Author: jero-boot
* @Date: 2022-01-21
@@ -349,6 +373,120 @@ public class BussDocumentLibraryEOController extends JeroController<BussDocument
}
@ApiOperation(value = "导出excel")
@PostMapping(value = "/exportExcel")
public void exportExcel(@RequestBody Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
OutputStream os = null;
Workbook workbook = null;
try {
String fileName = "11111";
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/force-download");
// 导出所有数据
List<Map<String,Object>> datas = new ArrayList<>();
workbook = bussDocumentLibraryEOService.exportDatas(datas);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
}
}
@ApiOperation(value = "带文件导出")
@PostMapping(value = "/exportZip")
public void exportZip(@RequestBody Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
bussDocumentLibraryEOService.exportZip(map,response,request);
}
@ApiOperation(value = "模板下载")
@GetMapping(value = "/exportTemplate")
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
OutputStream os = null;
OutputStream excelOS = null;
HSSFWorkbook workbook = new HSSFWorkbook();
String fileOriName = "导入模板";
String filePath = "";
try{
//创建临时文件夹
File nowFile = new File(filePath);
if (nowFile.exists()){
nowFile.delete();
}
nowFile.mkdirs();
String fileName = "导入模板.xls";
HSSFSheet sheetItems = workbook.createSheet("模板");
sheetItems.setDefaultColumnWidth(16);
HSSFCellStyle cellStyle =workbook.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Row rowHeader = sheetItems.createRow(0);//开始创建标题行
String exportFieldName = "标准编号,标题,发布时间";
if (StringUtils.isNotBlank(exportFieldName)) {
String[] headerArr = exportFieldName.split(",");
for (int i=0;i < headerArr.length; i++) {
rowHeader.createCell(i).setCellValue(headerArr[i]);
}
}
String repFileName = fileName.replaceAll("/","_");
excelOS = new FileOutputStream(filePath + "/" + repFileName);
response.setHeader("Content-Disposition",
"attachment; filename=\""+ fileOriName+".zip");
response.setContentType("application/force-download");
response.flushBuffer();
os = response.getOutputStream();
workbook.write(excelOS);
excelOS.flush();
excelOS.close();
ZipUtil.zip(filePath,filePath+".zip");
FileInputStream fis = new FileInputStream(filePath+".zip");
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
os.close(); // 后开先关
fis.close(); // 先开后关
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(excelOS);
}
}
@ApiOperation(value = "导入.zip")
@PostMapping(value = "/importZip")
public Result<?> importZip(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
//验证文件名是否合格
//截取后缀名
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos+1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0,pos);
//判断上传文件必须是zip
if (!str.equals("zip")) {
return Result.error("请上传zip文件");
}
return Result.OK("导入成功");
}
@ApiOperation(value = "推送")
@GetMapping(value = "/pullMessage")
public Result<?> pullMessage(String ids) {
return Result.OK("推送成功");
}
@@ -3,7 +3,10 @@ package com.jero.modules.document.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.modules.document.entity.BussDocumentLibraryEO;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@@ -131,5 +134,11 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
*/
void cancelSubscribe(String id);
Workbook exportDatas(List<Map<String,Object>> datas);
void exportZip(Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request);
}
@@ -1,5 +1,6 @@
package com.jero.modules.document.service.impl;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -7,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.constant.enums.ModuleEnum;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser;
import com.jero.generater.modules.online.cgform.entity.OnlCgformField;
import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServiceImpl;
@@ -28,11 +30,19 @@ import com.jero.modules.tag.entity.OnlCgformArea;
import com.jero.modules.tag.service.impl.OnlCgformAreaServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -45,6 +55,8 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static cn.hutool.core.io.FileUtil.copyFile;
/**
* @Description: 文档库信息表
* @Author: jero-boot
@@ -985,6 +997,8 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
iOnlCgformSubscribeService.remove(lambdaQueryWrapper);
}
/**
* 判断两个集合元素是否完全一致
* @param list
@@ -1015,4 +1029,82 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
mapResult.put(key,map.get(key) != null ?map.get(key):"");
}
}
/**
* 导出excel
* @param datas
*/
@Override
public Workbook exportDatas(List<Map<String, Object>> datas) {
Workbook workbook = new XSSFWorkbook();
return workbook;
}
/**
* 带文件导出
* @param datas
* @return
*/
@Override
public void exportZip(Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request) {
try {
String fileName = "带文件导出.zip";
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/force-download");
// 导出所有数据
List<Map<String,Object> > datas = new ArrayList<>();
List<String> pdfFileIdList = new ArrayList<>();
//从查询的数据中,过滤出文件(根据字段类型)
//文件信息
List<OSSFile> oSSFileList = iOSSFileService.getFileInfosByConnectId(StringUtils.join(pdfFileIdList,","));
//创建临时文件夹
String uuid = "";
String fileNowPath = "path" + "带文件导出";
File nowFile = new File(fileNowPath);
if (nowFile.exists()){
nowFile.delete();
}
nowFile.mkdirs();
try(Workbook workbook = exportDatas(datas);
OutputStream excelOS = new FileOutputStream(fileNowPath + File.separator + "军品数据.xls");
OutputStream os = response.getOutputStream();
) {
for (OSSFile oSSFile : oSSFileList) {
//文件路径
String path = oSSFile.getUrl();
copyFile(path,fileNowPath+File.separator);
}
workbook.write(excelOS);
excelOS.flush();
ZipUtil.zip(fileNowPath,fileNowPath+".zip");
FileInputStream fis = new FileInputStream(fileNowPath+".zip");
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
fis.close();
} catch (IOException e) {
throw new JeroBootException("下载文件失败,请重试");
}finally {
// File file = new File(uploadFilePath + "/tempZip/" + uuid);
// FileUnZip.deleteDir(file);
}
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
}
}
}