华为云obs;
This commit is contained in:
@@ -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<InputStreamResource> 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 公有读
|
||||
|
||||
Reference in New Issue
Block a user