浏览代码

上传文件添加返回文件路径

tianboguang 3 年之前
父节点
当前提交
e40e634b00

+ 14 - 8
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -27,9 +27,10 @@ public class OssFileService extends AbstractFYunFileService {
     private OSS ossClient;
 
     @Override
-    public void uploadFile(String bucket, byte[] data, String remoteFilePath) {
+    public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
         try {
             ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
@@ -38,24 +39,25 @@ public class OssFileService extends AbstractFYunFileService {
                 ossClient.shutdown();
             }
         }
+        return null;
     }
 
     @Override
-    public void uploadFile(String bucket, String filePath, String remoteFilePath) {
-        uploadFile(bucket, filePath, remoteFilePath, true, null);
+    public String uploadFile(String bucket, String filePath, String remoteFilePath) {
+        return uploadFile(bucket, filePath, remoteFilePath, true, null);
     }
 
     @Override
-    public void uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
-        uploadFile(bucket, filePath, remoteFilePath, true, headers);
+    public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
+        return uploadFile(bucket, filePath, remoteFilePath, true, headers);
     }
 
-    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) {
+    private String uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) {
         try {
             File file = new File(filePath);
             if (!file.exists()) {
                 log.error("要上传的文件不存在:" + filePath);
-                return;
+                return null;
             }
             ObjectMetadata metadata = new ObjectMetadata();
             if (filePath.contains(".jpg")) {
@@ -73,9 +75,11 @@ public class OssFileService extends AbstractFYunFileService {
                 }
             }
             ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata);
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
+            return null;
         } finally {
             if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(ossClient)) {
                 ossClient.shutdown();
@@ -84,15 +88,17 @@ public class OssFileService extends AbstractFYunFileService {
     }
 
     @Override
-    public void uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+    public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
         String ossPath = bucket + "/" + remoteFilePath;
         try {
             String command = String.format(OssConstants.UPLOAD_SH, ossPath, filePath);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
             CreateObjUtil.callshell(command);
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
             e.printStackTrace();
+            return null;
         }
     }
 

+ 8 - 8
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -19,23 +19,23 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
     }
 
     @Override
-    public void uploadFile(byte[] data, String remoteFilePath) {
-        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
+    public String uploadFile(byte[] data, String remoteFilePath) {
+        return uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
     }
 
     @Override
-    public void uploadFile(String filePath, String remoteFilePath) {
-        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+    public String uploadFile(String filePath, String remoteFilePath) {
+        return uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
     }
 
     @Override
-    public void uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
-        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
+    public String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
+        return uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
     }
 
     @Override
-    public void uploadFileByCommand(String filePath, String remoteFilePath) {
-        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+    public String uploadFileByCommand(String filePath, String remoteFilePath) {
+        return uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
     }
 
     @Override

+ 8 - 8
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileServiceInterface.java

@@ -19,7 +19,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(String bucket, byte[] data, String remoteFilePath) ;
+    String uploadFile(String bucket, byte[] data, String remoteFilePath) ;
 
     /**
      * 上传文件至系统默认bucket
@@ -28,7 +28,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(byte[] data, String remoteFilePath) ;
+    String uploadFile(byte[] data, String remoteFilePath) ;
 
     /**
      * 上传本地文件
@@ -38,7 +38,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(String bucket, String filePath, String remoteFilePath) ;
+    String uploadFile(String bucket, String filePath, String remoteFilePath) ;
 
     /**
      * 上传本地文件至系统默认bucket
@@ -47,7 +47,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(String filePath, String remoteFilePath) ;
+    String uploadFile(String filePath, String remoteFilePath) ;
 
     /**
      * 上传本地文件
@@ -57,7 +57,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) ;
+    String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) ;
 
     /**
      * 上传本地文件至系统默认bucket
@@ -66,7 +66,7 @@ public interface FYunFileServiceInterface {
      * @param remoteFilePath 上传后的文件路径
      * @
      */
-    void uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) ;
+    String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) ;
 
     /**
      * 通过本地脚本上传
@@ -74,7 +74,7 @@ public interface FYunFileServiceInterface {
      * @param filePath
      * @param remoteFilePath
      */
-    void uploadFileByCommand(String bucket, String filePath, String remoteFilePath) ;
+    String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) ;
 
     /**
      * 通过本地脚本上传至系统默认bucket
@@ -82,7 +82,7 @@ public interface FYunFileServiceInterface {
      * @param filePath
      * @param remoteFilePath
      */
-    void uploadFileByCommand(String filePath, String remoteFilePath) ;
+    String uploadFileByCommand(String filePath, String remoteFilePath) ;
 
     /**
      * 删除服务器文件

+ 16 - 9
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -28,12 +28,13 @@ public class S3FileService extends AbstractFYunFileService {
     private AmazonS3 s3;
 
     @Override
-    public void uploadFile(String bucket, byte[] data, String remoteFilePath){
+    public String uploadFile(String bucket, byte[] data, String remoteFilePath){
         try {
             ObjectMetadata metadata = new ObjectMetadata();
             PutObjectRequest request = new PutObjectRequest(bucket, remoteFilePath, new ByteArrayInputStream(data), metadata);
             request.withCannedAcl(CannedAccessControlList.PublicRead);
             s3.putObject(request);
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("s3上传文件失败", e);
         } finally {
@@ -41,24 +42,25 @@ public class S3FileService extends AbstractFYunFileService {
                 s3.shutdown();
             }
         }
+        return null;
     }
 
     @Override
-    public void uploadFile(String bucket, String filePath, String remoteFilePath){
-        uploadFile(bucket, filePath, remoteFilePath,true,null);
+    public String uploadFile(String bucket, String filePath, String remoteFilePath){
+        return uploadFile(bucket, filePath, remoteFilePath,true,null);
     }
 
     @Override
-    public void uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers){
-        uploadFile(bucket, filePath, remoteFilePath,true,headers);
+    public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers){
+        return uploadFile(bucket, filePath, remoteFilePath,true,headers);
     }
 
-    private void uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers){
+    private String uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers){
         try {
             File file = new File(filePath);
             if (!file.exists()) {
                 log.info("要上传s3的文件不存在");
-                return;
+                return null;
             }
 
             // 设置文件并设置公读
@@ -83,26 +85,31 @@ public class S3FileService extends AbstractFYunFileService {
             if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
                 log.info("s3上传文件成功:" + remoteFilePath);
             }
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
-            throw e;
+            log.error("文件上传失败:{}",filePath);
+            e.printStackTrace();
         } finally {
             if ((ObjectUtils.isEmpty(shutdown) || shutdown) && !ObjectUtils.isEmpty(s3)) {
                 s3.shutdown();
             }
         }
+        return null;
     }
 
     @Override
-    public void uploadFileByCommand(String bucket, String filePath, String remoteFilePath){
+    public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath){
         String ossPath = bucket + "/" + remoteFilePath;
         try {
             String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
             CreateObjUtil.callshell(command);
+            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
             e.printStackTrace();
         }
+        return null;
     }
 
     @Override