客户管理附件oss上传
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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用上传请求(多个)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user