增加通过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,6 +44,7 @@ public class FileController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 未登录的情况下的文件上传
|
* 未登录的情况下的文件上传
|
||||||
|
*
|
||||||
* @param multipartFile 文件上传参数
|
* @param multipartFile 文件上传参数
|
||||||
* @param uploadFileRequest 文件业务类型请求体
|
* @param uploadFileRequest 文件业务类型请求体
|
||||||
* @return 图片可访问地址
|
* @return 图片可访问地址
|
||||||
|
@ -117,7 +116,6 @@ public class FileController {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@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) {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验文件
|
* 校验文件
|
||||||
*
|
*
|
||||||
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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