From ba0f2acb76fe4a04d093789a68880918da6670ba Mon Sep 17 00:00:00 2001 From: PoffyZhang <99775271@qq.com> Date: Tue, 15 Aug 2023 14:56:15 +0800 Subject: [PATCH] =?UTF-8?q?wps=20=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/pmapi/wps/contants/WpsContant.java | 16 ++++ .../pmapi/wps/controller/WpsConvertController.java | 14 +++- .../pmapi/wps/manage/WpsConvertManage.java | 98 +++++++++++++++++++++- .../pmapi/wps/service/IConvertService.java | 5 ++ .../pmapi/wps/service/impl/ConvertService.java | 19 +++++ 5 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java b/pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java new file mode 100644 index 0000000..ad581dd --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/wps/contants/WpsContant.java @@ -0,0 +1,16 @@ +package com.ningdatech.pmapi.wps.contants; + +/** + * @Classname WpsContant + * @Description + * @Date 2023/8/15 14:09 + * @Author PoffyZhang + */ +public interface WpsContant { + + String WPS_CONVERT_URL_ONLINE = "http://10.53.157.47"; + String FIX_FILE_PATH = "/opt/file/"; + String PDF_PATH = "/opt/file/offices2pdf.pdf"; + String PDF_CONTENT_TYPE = "application/pdf"; + String PDF_NAME = "offices2pdf.pdf"; +} diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java b/pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java index 1f4d953..c229757 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/wps/controller/WpsConvertController.java @@ -13,8 +13,11 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; + +import javax.servlet.http.HttpServletResponse; import java.security.NoSuchAlgorithmException; import java.util.Objects; @@ -32,6 +35,10 @@ public class WpsConvertController { @Autowired private IRefreshTokenService refreshTokenService; + @Autowired + private WpsConvertManage wpsConvertManage; + + @Value("${irs.interface-refresh.request-token-url}") private String govRequestTokenUrl; @Value("${irs.interface-refresh.refresh-token-url}") @@ -39,7 +46,12 @@ public class WpsConvertController { @GetMapping("/office2pdf") public String office2pdf(){ - return WpsConvertManage.offs2Pdf(); + return wpsConvertManage.offs2Pdf(); + } + + @GetMapping("/downloadToPdfStream") + public void downloadToPdfStream(@RequestParam Long fileId, HttpServletResponse response){ + wpsConvertManage.downloadToPdfStream(fileId,response); } @GetMapping("/test-core") diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java index f69a083..e8fc4ea 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/wps/manage/WpsConvertManage.java @@ -1,8 +1,25 @@ package com.ningdatech.pmapi.wps.manage; +import cn.hutool.core.util.CharsetUtil; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.OSSObject; +import com.aliyun.oss.model.ObjectMetadata; +import com.ningdatech.file.properties.FileServerProperties; +import com.ningdatech.file.service.FileService; +import com.ningdatech.pmapi.wps.contants.WpsContant; import com.suwell.ofd.custom.agent.AtomAgent; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.net.URLEncoder; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -13,10 +30,18 @@ import java.util.List; * @Author PoffyZhang */ @Slf4j +@Component +@RequiredArgsConstructor public class WpsConvertManage { + + @Resource + private FileService fileService; + + private final FileServerProperties fileServerProperties; + // HTTPAgent ha = new HTTPAgent("http://127.0.0.1:8090"); //2、定义方法实现 多个文件合并转为1个pdf文件 - public static String offs2Pdf() { + public String offs2Pdf() { AtomAgent ha = new AtomAgent("http://10.53.157.47"); try { @@ -61,7 +86,74 @@ public class WpsConvertManage { return "转换失败"; } - public static void main(String[] args) { - offs2Pdf(); + public void downloadToPdfStream(Long fileId, HttpServletResponse response) { + com.ningdatech.file.entity.File file = fileService.getById(fileId); + downloadAndToPdf(file, response); + } + + private void downloadAndToPdf(com.ningdatech.file.entity.File file, HttpServletResponse response) { + OSS oss = new OSSClientBuilder().build(fileServerProperties.getAli().getEndpoint(), fileServerProperties.getAli().getAccessKeyId(), fileServerProperties.getAli().getAccessKeySecret()); + AtomAgent ha = new AtomAgent(WpsContant.WPS_CONVERT_URL_ONLINE); + try (OSSObject ossObject = oss.getObject(file.getBucket(), file.getPath()); + InputStream stream = ossObject.getObjectContent(); + ServletOutputStream outputStream = response.getOutputStream()) { + String filePath = WpsContant.FIX_FILE_PATH + file.getOriginalFileName(); + convert(stream,filePath); + //转换PDF + List fileList =new ArrayList(); + //2)、定义转换后的pdf文件输出流 + OutputStream out = null; + //3)、添加原文件到集合 + fileList.add(new File(filePath)); + //4)、赋值转换后的pdf文件输出流 + File pdfFile = new File(WpsContant.PDF_PATH); + out = new FileOutputStream(pdfFile); + //5)、调用方法,执行将多个文件转为pdf文件 + ha.OFDToPDF(fileList, out); + FileInputStream fileInputStream = new FileInputStream(pdfFile); + + ObjectMetadata metadata = ossObject.getObjectMetadata(); + response.setContentType(WpsContant.PDF_CONTENT_TYPE); + String fileName = URLEncoder.encode(WpsContant.PDF_NAME, CharsetUtil.UTF_8); + response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName); + byte[] bytes = new byte[4096]; + int read; + while ((read = fileInputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + outputStream.flush(); + for(File f : fileList){ + //用完就删 + f.deleteOnExit(); + } + }catch (Exception e){ + log.error(e.getMessage()); + }finally { + oss.shutdown(); + try { + ha.close(); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + } + + public static void convert(InputStream inputStream, String filePath) { + try { + File file = new File(filePath); + FileOutputStream outputStream = new FileOutputStream(file); + + int bytesRead; + byte[] buffer = new byte[1024]; + + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + + outputStream.close(); + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java b/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java new file mode 100644 index 0000000..31ae6a7 --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/IConvertService.java @@ -0,0 +1,5 @@ +package com.ningdatech.pmapi.wps.service; + +public interface IConvertService { + +} diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java b/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java new file mode 100644 index 0000000..aef699a --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/wps/service/impl/ConvertService.java @@ -0,0 +1,19 @@ +package com.ningdatech.pmapi.wps.service.impl; + +import com.ningdatech.pmapi.wps.service.IConvertService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * @Classname ConvertService + * @Description + * @Date 2023/8/15 10:56 + * @Author PoffyZhang + */ +@Service +@Slf4j +public class ConvertService implements IConvertService { + + + +}