From 73c9754540a4e2075926c52f2c0e9837d210d9eb Mon Sep 17 00:00:00 2001 From: wangchengjun <616865598@qq.com> Date: Tue, 7 Jun 2022 17:54:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E4=B8=AD=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/config/shiro/ShiroConfig.java | 1 + .../com/jero/modules/oss/SplitFileUtils.java | 35 ++++++++++ .../oss/controller/SplitFileController.java | 67 +++++++++++++++++++ .../split/service/impl/FileSpiltService.java | 23 +++---- .../src/main/resources/application-dev.yml | 3 +- 5 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/SplitFileUtils.java create mode 100644 jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/shiro/ShiroConfig.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/shiro/ShiroConfig.java index 83dc7b4c1..fe23ed6d2 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/shiro/ShiroConfig.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/config/shiro/ShiroConfig.java @@ -93,6 +93,7 @@ public class ShiroConfig { filterChainDefinitionMap.put("/sys/common/static/**", "anon");//图片预览 &下载文件不限制token filterChainDefinitionMap.put("/sys/common/pdf/**", "anon");//pdf预览 filterChainDefinitionMap.put("/sys/common/download/**", "anon");//文件预览 + filterChainDefinitionMap.put("/sys/split/file/**", "anon");//图片预览 filterChainDefinitionMap.put("/generic/**", "anon");//pdf预览需要文件 filterChainDefinitionMap.put("/", "anon"); filterChainDefinitionMap.put("/doc.html", "anon"); diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/SplitFileUtils.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/SplitFileUtils.java new file mode 100644 index 000000000..fafca7030 --- /dev/null +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/SplitFileUtils.java @@ -0,0 +1,35 @@ +package com.jero.modules.oss; + +import javax.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; + +import java.net.URLEncoder; +@Slf4j +public class SplitFileUtils { + + /** + * + * @Title: encodeFileName + * @Description: 导出文件转换文件名称编码 + * @param @param fileNames + * @param @param request + * @param @return 设定文件 + * @return String 返回类型 + * @throws + */ + public static String encodeFileName(String fileNames , HttpServletRequest request) { + try { + String agent = request.getHeader("User-Agent"); + if (agent.contains("Firefox")) { + fileNames = new String(fileNames.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器 + } else { + fileNames = URLEncoder.encode(fileNames, "utf-8"); + //谷歌中空格变为+问题 + fileNames = fileNames.replaceAll("\\+","%20"); + } + } catch (Exception e) { + log.error(e.getMessage(),e); + } + return fileNames ; + } +} diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java new file mode 100644 index 000000000..239b81381 --- /dev/null +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/controller/SplitFileController.java @@ -0,0 +1,67 @@ +package com.jero.modules.oss.controller; + +import com.jero.common.exception.JeroBootException; +import com.jero.modules.oss.SplitFileUtils; +import com.jero.modules.oss.service.IOSSFileService; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +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.ResponseBody; +import sun.misc.BASE64Decoder; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Base64; + +@Slf4j +@Controller +@RequestMapping("/sys/split/file") +public class SplitFileController { + + @Autowired + private IOSSFileService ossFileService; + + @ResponseBody + @GetMapping("/getImage") + public void queryPageList(@RequestParam("fileUrl") String fileUrl, HttpServletResponse response, HttpServletRequest request) { + + //base64解码 + BASE64Decoder base64Decoder = new BASE64Decoder(); + try { + byte[] bytes = base64Decoder.decodeBuffer(fileUrl); + fileUrl = new String(bytes, "utf-8"); + } catch (IOException e) { + throw new JeroBootException("文件不存在"); + } + int splitIndex = fileUrl.lastIndexOf(File.separator); + String fileName = fileUrl.substring(splitIndex); + response.setHeader("Content-Disposition", + "attachment; filename=\""+fileName+"\""); + response.setHeader("Content-Disposition", + "attachment; filename=\""+ SplitFileUtils.encodeFileName(fileName, request) +"\""); + response.setContentType("application/force-download"); + try(OutputStream os = response.getOutputStream(); + FileInputStream fis = new FileInputStream(fileUrl);) { + + int len = 0; + while ((len = fis.read()) != -1) { + os.write(len); + } + os.flush(); + } catch (FileNotFoundException e) { + throw new JeroBootException("文件不存在"); + } catch (IOException e) { + throw new JeroBootException("文件不存在"); + } + } + + +} diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java index 4248b510c..2b096eb18 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/service/impl/FileSpiltService.java @@ -33,8 +33,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; +import sun.misc.BASE64Encoder; import java.io.*; +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1220,19 +1223,12 @@ public class FileSpiltService { // 处理段落生成编号不识别问题 String paragraphString = p.getText(); - if (StringUtils.isNotBlank(paragraphString)) { + if (!StringUtils.isEmpty(paragraphString)) { paragraphString = paragraphString.replace((char) 12288, ' '); + paragraphString = paragraphString.replaceAll("\r\n",""); + paragraphString = paragraphString.replaceAll("\n",""); paragraphString = paragraphString.trim(); } - if(StringUtils.isEmpty(paragraphString)){ - continue; - } - paragraphString = paragraphString.replaceAll("\r\n",""); - paragraphString = paragraphString.replaceAll("\n",""); - paragraphString = paragraphString.trim(); - if(StringUtils.isEmpty(paragraphString)){ - continue; - } // if (StringUtils.isNotEmpty(p.getNumLevelText())&& p.getNumLevelText().indexOf(".")>0) { // String numberParent = p.getNumLevelText().substring(0,p.getNumLevelText().lastIndexOf(".")); // if (numberMap.containsKey(numberParent)){ @@ -2570,13 +2566,16 @@ public class FileSpiltService { try { FileOutputStream fos = new FileOutputStream(filepathandname); fos.write(bytev); - String path = splitUrl + "/" + imageName; +// String fileUrl = splitUrl + "/" + filepathandname; + BASE64Encoder base64Encoder = new BASE64Encoder(); + String encode = base64Encoder.encode(filepathandname.getBytes(StandardCharsets.UTF_8)); + String path = splitUrl+"/jero-boot/sys/split/file/getImage?fileUrl="+encode; String imgCon = path; String imgConVal = ""; message.getItemsCondi().add(imgConVal); message.setItermsConditions(message.getItermsConditions() + imgConVal); itemsValList.add(getSplitItemsValObject(message.getId(), SplitFilePragraTypeEnum.IMG.getValue(), imgCon, message.getItemsCondi().size(), null)); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml index 80377e4c8..3af9adef7 100644 --- a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml +++ b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml @@ -191,7 +191,8 @@ jero : # 本地:local\Minio:minio\阿里云:alioss uploadType: local backUrl: http://localhost:3000 - splitUrl: http://39.98.140.126:8888 + #拆分图片展示地址 + splitUrl: http://localhost:8080 path : #文件上传根目录 设置 upload: D://opt//upFiles