更新配置4
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
package com.mokee.admin.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* Web MVC 配置 — 注册用户上下文拦截器
|
||||
* Web MVC 配置
|
||||
* <p>
|
||||
* - 注册用户上下文拦截器(文件预览/下载端点不拦截)
|
||||
* - 注册 RestTemplate Bean(用于代理请求文件服务器)
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@@ -20,4 +26,13 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
.addPathPatterns("/zgapi/v1/admin/**") // 仅管理后台接口
|
||||
.excludePathPatterns("/zgapi/v1/admin/file/**/view", "/zgapi/v1/admin/file/**/download"); // 文件预览/下载不拦截
|
||||
}
|
||||
|
||||
/** 文件服务器代理专用 RestTemplate — 较短超时,避免阻塞页面渲染 */
|
||||
@Bean
|
||||
public RestTemplate fileServiceRestTemplate() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
factory.setConnectTimeout(5000);
|
||||
factory.setReadTimeout(30000);
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,4 +53,15 @@ public class FileController {
|
||||
public Result<SysFile> getById(@PathVariable String id) {
|
||||
return Result.ok(fileService.getMetaById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件预览 — 代理到文件服务器获取图片并返回
|
||||
* <p>
|
||||
* 用于 <img> 标签直接加载。每次请求从文件服务器获取新 Token 后下载文件,
|
||||
* 对前端透明。不走用户上下文拦截器(WebMvcConfig 中已排除)。
|
||||
*/
|
||||
@GetMapping("/{id}/view")
|
||||
public void view(@PathVariable String id, jakarta.servlet.http.HttpServletResponse response) {
|
||||
fileService.view(id, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,4 +25,15 @@ public interface FileService {
|
||||
|
||||
/** 查询元信息(不含 file_data) */
|
||||
SysFile getMetaById(String id);
|
||||
|
||||
/**
|
||||
* 代理文件预览 — 从文件服务器获取 Token、下载文件、流式返回给客户端
|
||||
* <p>
|
||||
* 用于 <img> 标签加载场景。每次请求都从文件服务器获取新 Token,
|
||||
* 避免 Token 缓存带来的消费时序问题。
|
||||
*
|
||||
* @param id 文件 ID
|
||||
* @param response HTTP 响应对象(用于设置 Content-Type 和输出流)
|
||||
*/
|
||||
void view(String id, jakarta.servlet.http.HttpServletResponse response);
|
||||
}
|
||||
|
||||
@@ -7,17 +7,26 @@ import com.mokee.admin.config.UserContextHolder;
|
||||
import com.mokee.admin.mapper.SysFileMapper;
|
||||
import com.mokee.admin.service.FileService;
|
||||
import com.mokee.common.entity.SysFile;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件服务实现 — 仅提供元信息查询
|
||||
* 文件服务实现
|
||||
* <p>
|
||||
* 文件上传/下载/删除已迁移至外部文件对象存储服务(file.wg.zgitm.com),
|
||||
* 外部服务直接写入 sys_file 表,本服务只做查询。
|
||||
* 外部服务直接写入 sys_file 表,本服务做元信息查询 + 预览代理。
|
||||
*
|
||||
* @author mokee
|
||||
*/
|
||||
@@ -27,6 +36,11 @@ import java.util.List;
|
||||
public class FileServiceImpl implements FileService {
|
||||
|
||||
private final SysFileMapper sysFileMapper;
|
||||
private final RestTemplate fileServiceRestTemplate;
|
||||
|
||||
/** 文件服务器地址 */
|
||||
@Value("${file.service.url:https://file.wg.zgitm.com}")
|
||||
private String fileServiceUrl;
|
||||
|
||||
@Override
|
||||
public IPage<SysFile> page(Integer page, Integer size, String fileType,
|
||||
@@ -72,4 +86,82 @@ public class FileServiceImpl implements FileService {
|
||||
w.select(SysFile.class, f -> !"file_data".equals(f.getColumn()));
|
||||
return sysFileMapper.selectOne(w);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void view(String id, HttpServletResponse response) {
|
||||
// 1. 查文件元信息
|
||||
SysFile file = getMetaById(id);
|
||||
if (file == null) {
|
||||
respondError(response, 404, "文件不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 从文件服务器获取下载 Token
|
||||
String token = fetchDownloadToken();
|
||||
if (token == null) {
|
||||
log.warn("获取文件服务器 Token 失败 — fileId={}", id);
|
||||
respondError(response, 502, "获取下载凭证失败");
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 从文件服务器下载文件二进制数据
|
||||
try {
|
||||
String downloadUrl = fileServiceUrl + "/api/download/" + id + "?token=" + token;
|
||||
ResponseEntity<byte[]> resp = fileServiceRestTemplate.exchange(
|
||||
downloadUrl, HttpMethod.GET, null, byte[].class);
|
||||
|
||||
byte[] body = resp.getBody();
|
||||
if (body == null || (resp.getStatusCode().isError())) {
|
||||
log.warn("文件服务器返回空或错误 — fileId={}, status={}", id, resp.getStatusCode());
|
||||
respondError(response, 502, "文件获取失败");
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 流式返回给客户端
|
||||
response.setContentType(file.getMimeType() != null ? file.getMimeType() : "application/octet-stream");
|
||||
response.setContentLength(body.length);
|
||||
response.getOutputStream().write(body);
|
||||
response.getOutputStream().flush();
|
||||
} catch (IOException e) {
|
||||
log.warn("文件预览流式输出失败 — fileId={}", id, e);
|
||||
respondError(response, 500, "文件输出失败");
|
||||
} catch (Exception e) {
|
||||
log.warn("文件服务器请求失败 — fileId={}", id, e);
|
||||
respondError(response, 502, "文件服务器连接失败");
|
||||
}
|
||||
}
|
||||
|
||||
/** 向文件服务器获取下载 Token */
|
||||
@SuppressWarnings("unchecked")
|
||||
private String fetchDownloadToken() {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Content-Type", "application/json");
|
||||
HttpEntity<Map<String, String>> request = new HttpEntity<>(
|
||||
Map.of("systemCode", "gateway"), headers);
|
||||
ResponseEntity<Map> resp = fileServiceRestTemplate.postForEntity(
|
||||
fileServiceUrl + "/api/token", request, Map.class);
|
||||
if (resp.getBody() != null && resp.getBody().get("data") instanceof Map) {
|
||||
Map<String, Object> data = (Map<String, Object>) resp.getBody().get("data");
|
||||
Object token = data.get("token");
|
||||
return token != null ? token.toString() : null;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.warn("获取文件服务 Token 异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 向客户端返回错误 */
|
||||
private void respondError(HttpServletResponse response, int status, String msg) {
|
||||
try {
|
||||
response.setStatus(status);
|
||||
response.setContentType("text/plain;charset=UTF-8");
|
||||
response.getWriter().write(msg);
|
||||
response.getWriter().flush();
|
||||
} catch (IOException ignored) {
|
||||
// 连接可能已断开,忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user