Kaynağa Gözat

文件兼容

xucaiqin 1 ay önce
ebeveyn
işleme
66b17d24d5

+ 7 - 8
sckw-common/sckw-common-core/src/main/java/com/sckw/core/utils/FileUploadUtils.java

@@ -5,7 +5,6 @@ import cn.hutool.core.io.file.FileNameUtil;
 import cn.hutool.core.util.StrUtil;
 import com.sckw.core.exception.BusinessException;
 import com.sckw.core.exception.InvalidExtensionException;
-import com.sckw.core.web.constant.CommonConstants;
 import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -16,7 +15,6 @@ import java.util.Objects;
 
 /**
  * 文件上传工具类
- *
  */
 public class FileUploadUtils {
     /**
@@ -98,13 +96,13 @@ public class FileUploadUtils {
      * @param file             上传的文件
      * @param allowedExtension 上传文件类型
      * @return 返回上传成功的文件名
-     * @throws FileSizeLimitExceededException       如果超出最大大小
-     * @throws IOException                          比如读写文件出错时
+     * @throws FileSizeLimitExceededException 如果超出最大大小
+     * @throws IOException                    比如读写文件出错时
      */
     public static String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, IOException, InvalidExtensionException {
         int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
         if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
-            throw new BusinessException("文件名超长"+FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
+            throw new BusinessException("文件名超长" + FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
         }
         assertAllowed(file, allowedExtension);
 
@@ -134,8 +132,9 @@ public class FileUploadUtils {
         return desc;
     }
 
-    public static String getPathFileName( String fileName) throws IOException {
-        return CommonConstants.RESOURCE_PREFIX + "/" +  fileName;
+    public static String getPathFileName(String fileName) throws IOException {
+//        return CommonConstants.RESOURCE_PREFIX + "/" +  fileName;
+        return fileName;
     }
 
 
@@ -146,7 +145,7 @@ public class FileUploadUtils {
      * @return 后缀名
      */
     public static String getExtension(MultipartFile file) {
-        String extension =  FileUtil.getSuffix(file.getOriginalFilename());
+        String extension = FileUtil.getSuffix(file.getOriginalFilename());
         if (StringUtils.isEmpty(extension)) {
             extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
         }

+ 24 - 8
sckw-common/sckw-common-core/src/main/java/com/sckw/core/utils/FileUtils.java

@@ -59,6 +59,7 @@ public class FileUtils {
     private static String accessKeyId;
     private static String accessKeySecret;
     private static String bucketName;
+    private static String domain;
     private static FileUtils fileUtils;
 
     @Value("${aliyun.oss.endpoint}")
@@ -73,6 +74,8 @@ public class FileUtils {
     @Value("${aliyun.oss.bucket}")
     public String oss_bucketName;
 
+    @Value("${common.domain}")
+    private String domainUrl;
     private static void defaultOss() {
         if (StringUtils.isBlank(endpoint)) {
             endpoint = DEFAULT_ENDPOINT;
@@ -94,6 +97,7 @@ public class FileUtils {
         accessKeyId = this.oss_accessKeyId;
         accessKeySecret = this.oss_accessKeySecret;
         bucketName = this.oss_bucketName;
+        domain = this.domainUrl;
     }
 
 //    @PostConstruct
@@ -134,13 +138,14 @@ public class FileUtils {
      */
     public static String getOSSAddressPrefix() {
         defaultOss();
-        return StringConstant.HTTPS_STRING
-                + StringConstant.COLON
-                + StringConstant.LEFT_SEPARATORS
-                + bucketName
-                + StringConstant.POINT
-                + endpoint
-                + StringConstant.LEFT_SEPARATOR;
+//        return StringConstant.HTTPS_STRING
+//                + StringConstant.COLON
+//                + StringConstant.LEFT_SEPARATORS
+//                + bucketName
+//                + StringConstant.POINT
+//                + endpoint
+//                + StringConstant.LEFT_SEPARATOR;
+        return domain;
     }
 
     /**
@@ -170,7 +175,18 @@ public class FileUtils {
         if (StrUtil.startWith(replace, "http")) {
             return replace;
         }
-        return stringBuilder.append(getOSSAddressPrefix()).append(replace).toString();
+        String prefix = getOSSAddressPrefix();
+        String path = replace;
+
+        if (prefix.endsWith("/")) {
+            prefix = prefix.substring(0, prefix.length() - 1);
+        }
+        if (path.startsWith("/")) {
+            path = path.substring(1);
+        }
+
+        stringBuilder.append(prefix).append("/").append(path);
+        return stringBuilder.toString();
     }
 
 

+ 1 - 1
sckw-modules/sckw-file/src/main/resources/bootstrap.yml

@@ -13,5 +13,5 @@ spring:
 
 common:
   profile: /www/wwwroot/resources
-  domain: http://8.156.88.8:9600
+  domain: http://8.156.88.8:9600/