【修改】CommonController修改上传与下载文件接口

This commit is contained in:
mzc5649
2021-03-29 17:28:27 +08:00
parent 62b19e3093
commit e8b34d5144
@@ -109,58 +109,43 @@ public class CommonController {
/**
* 预览图片&下载文件
* 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
*
* @param id 传入文件id
* @param request
* @param response
*/
@GetMapping(value = "/download/**")
public void view(HttpServletRequest request, HttpServletResponse response) {
// ISO-8859-1 ==> UTF-8 进行编码转换
String imgPath = extractPathFromPattern(request);
if(oConvertUtils.isEmpty(imgPath) || imgPath == "null"){
return;
@GetMapping(value = "/download/{id}")
public void view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
throw new RuntimeException("文件不存在..");
}
// 其余处理略
String fileUrl = ossFile.getUrl();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
imgPath = imgPath.replace("..", "");
if (imgPath.endsWith(",")) {
imgPath = imgPath.substring(0, imgPath.length() - 1);
}
String fileName = "";
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
//本地下载
String filePath = uploadpath + File.separator + imgPath;
String filePath = uploadpath + File.separator + fileName;
File file = new File(filePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件不存在..");
}
// 查询数据表数据是否存在
queryWrapper.eq(OSSFile::getUrl,imgPath);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
throw new RuntimeException("文件不存在..");
}
// 文件名称
fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath));
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
// minio 下载
queryWrapper.eq(OSSFile::getUrl,imgPath.replaceFirst("/","//"));
OSSFile ossFile = ossFileService.getOne(queryWrapper);
// 查询该文件数据是否存在
if( null == ossFile){
throw new RuntimeException("文件不存在");
}
// 通过MinioUtil查询时 只需要 桶后面的路径
// 通过MinioUtil查询时 只需要桶后面的路径
String minioUrl = MinioUtil.getMinioUrl();
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = ossFile.getUrl().replace(minioUrl, "");
String url = fileUrl.replace(minioUrl, "");
// 文件名称
fileName = ossFile.getFileName();
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);