一般情况下,文件对象的存取都是通过搭建一个 MinIO 搞定
但是,当下层应用只提供了一个文件字节流下载方法,而不提供外链的时候,就只能自己在应用层做一个转发了,也就是在应用层生成一个外链
头疼的是,应用层的部署环境一直是变化的。(本地环境、测试环境、线上环境、生产环境1、生产环境2 … 生产环境n)
解决方式:
1、动态配置。把部署环境的 ip、端口信息配置在 nacos 中,生成外链的时候读取 nacos 配置
2、 从请求中获取 ip、端口信息。缺点是,只能在有请求进入的时候才能生效
项目的变数太大了,我也不知道到时候的部署环境是啥?这里选第二种
外链构建工具类
import com.czh.common.constant.IotConst;
import com.czh.common.constant.StringConst;
import com.czh.controller.CommonController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 根据请求构建文件对象的访问外链
*
* @author
* @date 3/18/2024
*/
public class MinioUtils {
private static final String PATH = "path";
private static final String SHA256 = "sha256";
private static final String BUCK_NAME = "bucketName";
/**
* <p>构建文件 url</p>
* 只能在<b>能获取到请求对象</b>的地方调用;
* 也就是从外部发起请求的时候才能调用,否则(例如在定时任务里使用)返回的路径为 null
*
* @param map
* @return
*/
public static String buildFileLink(Map<String,Object> map) {
// 通过RequestContextHolder获取ServletRequestAttributes对象
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null == requestAttributes || null == requestAttributes.getRequest()) {
return null;
}
// 构架请求参数
String requestParam = handleRequestParam(map);
if (StringUtils.isBlank(requestParam)) {
return null;
}
// 当前应用的访问路径 eg: http://localhost:16701/bs-web
String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
// 完整文件 url
String url = contextPath + CommonController.FILE_MAPPING_PATH + requestParam;
return url;
}
public static String buildFileLink(String bucketName, String path, String sha256) {
Map<String, Object> map = new HashMap<>();
map.put(BUCK_NAME,bucketName);
map.put(PATH,path);
map.put(SHA256,sha256);
return buildFileLink(map);
}
/**
* 构建 url 请求参数
*
* @param map
* @return
*/
private static String handleRequestParam(Map<String, Object> map) {
if (CollectionUtils.isEmpty(map)) {
return "";
}
String path = (String) map.get(PATH);
if (null == path) {
path = "";
}
String sha256 = (String) map.get(SHA256);
if (null == sha256) {
sha256 = "";
}
String bucketName = (String) map.get(BUCK_NAME);
if (null == bucketName) {
bucketName = "";
}
// 文件参数为空,说明没有文件
if (StringUtils.isBlank(path) && StringUtils.isBlank(sha256) && StringUtils.isBlank(bucketName)) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(StringConst.QUESTION_MARK).append(PATH).append(StringConst.EQUALS_SIGN).append(path)
.append(StringConst.AND).append(SHA256).append(StringConst.EQUALS_SIGN).append(sha256)
.append(StringConst.AND).append(BUCK_NAME).append(StringConst.EQUALS_SIGN).append(bucketName);
return stringBuilder.toString();
}
}
文件对象下载接口
import com.czh.common.domain.Result;
import com.czh.common.utils.IotApiUtils;
import com.czh.common.utils.MinioUtils;
import com.czh.nacos.service.DahuaOthersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* @author
* @date 3/15/2024
*/
@RestController
@RequestMapping("/common")
public class CommonController {
/**
* file() 接口的映射路径。
* !!注意!!
* 如果发生改变需要同时修改这个路径
*/
public static final String FILE_MAPPING_PATH = "/common/file";
@Autowired
DahuaOthersService dahuaOthersService;
/**
* 获取 minio 文件流
*
* @param bucketName
* @param sha256
* @param path
* @param response
*/
@GetMapping("/file")
public void file(@RequestParam(value = "bucketName", required = false) String bucketName,
@RequestParam(value = "sha256", required = false) String sha256,
@RequestParam(value = "path", required = false) String path,
HttpServletResponse response) {
dahuaOthersService.getFileByStream(bucketName, sha256, path, response);
}
/**
* 获取某个 minio 的文件流的 url;多次访问生成的 url 是相同的
* @param bucketName
* @param sha256
* @param path
* @return
*/
@GetMapping("/fileUrl")
public Result fileUrl(@RequestParam(value = "bucketName", required = false) String bucketName,
@RequestParam(value = "sha256", required = false) String sha256,
@RequestParam(value = "path", required = false) String path) {
String url = MinioUtils.buildFileLink(bucketName,path,sha256);
return Result.success(url);
}
}