From c62cd6b5df5c692c108268c002a75423d87afa5f Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Fri, 25 Feb 2022 10:11:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=BA=93--=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=92=8C=E5=AF=BC=E5=85=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oss/service/impl/OSSFileServiceImpl.java | 8 +- .../BussDocumentLibraryEOController.java | 148 +++++++++++++++++- .../IBussDocumentLibraryEOService.java | 9 ++ .../BussDocumentLibraryEOServiceImpl.java | 94 ++++++++++- 4 files changed, 252 insertions(+), 7 deletions(-) diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java index a27af548e..80b700b0f 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java @@ -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 impl @Override public List getFileInfosByConnectId(String connectId) { LambdaQueryWrapper 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 list = this.list(wrapper); return list; } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/controller/BussDocumentLibraryEOController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/controller/BussDocumentLibraryEOController.java index 778517302..4e3aedda4 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/controller/BussDocumentLibraryEOController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/controller/BussDocumentLibraryEOController.java @@ -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 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> 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 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("推送成功"); + } + + diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IBussDocumentLibraryEOService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IBussDocumentLibraryEOService.java index 2cdad8e2e..743328b80 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IBussDocumentLibraryEOService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/IBussDocumentLibraryEOService.java @@ -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> datas); + + void exportZip(Map map, + HttpServletResponse response, + HttpServletRequest request); + } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java index 747e82915..b2b617d31 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java @@ -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> datas) { + Workbook workbook = new XSSFWorkbook(); + return workbook; + + } + + /** + * 带文件导出 + * @param datas + * @return + */ + @Override + public void exportZip(Map map, + HttpServletResponse response, + HttpServletRequest request) { + try { + String fileName = "带文件导出.zip"; + response.setHeader("Content-Disposition", + "attachment; filename=" + fileName); + response.setContentType("application/force-download"); + // 导出所有数据 + List > datas = new ArrayList<>(); + + List pdfFileIdList = new ArrayList<>(); + //从查询的数据中,过滤出文件(根据字段类型) + + //文件信息 + List 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("下载文件失败,请重试"); + } + } + + }