he2802 2 лет назад
Родитель
Сommit
4ac8e8b1ea

+ 51 - 32
zhongzheng-system/src/main/java/com/zhongzheng/modules/alioss/service/impl/OssServiceImpl.java

@@ -15,6 +15,7 @@ import com.aliyuncs.exceptions.ClientException;
 import com.zhongzheng.common.constant.Constants;
 import com.zhongzheng.common.core.domain.AjaxResult;
 import com.zhongzheng.common.exception.CustomException;
+import com.zhongzheng.common.utils.file.FileUtils;
 import com.zhongzheng.modules.alioss.bo.OssCallbackParam;
 import com.zhongzheng.modules.alioss.bo.OssRequest;
 import com.zhongzheng.modules.alioss.service.OssService;
@@ -252,8 +253,10 @@ public class OssServiceImpl implements OssService {
             //获取上传文件输入流
             InputStream inputStream = file.getFile().getInputStream();
             String originalFilename = file.getFile().getOriginalFilename();
+            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
 
-            String fileName = ALIYUN_OSS_DIR_PREFIX + generateRandomFilename(file) ;
+            String fileName = ALIYUN_OSS_DIR_PREFIX + generateRandomFilename(file) + "." + suffix;
+//            String fileName = ALIYUN_OSS_DIR_PREFIX + generateRandomFilename(file) ;
 
             //调用oss方法实现上传
             //第一个参数  Bucket名称
@@ -269,6 +272,51 @@ public class OssServiceImpl implements OssService {
         }
     }
 
+    @Override
+    public String uploadWithPath(OssRequest base, String path) throws Exception {
+        try {
+            //获取上传文件输入流
+            InputStream inputStream = base.getFile().getInputStream();
+            String originalFilename = base.getFile().getOriginalFilename();
+
+            PutObjectResult putObjectResult = ossClient.putObject(ALIYUN_OSS_BUCKET_NAME, path, inputStream);
+            // 关闭OSSClient。
+            //ossClient.shutdown();
+            return path;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new IllegalStateException("上传失败");
+        }
+    }
+
+    @Override
+    public String uploadWithUrl(OssRequest base, String urlString){
+        String path = null;
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(urlString);
+            URLConnection con = url.openConnection();
+            //获取上传文件输入流
+            inputStream =con.getInputStream();
+            path = uploadInputStream(inputStream,base.getImageStatus());
+
+            // 关闭OSSClient。
+            //ossClient.shutdown();
+            return path;
+        } catch (Exception e) {
+
+        }finally {
+            try {
+                if (null != inputStream) {
+                    inputStream.close();
+                }
+            }catch (IOException e){
+
+            }
+            return path;
+        }
+    }
+
     @Override
     public String uploadInputStream(InputStream inputStream, Integer imageStatus) throws Exception {
         try {
@@ -354,8 +402,8 @@ public class OssServiceImpl implements OssService {
 
     @Override
     public InputStream getStreamByObject(String filePath) throws Exception {
-            OSSObject ossObject = ossClient.getObject(ALIYUN_OSS_BUCKET_NAME, filePath);
-            return ossObject.getObjectContent();
+        OSSObject ossObject = ossClient.getObject(ALIYUN_OSS_BUCKET_NAME, filePath);
+        return ossObject.getObjectContent();
     }
 
     @Override
@@ -384,35 +432,6 @@ public class OssServiceImpl implements OssService {
         }
     }
 
-    @Override
-    public String uploadWithUrl(OssRequest base, String urlString){
-        String path = null;
-        InputStream inputStream = null;
-        try {
-            URL url = new URL(urlString);
-            URLConnection con = url.openConnection();
-            //获取上传文件输入流
-            inputStream =con.getInputStream();
-            path = uploadInputStream(inputStream,base.getImageStatus());
-
-            // 关闭OSSClient。
-            //ossClient.shutdown();
-            return path;
-        } catch (Exception e) {
-
-        }finally {
-            try {
-                if (null != inputStream) {
-                    inputStream.close();
-                }
-            }catch (IOException e){
-
-            }
-            return path;
-        }
-    }
-
-
     public void zipFile(List<String> fileList, ZipOutputStream zipOut,String dir) {
         if (CollectionUtils.isEmpty(fileList)) {
             return;