From e704d38c7764884a549911082a67d566c1923a17 Mon Sep 17 00:00:00 2001 From: zhangqinbin <181961702@qq.com> Date: Tue, 31 Oct 2023 09:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=91=E5=B1=B1WPS=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extend/wps/controller/WpsController.java | 68 ++++++++++++++++++- .../ebtp/extend/wps/service/WpsService.java | 2 + .../wps/service/impl/WpsServiceImpl.java | 4 +- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/controller/WpsController.java b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/controller/WpsController.java index 550dc01..c50d38d 100644 --- a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/controller/WpsController.java +++ b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/controller/WpsController.java @@ -2,6 +2,9 @@ package com.chinaunicom.mall.ebtp.extend.wps.controller; import com.alibaba.fastjson.JSON; +import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api.AttachmentClient; +import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail; +import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity; import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.SecurityEntity; import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse; import com.chinaunicom.mall.ebtp.common.log.OperationLogDetail; @@ -9,6 +12,7 @@ import com.chinaunicom.mall.ebtp.common.log.enums.EbtpLogBusinessModule; import com.chinaunicom.mall.ebtp.common.log.enums.EbtpLogType; import com.chinaunicom.mall.ebtp.extend.feign.client.UnicomOAuthClient; import com.chinaunicom.mall.ebtp.extend.wps.entity.KingSoftFileSaveVo; +import com.chinaunicom.mall.ebtp.extend.wps.enums.WpsExceptionEnum; import com.chinaunicom.mall.ebtp.extend.wps.service.WpsService; import com.chinaunicom.mall.ebtp.extend.wps.util.HttpsUtils; import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsClientInVo; @@ -18,7 +22,9 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.StringEscapeUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; @@ -26,9 +32,11 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.PrintWriter; +import java.io.*; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.Optional; @Slf4j @RestController @@ -45,6 +53,11 @@ public class WpsController { @Value("${KingSoftWps.kswpsurl}") private String kswpsurl; + + /* 附件工具 */ + @Autowired + private AttachmentClient attachmentClient; + /** * 获得编辑或查看链接 * @@ -123,6 +136,57 @@ public class WpsController { public byte[] getKSFile(@RequestParam("key") String key) { return this.wpsService.getKSWpsFile(key); } + /** + * (金山)沃文档回调-查询文档 + * 编辑 permission=write + * 预览 permission=read + * @param + * @return + */ + @ApiOperation("(金山)沃文档回调-查询文档") + @GetMapping("/kingSoft/file2") + public boolean getKSFile2(HttpServletResponse response,@RequestParam("key") String key) { + String fileId = this.wpsService.downloadDecrypt(key); + + List businessIdList = new ArrayList<>(); + + businessIdList.add(fileId); + + Optional optional = attachmentClient.findByBusinessId(businessIdList); + + AttachmentDetail detail = optional.get(); + + List entityList = detail.get(fileId); + + Optional optional1 = attachmentClient.downloadFileByObjectId(entityList.get(0).getId()); + + // 设置响应的头信息,告诉浏览器文件的大小和下载方式 + response.setHeader("Content-Length", String.valueOf(optional1.get().length)); + response.setHeader("Content-Disposition", "attachment; filename=\"" + entityList.get(0).getFilename() + "\""); + + try{ + InputStream word = new ByteArrayInputStream(optional1.get()); + // 创建输入输出流 + BufferedInputStream inStream = new BufferedInputStream(word); + BufferedOutputStream outStream = new BufferedOutputStream(response.getOutputStream()); + + // 读取文件并写入到输出流 + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inStream.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + + // 关闭输入输出流 + inStream.close(); + outStream.close(); + }catch (Exception e){ + log.error("下载失败",e); + WpsExceptionEnum.FRAME_EXCEPTION_NO.customValidName("下载失败", true); + } + + return true; + } /** * 沃文档回调-保存文档 * @return diff --git a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/WpsService.java b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/WpsService.java index 4cc38f9..92362ec 100644 --- a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/WpsService.java +++ b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/WpsService.java @@ -24,5 +24,7 @@ public interface WpsService{ byte[] getKSWpsFile(String id); + public String downloadDecrypt(String key); + Map saveKSFile(KingSoftFileSaveVo vo); } diff --git a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/impl/WpsServiceImpl.java b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/impl/WpsServiceImpl.java index 1fa086c..646e577 100644 --- a/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/impl/WpsServiceImpl.java +++ b/src/main/java/com/chinaunicom/mall/ebtp/extend/wps/service/impl/WpsServiceImpl.java @@ -547,8 +547,8 @@ public class WpsServiceImpl implements WpsService { return key; } - - private String downloadDecrypt(String key){ + @Override + public String downloadDecrypt(String key){ String val = RSA.decrypt(key,kswprivateKey); CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌无效",val.indexOf("_")<0);