From 3b13336161b5f0d1d63e4845d197a10521d08584 Mon Sep 17 00:00:00 2001
From: feige <791364011@qq.com>
Date: 星期二, 06 五月 2025 17:07:39 +0800
Subject: [PATCH] 增加了二维码接口
---
zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java | 52 ++++++++++++++++++----------------------------------
1 files changed, 18 insertions(+), 34 deletions(-)
diff --git a/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java b/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java
index 866a6b5..e041bbd 100644
--- a/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java
+++ b/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java
@@ -1,8 +1,13 @@
package com.ruoyi.service.impl;
+import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.service.DownLoadFileService;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
@@ -14,47 +19,26 @@
* @Date 2023-03-19 17:33
*/
@Service
+@Slf4j
public class DownLoadFileServiceImpl implements DownLoadFileService {
-
- @Value("${ruoyi.profile}")
- private String basePath;
@Override
public void downLoadFile(String path, HttpServletResponse response) {
- path=basePath+path.substring(8);
- File file = new File(path);
- byte[] buffer = new byte[1024];
- BufferedInputStream bis = null;
- OutputStream os = null;
+ path = path.substring(8);
try {
- //鏂囦欢鏄惁瀛樺湪
- if (file.exists()) {
- //璁剧疆鍝嶅簲
- response.setContentType("application/octet-stream;charset=UTF-8");
- response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
- response.setHeader("Content-Disposition","attachment;filename=");
- response.setCharacterEncoding("UTF-8");
- os = response.getOutputStream();
- bis = new BufferedInputStream(new FileInputStream(file));
- while(bis.read(buffer) != -1){
- os.write(buffer);
- }
+ if (!FileUtils.checkAllowDownload(path)) {
+ throw new Exception(StringUtils.format("鏂囦欢鍚嶇О({})闈炴硶锛屼笉鍏佽涓嬭浇銆� ", path));
}
- }catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if(bis != null) {
- bis.close();
- }
- if(os != null) {
- os.flush();
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
+ String realFileName = System.currentTimeMillis() + path.substring(path.indexOf("_") + 1);
+ String filePath = RuoYiConfig.getProfile() + path;
+
+ response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
+ FileUtils.setAttachmentResponseHeader(response, realFileName);
+ FileUtils.writeBytes(filePath, response.getOutputStream());
+
+ } catch (Exception e) {
+ log.error("涓嬭浇鏂囦欢澶辫触", e);
}
}
}
--
Gitblit v1.9.1