|
@@ -1,6 +1,7 @@
|
|
|
package org.springblade.controller;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
@@ -19,6 +20,7 @@ import org.springblade.core.tool.api.R;
|
|
|
import org.springblade.core.tool.api.ResultCode;
|
|
|
import org.springblade.core.tool.utils.StringUtil;
|
|
|
import org.springblade.model.FileAttribute;
|
|
|
+import org.springblade.model.ReturnResponse;
|
|
|
import org.springblade.service.FileHandlerService;
|
|
|
import org.springblade.service.FilePreview;
|
|
|
import org.springblade.service.FilePreviewFactory;
|
|
@@ -26,6 +28,7 @@ import org.springblade.service.OfficeToPdfService;
|
|
|
import org.springblade.service.cache.CacheService;
|
|
|
import org.springblade.service.impl.OtherFilePreviewImpl;
|
|
|
import org.springblade.utils.AddWatermarkUtil;
|
|
|
+import org.springblade.utils.DownloadUtils;
|
|
|
import org.springblade.utils.KkFileUtils;
|
|
|
import org.springblade.utils.WebUtils;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@@ -36,6 +39,7 @@ import org.apache.http.impl.client.DefaultRedirectStrategy;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
@@ -53,6 +57,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.xml.crypto.Data;
|
|
|
import java.io.*;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -73,6 +78,8 @@ public class OnlinePreviewController {
|
|
|
private static final RestTemplate restTemplate = new RestTemplate();
|
|
|
private static final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+ @Value("${blade.file.uploadPath}")
|
|
|
+ private String picDir;
|
|
|
|
|
|
public OnlinePreviewController(OfficeToPdfService officeToPdfService,FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) {
|
|
|
this.previewFactory = filePreviewFactory;
|
|
@@ -119,6 +126,8 @@ public class OnlinePreviewController {
|
|
|
}
|
|
|
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req); //这里不在进行URL 处理了
|
|
|
model.addAttribute("file", fileAttribute);
|
|
|
+ model.addAttribute("url", url);
|
|
|
+ model.addAttribute("watermark", jsonObject.get("watermark"));
|
|
|
FilePreview filePreview = previewFactory.get(fileAttribute);
|
|
|
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
|
|
|
fileUrl = WebUtils.urlEncoderencode(fileUrl);
|
|
@@ -134,19 +143,51 @@ public class OnlinePreviewController {
|
|
|
}
|
|
|
@SneakyThrows
|
|
|
@GetMapping("/office2PDF2")
|
|
|
- public Map office2PDF2(String sourceFile, String destFile, HttpServletRequest req) {
|
|
|
+ public Map office2PDF2(String url, String watermark, HttpServletRequest req,HttpServletResponse resp) {
|
|
|
+ String destFile =picDir+ UUID.randomUUID().toString() + ".pdf";
|
|
|
|
|
|
- HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
|
|
|
+ try {
|
|
|
+
|
|
|
+// File destFile = new File(picDir + tempFileName);
|
|
|
+
|
|
|
+ String fileUrl = WebUtils.decodeUrl(url);
|
|
|
+ HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
|
|
|
+ FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
|
|
|
+
|
|
|
+ // 下载远程文件到本地,如果文件在本地已存在不会重复下载
|
|
|
+ ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileUrl);
|
|
|
+ if (response.isFailure()) {
|
|
|
+ // return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
+ }
|
|
|
+ String filePath = response.getContent();
|
|
|
+
|
|
|
+ officeToPdfService.openOfficeToPDF(filePath, destFile, fileAttribute);
|
|
|
+// officeToPdfService.openOfficeToPDF(fileUrl, destFile, fileAttribute);
|
|
|
|
|
|
- FileAttribute fileAttribute = fileHandlerService.getFileAttribute(sourceFile, req);
|
|
|
- officeToPdfService.openOfficeToPDF(sourceFile, destFile, fileAttribute);
|
|
|
+ File outputFile = new File(destFile);
|
|
|
+ InputStream docxInputStream = new FileInputStream(outputFile);
|
|
|
+ byte[] watermarks = AddWatermarkUtil.addWatermark(docxInputStream, watermark);
|
|
|
+ AddWatermarkUtil.writePdfToFile(watermarks, destFile);
|
|
|
|
|
|
- File outputFile = new File(destFile);
|
|
|
- InputStream docxInputStream = new FileInputStream(outputFile);
|
|
|
- byte[] watermarks = AddWatermarkUtil.addWatermark(docxInputStream, "watermark");
|
|
|
- AddWatermarkUtil.writePdfToFile(watermarks, destFile);
|
|
|
- objectObjectHashMap.put("destFile",destFile);
|
|
|
- return objectObjectHashMap;
|
|
|
+ objectObjectHashMap.put("destFile",destFile);
|
|
|
+
|
|
|
+// String filePath = picDir + file.getFilePath().replace("uploads/", "");
|
|
|
+ writeFile(resp, Files.newInputStream(new File(destFile).toPath()));
|
|
|
+ return objectObjectHashMap;
|
|
|
+ } finally {
|
|
|
+ File file = new File(destFile);
|
|
|
+ // 判断文件是否存在
|
|
|
+ if (file.exists()) {
|
|
|
+ // 删除文件
|
|
|
+ if (file.delete()) {
|
|
|
+ System.out.println("文件 " + destFile + " 已删除");
|
|
|
+ } else {
|
|
|
+ System.out.println("无法删除文件 " + destFile);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("文件 " + destFile + " 不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -295,4 +336,34 @@ public class OnlinePreviewController {
|
|
|
// log.error(ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将输入流输出到页面
|
|
|
+ *
|
|
|
+ * @param resp
|
|
|
+ * @param inputStream
|
|
|
+ * @date: 2020年11月17日
|
|
|
+ */
|
|
|
+ private void writeFile(HttpServletResponse resp, InputStream inputStream) throws Exception {
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ out = resp.getOutputStream();
|
|
|
+ int len = 0;
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ while ((len = inputStream.read(b)) != -1) {
|
|
|
+ out.write(b, 0, len);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|