Merge branch 'master' of https://git.lionquexpress.com/wuweihua/BMS
This commit is contained in:
@@ -11,6 +11,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.bms.common.config.BmsConfig;
|
||||
@@ -18,6 +19,7 @@ import com.bms.common.core.domain.AjaxResult;
|
||||
import com.bms.common.utils.StringUtils;
|
||||
import com.bms.common.utils.file.FileUploadUtils;
|
||||
import com.bms.common.utils.file.FileUtils;
|
||||
import com.bms.common.utils.file.MimeTypeUtils;
|
||||
import com.bms.common.utils.file.OssStorage;
|
||||
import com.bms.framework.config.ServerConfig;
|
||||
|
||||
@@ -98,6 +100,56 @@ public class CommonController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用上传请求(OSS 存储,返回 oss:// 存储标识)。
|
||||
* 客户附件、公司主体、收款凭证等业务附件走此接口;
|
||||
* 下载统一经 /common/download/resource?resource=oss://... 鉴权转发。
|
||||
*/
|
||||
@PostMapping("/upload/oss")
|
||||
public AjaxResult uploadFileOss(MultipartFile file,
|
||||
@RequestParam(value = "module", required = false) String module) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
if (file == null || file.isEmpty())
|
||||
{
|
||||
return AjaxResult.error("上传文件不能为空");
|
||||
}
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
// 扩展名白名单与大小校验(默认 50MB,图片/文档/压缩包/视频/pdf)
|
||||
FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
String extension = "";
|
||||
int dotIndex = originalFilename != null ? originalFilename.lastIndexOf('.') : -1;
|
||||
if (dotIndex >= 0)
|
||||
{
|
||||
extension = originalFilename.substring(dotIndex + 1).toLowerCase();
|
||||
}
|
||||
// module 仅允许字母数字下划线中划线,防止对象 key 注入
|
||||
String moduleKey = StringUtils.defaultIfBlank(module, "misc").replaceAll("[^a-zA-Z0-9_-]", "");
|
||||
if (moduleKey.isEmpty())
|
||||
{
|
||||
moduleKey = "misc";
|
||||
}
|
||||
String datePath = new java.text.SimpleDateFormat("yyyy/MM/dd").format(new java.util.Date());
|
||||
String objectKey = "attachment/" + moduleKey + "/" + datePath + "/"
|
||||
+ java.util.UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
||||
String storageKey = ossStorage.upload(file.getInputStream(), file.getSize(), objectKey, file.getContentType());
|
||||
String downloadUrl = "/common/download/resource?resource="
|
||||
+ java.net.URLEncoder.encode(storageKey, java.nio.charset.StandardCharsets.UTF_8);
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("url", downloadUrl);
|
||||
ajax.put("fileName", storageKey);
|
||||
ajax.put("newFileName", originalFilename);
|
||||
ajax.put("originalFilename", originalFilename);
|
||||
return ajax;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("upload file to oss failed", e);
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用上传请求(多个)
|
||||
*/
|
||||
|
||||
@@ -29,6 +29,13 @@ public class OssStorage {
|
||||
|
||||
/** 使用文件流上传,避免把整个账单读入内存。返回可持久化的存储标识。 */
|
||||
public String upload(Path file, String objectKey, String contentType) throws IOException {
|
||||
try (InputStream input = Files.newInputStream(file)) {
|
||||
return upload(input, Files.size(file), objectKey, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
/** 使用输入流上传(MultipartFile 直传,不落临时文件)。返回可持久化的存储标识。 */
|
||||
public String upload(InputStream input, long size, String objectKey, String contentType) throws IOException {
|
||||
validateObjectKey(objectKey);
|
||||
String prefix = properties.getPathPrefix();
|
||||
validateObjectKey(prefix);
|
||||
@@ -37,9 +44,9 @@ public class OssStorage {
|
||||
}
|
||||
objectKey = prefix + "/" + objectKey;
|
||||
OSS client = createClient();
|
||||
try (InputStream input = Files.newInputStream(file)) {
|
||||
try {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(Files.size(file));
|
||||
metadata.setContentLength(size);
|
||||
metadata.setContentType(contentType);
|
||||
metadata.setObjectAcl(CannedAccessControlList.Private);
|
||||
client.putObject(new PutObjectRequest(properties.getBucket(), objectKey, input, metadata));
|
||||
|
||||
Reference in New Issue
Block a user