diff --git a/pom.xml b/pom.xml
index fd7cb79..61b3980 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@
GuoYan
GuoYan
- 11
+ 1.8
@@ -67,6 +67,25 @@
commons-io
2.8.0
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
+
+ com.squareup.okhttp3
+ okhttp
+ 4.9.3
+
+
+ org.apache.poi
+ poi-scratchpad
+ 3.17
+
+
+ org.apache.poi
+ poi-ooxml
+ 3.17
+
diff --git a/src/main/java/com/example/guoyan/controller/FilePreviewController.java b/src/main/java/com/example/guoyan/controller/FilePreviewController.java
new file mode 100644
index 0000000..164e6b1
--- /dev/null
+++ b/src/main/java/com/example/guoyan/controller/FilePreviewController.java
@@ -0,0 +1,232 @@
+package com.example.guoyan.controller;
+
+import com.example.guoyan.mapper.AttachMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.HashMap;
+
+@RestController
+@RequestMapping("/fileview")
+@Slf4j
+public class FilePreviewController {
+
+ @Autowired
+ private AttachMapper attachMapper;
+ @Autowired
+ private RestTemplate restTemplate;
+// @GetMapping("/download")
+// public void fileDownload(String id, String dataid, HttpServletResponse response) throws IOException {
+//
+// InputStream fileInputStream = null;
+// BufferedInputStream inputStream = null;
+// ServletOutputStream outputStream = null;
+// log.info("id: " + id);
+// log.info("dataid: " + dataid);
+// try {
+// // 获取文件信息
+// HashMap fileInfo = attachMapper.selectByid(id);
+// String filepath = "/opt/eresource/attachFiles/" + fileInfo.get("filepath");
+// String originalname = fileInfo.get("originalname");
+// File file = new File(filepath);
+//
+// // 设置响应头
+// response.addHeader("content-disposition", "attachment;filename=" +
+// URLEncoder.encode(originalname, "UTF-8") + ";filename*=UTF-8''" +
+// URLEncoder.encode(originalname, "UTF-8"));
+// response.setContentType("application/octet-stream");
+// response.addHeader("Content-Length", "" + file.length());
+//
+// // 读取文件并写入响应流
+// fileInputStream = new FileInputStream(filepath);
+// inputStream = new BufferedInputStream(fileInputStream);
+// outputStream = response.getOutputStream();
+//
+// byte[] bytes = new byte[1024];
+// int len = inputStream.read(bytes);
+// while (len != -1) {
+// outputStream.write(bytes, 0, len); // 修改这里,使用len而不是bytes.length
+// outputStream.flush();
+// len = inputStream.read(bytes);
+// }
+//
+// } catch (IOException e) {
+// throw new RuntimeException("文件下载失败", e);
+// } finally {
+// log.info("文件下载完成");
+// // 关闭流
+// if (inputStream != null) {
+// try {
+// inputStream.close();
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
+// }
+// if (fileInputStream != null) {
+// try {
+// fileInputStream.close();
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
+// }
+// if (outputStream != null) {
+// try {
+// outputStream.close();
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
+// }
+// }
+// }
+@GetMapping("/download")
+public void fileDownload(String id, String dataid, HttpServletResponse response) throws IOException {
+ try {
+ // 获取文件信息
+ HashMap fileInfo = attachMapper.selectByid(id);
+ String filepath = "/opt/eresource/attachFiles/" + fileInfo.get("filepath");
+ File file = new File(filepath);
+
+ // 设置响应头
+ response.setHeader("content-disposition", "attachment;filename=" +
+ URLEncoder.encode(fileInfo.get("originalname"), "UTF-8") +
+ ";filename*=UTF-8''" + URLEncoder.encode(fileInfo.get("originalname"), "UTF-8"));
+ response.setContentType("application/octet-stream");
+ response.setContentLength((int) file.length());
+
+ // 使用NIO通道
+ try (FileChannel fileChannel = new FileInputStream(file).getChannel();
+ WritableByteChannel outputChannel = Channels.newChannel(response.getOutputStream())) {
+
+ // 使用transferTo方法直接传输数据
+ fileChannel.transferTo(0, file.length(), outputChannel);
+ }
+
+ } catch (Exception e) {
+ log.error("文件下载失败", e);
+ throw new RuntimeException("文件下载失败", e);
+ }
+}
+ @GetMapping("/retrieveImage")
+ public void retrieveImage(String action, String id, HttpServletResponse response) throws IOException {
+ try {
+ // 验证参数
+ if (!"retrieve".equals(action) || StringUtils.isEmpty(id)) {
+ response.sendError(HttpServletResponse.SC_BAD_REQUEST, "参数错误");
+ return;
+ }
+
+ // 获取文件信息
+ HashMap fileInfo = attachMapper.selectByid(id);
+ if (fileInfo == null) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片不存在");
+ return;
+ }
+
+ String filepath = "/opt/eresource/attachFiles/" + fileInfo.get("filepath");
+ File file = new File(filepath);
+
+ if (!file.exists()) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在");
+ return;
+ }
+
+ // 设置响应头
+ String contentType = getContentType(fileInfo.get("originalname"));
+ response.setContentType(contentType);
+ response.setContentLength((int) file.length());
+
+ // 使用NIO通道传输数据
+ try (FileChannel fileChannel = new FileInputStream(file).getChannel();
+ WritableByteChannel outputChannel = Channels.newChannel(response.getOutputStream())) {
+ fileChannel.transferTo(0, file.length(), outputChannel);
+ }
+
+ } catch (Exception e) {
+ log.error("图片获取失败", e);
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "图片获取失败");
+ }
+ }
+ @GetMapping("/preview")
+ public void previewFile(String id, String dataid, HttpServletRequest request, HttpServletResponse response,Boolean isdownload,String action) throws IOException {
+ if(isdownload!=null&&isdownload){
+ fileDownload(id, dataid, response);
+ return;
+ }
+ if(isdownload==null&&action!=null&&action.equals("retrieve")){
+ retrieveImage(action, id, response);
+ return;
+ }
+ try {
+ log.info("id: " + id);
+ // 获取文件信息
+ HashMap fileInfo = attachMapper.selectByid(id);
+ String originalname = fileInfo.get("originalname");
+ String filetype = fileInfo.get("filetype");
+ log.info("originalname: " + originalname);
+ // 配置信息(建议放在配置文件中)
+ String kkFileViewUrl = "http://116.62.210.190:8012/onlinePreview";
+ String currentServerUrl = "";
+ if(filetype!=null&&filetype.contains("image")){
+ currentServerUrl = "http://116.62.210.190:8002";
+
+ }else{
+ currentServerUrl = "http://127.0.0.1:8002";
+ }
+
+ // 构建原始文件下载URL
+ String fileUrl = currentServerUrl + "/fileview/download?id=" + id + "&dataid=" + dataid+"&fullfilename="+id+"_"+originalname;
+ log.info("fileUrl: " + fileUrl);
+ // Base64编码文件URL
+ String base64FileUrl = Base64.getEncoder().encodeToString(fileUrl.getBytes());
+ log.info("base64FileUrl: " + base64FileUrl);
+ // 构建完整的预览URL
+ String previewUrl = kkFileViewUrl + "?url=" + base64FileUrl;
+ log.info("kkFileViewUrl: " + previewUrl);
+
+ response.sendRedirect(previewUrl);
+
+ } catch (Exception e) {
+ throw new RuntimeException("文件预览失败", e);
+ }
+ }
+ /**
+ * 获取文件的ContentType
+ */
+ private String getContentType(String filepath) {
+ String extension = filepath.substring(filepath.lastIndexOf(".")).toLowerCase();
+ switch (extension) {
+ case ".jpg":
+ case ".jpeg":
+ return "image/jpeg";
+ case ".png":
+ return "image/png";
+ case ".gif":
+ return "image/gif";
+ case ".webp":
+ return "image/webp";
+ default:
+ return "application/octet-stream";
+ }
+ }
+}
diff --git a/src/main/java/com/example/guoyan/controller/YunpingtaiController.java b/src/main/java/com/example/guoyan/controller/YunpingtaiController.java
index 445e874..3059819 100644
--- a/src/main/java/com/example/guoyan/controller/YunpingtaiController.java
+++ b/src/main/java/com/example/guoyan/controller/YunpingtaiController.java
@@ -9,6 +9,7 @@ import com.example.guoyan.entity.SendKpxx;
import com.example.guoyan.entity.SendXm;
import com.example.guoyan.mapper.*;
import com.example.guoyan.response.ResponseBeanForBeiJing;
+import com.example.guoyan.scheduled.MailService;
import com.example.guoyan.scheduled.ProcessScheduled;
import lombok.extern.slf4j.Slf4j;
@@ -62,6 +63,9 @@ public class YunpingtaiController {
private String loginUrl;
@Value("${ypt.deleteUrl}")
private String deleteUrl;
+ @Autowired
+ private MailService mailService;
+
@GetMapping()
public Result upLoadFile() {
String url = loginUrl;
@@ -259,4 +263,16 @@ public class YunpingtaiController {
String[] s = {"2c9a59b68d401eb1018e5f47201e5d84", "2c9a59b68d401eb1018e5f4720205d85"};
processScheduled.sendfpfielUpdown(headers, s);
}
+
+
+ /**
+ * 发送文本邮件
+ * @param to
+ * @param subject
+ * @param text
+ */
+ @GetMapping("/sendTextMail")
+ public void sendTextMail(String to,String subject,String text){
+ mailService.sendTextMailMessage(to,subject,text);
+ }
}
diff --git a/src/main/java/com/example/guoyan/mapper/AttachMapper.java b/src/main/java/com/example/guoyan/mapper/AttachMapper.java
new file mode 100644
index 0000000..446d367
--- /dev/null
+++ b/src/main/java/com/example/guoyan/mapper/AttachMapper.java
@@ -0,0 +1,13 @@
+package com.example.guoyan.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.HashMap;
+
+@Mapper
+public interface AttachMapper extends BaseMapper