文档库--带文件导出
This commit is contained in:
+1
-1
@@ -138,7 +138,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
|
||||
public List<OSSFile> getFileInfosByConnectId(String connectId) {
|
||||
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
|
||||
if(connectId.contains(",")){
|
||||
wrapper.eq(OSSFile::getConnectId, Arrays.asList(connectId.split(",")));
|
||||
wrapper.in(OSSFile::getConnectId, Arrays.asList(connectId.split(",")));
|
||||
}else{
|
||||
wrapper.eq(OSSFile::getConnectId,connectId);
|
||||
}
|
||||
|
||||
+134
-68
@@ -1,5 +1,6 @@
|
||||
package com.jero.modules.document.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
import com.aliyuncs.utils.IOUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@@ -44,6 +45,7 @@ import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.aspectj.util.FileUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -52,6 +54,8 @@ 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;
|
||||
@@ -1064,6 +1068,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
*/
|
||||
@Override
|
||||
public void exportExcel(Map<String, Object> map, HttpServletResponse response, HttpServletRequest request) {
|
||||
String flag = "file";
|
||||
OutputStream os = null;
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
try {
|
||||
@@ -1087,7 +1092,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
&& !"sys_org_code".equals(e.getDbFieldName())
|
||||
&& !"warn_flag".equals(e.getDbFieldName())).collect(Collectors.toList());
|
||||
//只导出数据时,过滤掉文件类型
|
||||
fieldListTemp = fieldListTemp.stream().filter(e -> !FieldTypeEnum.FILE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
|
||||
if(!"file".equals(flag)){
|
||||
fieldListTemp = fieldListTemp.stream().filter(e -> !FieldTypeEnum.FILE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
|
||||
}
|
||||
headerFieldList = fieldListTemp.stream().map(OnlCgformField::getDbFieldTxt).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
@@ -1111,15 +1118,75 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
// 创建数据
|
||||
createDatas(workbook, sheet, datas, header, fieldListTemp);
|
||||
|
||||
os = response.getOutputStream();
|
||||
workbook.write(os);
|
||||
os.flush();
|
||||
if(!"file".equals(flag)){
|
||||
//不带文件导出
|
||||
os = response.getOutputStream();
|
||||
workbook.write(os);
|
||||
os.flush();
|
||||
}else{
|
||||
String path = uploadpath + "/tempZip";
|
||||
File fileTemp = new File(path);
|
||||
if (fileTemp.exists()){
|
||||
fileTemp.delete();
|
||||
}
|
||||
fileTemp.mkdirs();
|
||||
//excel
|
||||
OutputStream excelOS = new FileOutputStream(path + File.separator + "文档库.xls");
|
||||
workbook.write(excelOS);
|
||||
excelOS.flush();
|
||||
//带文件导出
|
||||
//过滤出文件字段
|
||||
List<OnlCgformField> onlCgformFieldList = fieldListTemp.stream()
|
||||
.filter(e -> FieldTypeEnum.FILE.getValue().equals(e.getFieldShowType()))
|
||||
.collect(Collectors.toList());
|
||||
List<String> fileFieldList = onlCgformFieldList.stream().map(OnlCgformField::getDbFieldName).collect(Collectors.toList());
|
||||
|
||||
for (Map<String, Object> data : datas) {
|
||||
if(StringUtils.isBlank((String) data.get("serial_number"))){
|
||||
continue;
|
||||
}
|
||||
String fileNowPath = uploadpath + "/tempZip/" + (String) data.get("serial_number");
|
||||
File file = new File(fileNowPath);
|
||||
if (file.exists()){
|
||||
file.delete();
|
||||
}
|
||||
file.mkdirs();
|
||||
List<String> valueList = new ArrayList<>();
|
||||
for (String field : fileFieldList) {
|
||||
String value = (String) data.get(field);
|
||||
if(StringUtils.isNotBlank(value)){
|
||||
valueList.add(value);
|
||||
}
|
||||
}
|
||||
//每一个编号下的所有的文件
|
||||
if(valueList.size()!= 0){
|
||||
List<OSSFile> fileInfosList = iOSSFileService.getFileInfosByConnectId(StringUtils.join(valueList, ","));
|
||||
for (OSSFile ossFile : fileInfosList) {
|
||||
String url = ossFile.getUrl();
|
||||
copyFile(url, fileNowPath+ "/"+ossFile.getFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
ZipUtil.zip(path,path+".zip");
|
||||
//文件
|
||||
FileInputStream fis = new FileInputStream(path+".zip");
|
||||
os = response.getOutputStream();
|
||||
|
||||
int len = 0;
|
||||
while ((len = fis.read()) != -1) {
|
||||
os.write(len);
|
||||
}
|
||||
os.flush();
|
||||
fis.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new JeroBootException("下载文件失败,请重试");
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
File file = new File(uploadpath + "/tempZip");
|
||||
FileUtil.deleteContents(file);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void createDatas(Workbook workbook, Sheet sheet, List<Map<String, Object>> datas, String header, List<OnlCgformField> fieldList) {
|
||||
@@ -1128,10 +1195,23 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
//数据字典
|
||||
List<SysDictItem> sysDictItems = sysDictItemServiceImpl.selectItemsAll();
|
||||
//过滤出下拉单选和下拉多选的字段
|
||||
List<OnlCgformField> onlCgformFieldList = fieldList.stream()
|
||||
.filter(e -> FieldTypeEnum.PULL_SINGLE.getValue().equals(e.getFieldShowType())
|
||||
|| FieldTypeEnum.PULL_MORE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
|
||||
//文件字段
|
||||
List<OnlCgformField> fileFieldList = fieldList.stream().filter(e -> FieldTypeEnum.FILE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
|
||||
//过滤出所有文件字段对应的connect_id
|
||||
List<String> connectIdList = new ArrayList<>();
|
||||
for (Map<String, Object> data : datas) {
|
||||
for (OnlCgformField onlCgformField : fileFieldList) {
|
||||
String value = (String) data.get(onlCgformField.getDbFieldName());
|
||||
if(StringUtils.isNotBlank(value)){
|
||||
connectIdList.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
//所有的文件信息
|
||||
List<OSSFile> fileInfos = new ArrayList<>();
|
||||
if(connectIdList.size() != 0){
|
||||
fileInfos = iOSSFileService.getFileInfosByConnectId(StringUtils.join(connectIdList, ","));
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (Map<String, Object> data : datas) {
|
||||
@@ -1153,14 +1233,26 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
if (ObjectUtils.isNotEmpty(data.get(onlCgformField.getDbFieldName()))) {
|
||||
value = sdf.format(data.get(onlCgformField.getDbFieldName()));
|
||||
}
|
||||
} else {
|
||||
}else if(FieldTypeEnum.FILE.getValue().equals(onlCgformField.getFieldShowType())){
|
||||
//处理文件名称(由connect_id转化为文件名)
|
||||
if(fileInfos.size() != 0){
|
||||
List<OSSFile> ossFileList = fileInfos.stream()
|
||||
.filter(e -> e.getConnectId().equals(data.get(onlCgformField.getDbFieldName())))
|
||||
.collect(Collectors.toList());
|
||||
if(ossFileList.size()!= 0){
|
||||
List<String> fileNameList = ossFileList.stream().map(OSSFile::getFileName).collect(Collectors.toList());
|
||||
value = StringUtils.join(fileNameList,",");
|
||||
}else{
|
||||
value = "";
|
||||
}
|
||||
}
|
||||
}else {
|
||||
value = (String) data.get(onlCgformField.getDbFieldName());
|
||||
}
|
||||
row.createCell(sheetNum).setCellValue(value);
|
||||
sheetNum++;
|
||||
}
|
||||
}
|
||||
int a = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1208,68 +1300,15 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
|
||||
/**
|
||||
* 带文件导出
|
||||
*
|
||||
* @param datas
|
||||
* @return
|
||||
* @param map
|
||||
* @param response
|
||||
* @param request
|
||||
*/
|
||||
@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("下载文件失败,请重试");
|
||||
// }
|
||||
exportExcel(map,response,request);
|
||||
}
|
||||
|
||||
|
||||
@@ -1434,5 +1473,32 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
|
||||
|
||||
}
|
||||
|
||||
public static void copyFile(String oldFile, String newFile) throws IOException {
|
||||
|
||||
File file2 = new File(newFile);
|
||||
if(!file2.exists()){
|
||||
file2.createNewFile();
|
||||
}
|
||||
// file2.mkdir();
|
||||
File temp = new File(oldFile );
|
||||
|
||||
if (temp.isFile()) {
|
||||
try(FileInputStream in = new FileInputStream(temp);
|
||||
FileOutputStream ou = new FileOutputStream(newFile);) {
|
||||
|
||||
byte[] bs = new byte[1024];
|
||||
int count = 0;
|
||||
while ((count = in.read(bs, 0, bs.length)) != -1) {
|
||||
ou.write(bs, 0, count);
|
||||
}
|
||||
ou.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IOException("复制文件失败!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user