From 50069a27b05fd397362f8a4c75b051f0823c4b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A5=8E=E5=85=B4?= <2220574228@qq.com> Date: Fri, 26 Dec 2025 12:28:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8E=E4=B8=BA=E4=BA=91obs;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interfaces/facade/menu/MenuApi.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/mhd_product/src/main/java/com/linke/product/interfaces/facade/menu/MenuApi.java b/mhd_product/src/main/java/com/linke/product/interfaces/facade/menu/MenuApi.java index ec7ee1470..0997fb664 100644 --- a/mhd_product/src/main/java/com/linke/product/interfaces/facade/menu/MenuApi.java +++ b/mhd_product/src/main/java/com/linke/product/interfaces/facade/menu/MenuApi.java @@ -1,5 +1,6 @@ package com.linke.product.interfaces.facade.menu; +import cn.hutool.core.io.resource.InputStreamResource; import cn.hutool.core.util.StrUtil; import com.linke.product.application.service.menu.MenuApplicationService; import com.linke.product.domain.menu.entity.Menu; @@ -21,10 +22,16 @@ import com.mhd.common.log.enums.BusinessType; import com.mhd.common.security.utils.SecurityUtils; import com.mhd.system.api.model.LoginUser; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -253,6 +260,45 @@ public class MenuApi extends BaseController { return AjaxResult.success(fileUrls); } + @GetMapping("/download") + public ResponseEntity downloadFromOBS( + @RequestParam String obsUrl, + @RequestParam String filename) { + + try { + // 1. 连接到OBS临时URL + URL url = new URL(obsUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + // 2. 获取输入流 + InputStream inputStream = connection.getInputStream(); + + // 3. 设置响应头 + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + filename + "\""); + headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate"); + headers.add(HttpHeaders.PRAGMA, "no-cache"); + headers.add(HttpHeaders.EXPIRES, "0"); + + // 4. 根据文件类型设置Content-Type + String contentType = connection.getContentType(); + MediaType mediaType = contentType != null ? + MediaType.valueOf(contentType) : + MediaType.APPLICATION_OCTET_STREAM; + + // 5. 返回流式响应 + return ResponseEntity.ok() + .headers(headers) + .contentType(mediaType) + .body(new InputStreamResource(inputStream)); + + } catch (Exception e) { + throw new RuntimeException("下载文件失败", e); + } + } + /** * @description 临时使用产品的oss 公有读