增加通过objectKey下载OBS文件
This commit is contained in:
parent
6cdaf6c1ef
commit
fcd668e590
|
@ -37,7 +37,6 @@ public class AuthInterceptor {
|
||||||
String mustRole = authCheck.mustRole();
|
String mustRole = authCheck.mustRole();
|
||||||
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
||||||
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
||||||
|
|
||||||
//当前登录用户
|
//当前登录用户
|
||||||
User loginUser = userService.getLoginUser(request);
|
User loginUser = userService.getLoginUser(request);
|
||||||
//必须有该权限才通过
|
//必须有该权限才通过
|
||||||
|
@ -73,4 +72,5 @@ public class AuthInterceptor {
|
||||||
//通过权限校验,放行
|
//通过权限校验,放行
|
||||||
return joinPoint.proceed();
|
return joinPoint.proceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,10 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -46,14 +44,15 @@ public class FileController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 未登录的情况下的文件上传
|
* 未登录的情况下的文件上传
|
||||||
|
*
|
||||||
* @param multipartFile 文件上传参数
|
* @param multipartFile 文件上传参数
|
||||||
* @param uploadFileRequest 文件业务类型请求体
|
* @param uploadFileRequest 文件业务类型请求体
|
||||||
* @return 图片可访问地址
|
* @return 图片可访问地址
|
||||||
*/
|
*/
|
||||||
@PostMapping("/upload/server/not_login")
|
@PostMapping("/upload/server/not_login")
|
||||||
@Operation(summary = "(未使用)未登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFileNotLogin")
|
@Operation(summary = "(未使用)未登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFileNotLogin")
|
||||||
private BaseResponse<String> uploadServerFileNotLogin(@RequestPart("file")MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
|
private BaseResponse<String> uploadServerFileNotLogin(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
|
||||||
//获取业务名称
|
// 获取业务名称
|
||||||
String biz = uploadFileRequest.getBiz();
|
String biz = uploadFileRequest.getBiz();
|
||||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||||
if (fileUploadBizEnum == null) {
|
if (fileUploadBizEnum == null) {
|
||||||
|
@ -61,7 +60,7 @@ public class FileController {
|
||||||
}
|
}
|
||||||
// 校验文件
|
// 校验文件
|
||||||
validFile(multipartFile, fileUploadBizEnum);
|
validFile(multipartFile, fileUploadBizEnum);
|
||||||
//文件目录:根据业务、用户来划分
|
// 文件目录:根据业务、用户来划分
|
||||||
String uuid = RandomStringUtils.randomAlphabetic(8);
|
String uuid = RandomStringUtils.randomAlphabetic(8);
|
||||||
String filename = uuid + "-" + multipartFile.getOriginalFilename();
|
String filename = uuid + "-" + multipartFile.getOriginalFilename();
|
||||||
String filepath = String.format("/%s/%s/%s", fileUploadBizEnum.getValue(), 0, filename);
|
String filepath = String.format("/%s/%s/%s", fileUploadBizEnum.getValue(), 0, filename);
|
||||||
|
@ -75,7 +74,7 @@ public class FileController {
|
||||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//返回可访问地址
|
// 返回可访问地址
|
||||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||||
return ResultUtils.success(url);
|
return ResultUtils.success(url);
|
||||||
}
|
}
|
||||||
|
@ -83,8 +82,8 @@ public class FileController {
|
||||||
|
|
||||||
@PostMapping("/upload/server")
|
@PostMapping("/upload/server")
|
||||||
@Operation(summary = "已登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFile")
|
@Operation(summary = "已登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFile")
|
||||||
public BaseResponse<String> uploadServerFile(@RequestPart("file")MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) {
|
public BaseResponse<String> uploadServerFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) {
|
||||||
//获取业务名称
|
// 获取业务名称
|
||||||
String biz = uploadFileRequest.getBiz();
|
String biz = uploadFileRequest.getBiz();
|
||||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||||
if (fileUploadBizEnum == null) {
|
if (fileUploadBizEnum == null) {
|
||||||
|
@ -111,16 +110,15 @@ public class FileController {
|
||||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//返回可访问地址
|
// 返回可访问地址
|
||||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||||
return ResultUtils.success(url);
|
return ResultUtils.success(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/upload/single")
|
@PostMapping("/upload/single")
|
||||||
@Operation(summary = "单独文件上传", description = "参数:file,文件对象(multipartFile),权限:所有人,方法名:uploadServerFile")
|
@Operation(summary = "单独文件上传", description = "参数:file,文件对象(multipartFile),权限:所有人,方法名:uploadServerFile")
|
||||||
public Object uploadSingleServerFile(@RequestPart("file")MultipartFile multipartFile, HttpServletRequest request) {
|
public Object uploadSingleServerFile(@RequestPart("file") MultipartFile multipartFile, HttpServletRequest request) {
|
||||||
//
|
//
|
||||||
// 校验文件
|
// 校验文件
|
||||||
validFile(multipartFile, null);
|
validFile(multipartFile, null);
|
||||||
|
@ -138,7 +136,7 @@ public class FileController {
|
||||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//返回可访问地址
|
// 返回可访问地址
|
||||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||||
|
|
||||||
Map<String, String> data = new HashMap<>();
|
Map<String, String> data = new HashMap<>();
|
||||||
|
@ -149,14 +147,9 @@ public class FileController {
|
||||||
response.put("errno", 0);
|
response.put("errno", 0);
|
||||||
response.put("data", data);
|
response.put("data", data);
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
|
||||||
return gson.fromJson(gson.toJson(response), Object.class);
|
return gson.fromJson(gson.toJson(response), Object.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验文件
|
* 校验文件
|
||||||
*
|
*
|
||||||
|
@ -164,9 +157,9 @@ public class FileController {
|
||||||
* @param fileUploadBizEnum 业务类型
|
* @param fileUploadBizEnum 业务类型
|
||||||
*/
|
*/
|
||||||
private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
|
private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
|
||||||
//文件大小
|
// 文件大小
|
||||||
long fileSize = multipartFile.getSize();
|
long fileSize = multipartFile.getSize();
|
||||||
//文件后缀
|
// 文件后缀
|
||||||
String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
|
String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
|
||||||
final long LIMIT = 30 * 1024 * 1024L;
|
final long LIMIT = 30 * 1024 * 1024L;
|
||||||
if (fileSize > LIMIT) {
|
if (fileSize > LIMIT) {
|
||||||
|
@ -177,4 +170,9 @@ public class FileController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("downloadFile")
|
||||||
|
public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) {
|
||||||
|
iHweiYunOBSService.downloadFile(objectKey, response);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,4 +32,5 @@ public class BusinessException extends RuntimeException {
|
||||||
public int getCode() {
|
public int getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.cultural.heritage.service.file;
|
package com.cultural.heritage.service.file;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,7 +31,7 @@ public interface IHweiYunOBSService {
|
||||||
*/
|
*/
|
||||||
String fileUpload(MultipartFile uploadFile, String objectKey);
|
String fileUpload(MultipartFile uploadFile, String objectKey);
|
||||||
|
|
||||||
// String uploadFileByte(byte data[], String objectKey);
|
// String uploadFileByte(byte data[], String objectKey);
|
||||||
// String uploadFile(File file);
|
// String uploadFile(File file);
|
||||||
//
|
//
|
||||||
// /**
|
// /**
|
||||||
|
@ -39,4 +40,6 @@ public interface IHweiYunOBSService {
|
||||||
// * @return: java.io.InputStream
|
// * @return: java.io.InputStream
|
||||||
// */
|
// */
|
||||||
// InputStream fileDownload(String objectKey);
|
// InputStream fileDownload(String objectKey);
|
||||||
|
void downloadFile(String objectKey, HttpServletResponse response);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import com.cultural.heritage.service.file.IHweiYunOBSService;
|
||||||
import com.obs.services.ObsClient;
|
import com.obs.services.ObsClient;
|
||||||
import com.obs.services.exception.ObsException;
|
import com.obs.services.exception.ObsException;
|
||||||
import com.obs.services.model.*;
|
import com.obs.services.model.*;
|
||||||
|
import jakarta.servlet.ServletOutputStream;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -12,6 +14,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,14 +112,50 @@ public class HweiYunOBSServiceImpl implements IHweiYunOBSService {
|
||||||
PutObjectResult result = obsClient.putObject(request);
|
PutObjectResult result = obsClient.putObject(request);
|
||||||
// 读取该已上传对象的URL
|
// 读取该已上传对象的URL
|
||||||
return result.getObjectUrl();
|
return result.getObjectUrl();
|
||||||
} catch (ObsException e) {
|
} catch (ObsException | IOException e) {
|
||||||
} catch (IOException e) {
|
|
||||||
} finally {
|
} finally {
|
||||||
hweiOBSConfig.destroy(obsClient);
|
hweiOBSConfig.destroy(obsClient);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description : 文件下载
|
||||||
|
* @author : YBL
|
||||||
|
* @date : 2025/3/14 14:46
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void downloadFile(String objectKey, HttpServletResponse response) {
|
||||||
|
// 使用永久AK/SK初始化客户端
|
||||||
|
ObsClient obsClient = hweiOBSConfig.getInstance();
|
||||||
|
try {
|
||||||
|
// 清空response
|
||||||
|
response.reset();
|
||||||
|
// 设置response的Header
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(objectKey, StandardCharsets.UTF_8));
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
// 流式下载
|
||||||
|
ObsObject obsObject = obsClient.getObject(hweiOBSConfig.getBucketName(), objectKey);
|
||||||
|
// 读取对象内容
|
||||||
|
System.out.println("Object content:");
|
||||||
|
InputStream inputStream = obsObject.getObjectContent();
|
||||||
|
byte[] b = new byte[1024];
|
||||||
|
ServletOutputStream outputStream = response.getOutputStream();
|
||||||
|
int len;
|
||||||
|
while ((len = inputStream.read(b)) != -1) {
|
||||||
|
outputStream.write(b, 0, len);
|
||||||
|
}
|
||||||
|
System.out.println("getObjectContent successfully");
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("getObjectContent failed");
|
||||||
|
// 其他异常信息打印
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @Override
|
// @Override
|
||||||
// public String uploadFileByte(byte[] data, String fileName) {
|
// public String uploadFileByte(byte[] data, String fileName) {
|
||||||
// try {
|
// try {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user