增加通过objectKey下载OBS文件
This commit is contained in:
parent
6cdaf6c1ef
commit
fcd668e590
|
@ -37,7 +37,6 @@ public class AuthInterceptor {
|
|||
String mustRole = authCheck.mustRole();
|
||||
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
||||
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
||||
|
||||
//当前登录用户
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
//必须有该权限才通过
|
||||
|
@ -73,4 +72,5 @@ public class AuthInterceptor {
|
|||
//通过权限校验,放行
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,12 +16,10 @@ import io.swagger.v3.oas.annotations.Operation;
|
|||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -46,14 +44,15 @@ public class FileController {
|
|||
|
||||
/**
|
||||
* 未登录的情况下的文件上传
|
||||
*
|
||||
* @param multipartFile 文件上传参数
|
||||
* @param uploadFileRequest 文件业务类型请求体
|
||||
* @return 图片可访问地址
|
||||
*/
|
||||
@PostMapping("/upload/server/not_login")
|
||||
@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();
|
||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||
if (fileUploadBizEnum == null) {
|
||||
|
@ -61,7 +60,7 @@ public class FileController {
|
|||
}
|
||||
// 校验文件
|
||||
validFile(multipartFile, fileUploadBizEnum);
|
||||
//文件目录:根据业务、用户来划分
|
||||
// 文件目录:根据业务、用户来划分
|
||||
String uuid = RandomStringUtils.randomAlphabetic(8);
|
||||
String filename = uuid + "-" + multipartFile.getOriginalFilename();
|
||||
String filepath = String.format("/%s/%s/%s", fileUploadBizEnum.getValue(), 0, filename);
|
||||
|
@ -75,7 +74,7 @@ public class FileController {
|
|||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||
}
|
||||
}
|
||||
//返回可访问地址
|
||||
// 返回可访问地址
|
||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||
return ResultUtils.success(url);
|
||||
}
|
||||
|
@ -83,8 +82,8 @@ public class FileController {
|
|||
|
||||
@PostMapping("/upload/server")
|
||||
@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();
|
||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||
if (fileUploadBizEnum == null) {
|
||||
|
@ -111,16 +110,15 @@ public class FileController {
|
|||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||
}
|
||||
}
|
||||
//返回可访问地址
|
||||
// 返回可访问地址
|
||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||
return ResultUtils.success(url);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/upload/single")
|
||||
@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);
|
||||
|
@ -138,7 +136,7 @@ public class FileController {
|
|||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
|
||||
}
|
||||
}
|
||||
//返回可访问地址
|
||||
// 返回可访问地址
|
||||
String url = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
|
||||
|
||||
Map<String, String> data = new HashMap<>();
|
||||
|
@ -149,14 +147,9 @@ public class FileController {
|
|||
response.put("errno", 0);
|
||||
response.put("data", data);
|
||||
Gson gson = new Gson();
|
||||
|
||||
return gson.fromJson(gson.toJson(response), Object.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 校验文件
|
||||
*
|
||||
|
@ -164,9 +157,9 @@ public class FileController {
|
|||
* @param fileUploadBizEnum 业务类型
|
||||
*/
|
||||
private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
|
||||
//文件大小
|
||||
// 文件大小
|
||||
long fileSize = multipartFile.getSize();
|
||||
//文件后缀
|
||||
// 文件后缀
|
||||
String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
|
||||
final long LIMIT = 30 * 1024 * 1024L;
|
||||
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() {
|
||||
return code;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.cultural.heritage.service.file;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
|
@ -30,7 +31,7 @@ public interface IHweiYunOBSService {
|
|||
*/
|
||||
String fileUpload(MultipartFile uploadFile, String objectKey);
|
||||
|
||||
// String uploadFileByte(byte data[], String objectKey);
|
||||
// String uploadFileByte(byte data[], String objectKey);
|
||||
// String uploadFile(File file);
|
||||
//
|
||||
// /**
|
||||
|
@ -39,4 +40,6 @@ public interface IHweiYunOBSService {
|
|||
// * @return: java.io.InputStream
|
||||
// */
|
||||
// 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.exception.ObsException;
|
||||
import com.obs.services.model.*;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -12,6 +14,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.io.IOException;
|
||||
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);
|
||||
// 读取该已上传对象的URL
|
||||
return result.getObjectUrl();
|
||||
} catch (ObsException e) {
|
||||
} catch (IOException e) {
|
||||
} catch (ObsException | IOException e) {
|
||||
} finally {
|
||||
hweiOBSConfig.destroy(obsClient);
|
||||
}
|
||||
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
|
||||
// public String uploadFileByte(byte[] data, String fileName) {
|
||||
// try {
|
||||
|
|
Loading…
Reference in New Issue
Block a user