|
|
@@ -46,7 +46,23 @@ public class AliOSSUtil {
|
|
|
* @return filePath
|
|
|
*/
|
|
|
public String uploadFile(MultipartFile multipartFile, String ossFilePath) {
|
|
|
+ // 验证参数
|
|
|
+ if (multipartFile == null || multipartFile.isEmpty()) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: multipartFile is null or empty");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(ossFilePath)) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: ossFilePath is empty");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
File convertFile = FileUtil.convert(multipartFile);
|
|
|
+ if (convertFile == null) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: convertFile is null");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
return uploadFile(convertFile, ossFilePath);
|
|
|
}
|
|
|
|
|
|
@@ -58,6 +74,39 @@ public class AliOSSUtil {
|
|
|
* @return filePath
|
|
|
*/
|
|
|
public String uploadFile(File file, String ossFilePath) {
|
|
|
+ // 验证参数
|
|
|
+ if (file == null) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: file is null");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(ossFilePath)) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: ossFilePath is empty");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查文件是否存在且不为空
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: file does not exist, path: {}", file.getAbsolutePath());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file.length() == 0) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: file is empty, path: {}", file.getAbsolutePath());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证ossFilePath格式 - 不能以/开头
|
|
|
+ if (ossFilePath.startsWith("/")) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: ossFilePath cannot start with '/': {}", ossFilePath);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ossFilePath.contains("..") || ossFilePath.contains("\\")) {
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: invalid ossFilePath format: {}", ossFilePath);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
// 创建OSSClient实例。
|
|
|
// 当OSSClient实例不再使用时,调用shutdown方法以释放资源。
|
|
|
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
|
|
@@ -76,7 +125,7 @@ public class AliOSSUtil {
|
|
|
}
|
|
|
return null;
|
|
|
} catch (Exception e) {
|
|
|
- log.error("AliOSSUtil.uploadFile ERROR: {}", e.getMessage());
|
|
|
+ log.error("AliOSSUtil.uploadFile ERROR: {}", e.getMessage(), e);
|
|
|
return null;
|
|
|
} finally {
|
|
|
if (ossClient != null) {
|
|
|
@@ -85,4 +134,4 @@ public class AliOSSUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|