Explorar o código

文件操作,不需要往外抛出异常的,日志打印为warn

dengsixing %!s(int64=2) %!d(string=hai) anos
pai
achega
f6dc1582ba

+ 4 - 4
4dkankan-common-utils/src/main/java/com/fdkankan/common/util/FileUtils.java

@@ -202,9 +202,9 @@ public class FileUtils {
         try {
             return FileUtil.readUtf8String(path);
         } catch (IORuntimeException e) {
-            log.error("读取文件失败,文件不存在:{}", path);
-            return null;
+            log.warn("读取文件失败,文件不存在,path:"+path, e);
         }
+        return null;
     }
 
     public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
@@ -922,9 +922,9 @@ public class FileUtils {
             JSONObject jsonObj = JSON.parseObject(content);
             return jsonObj;
         } catch (Exception e) {
-            log.error("读取json失败,filePath=" + filePath, e);
-            return null;
+            log.warn("读取json失败,filePath:" + filePath, e);
         }
+        return null;
     }
 
     /**

+ 22 - 30
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -34,8 +34,7 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
         } catch (Exception e) {
-            log.error("oss上传文件失败", e);
-            e.printStackTrace();
+            log.warn("oss上传文件失败,remoteFilePath:" + remoteFilePath, e);
         }
         return null;
     }
@@ -49,9 +48,9 @@ public class OssFileService extends AbstractFYunFileService {
     public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) {
         try {
             ossClient.putObject(bucket, remoteFilePath, inputStream);
-            log.info("文件流上传成功,目标路径:path:{}", remoteFilePath);
+            log.info("文件流上传成功,目标路径:remoteFilePath:{}", remoteFilePath);
         } catch (Exception e) {
-            log.error("oss上传文件失败", e);
+            log.warn("oss上传文件失败,remoteFilePath:"+remoteFilePath, e);
             e.printStackTrace();
         }
         return null;
@@ -62,7 +61,7 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             File file = new File(filePath);
             if (!file.exists()) {
-                log.error("要上传的文件不存在:" + filePath);
+                log.warn("要上传的文件不存在,filePath" + filePath);
                 return null;
             }
             ObjectMetadata metadata = new ObjectMetadata();
@@ -83,8 +82,7 @@ public class OssFileService extends AbstractFYunFileService {
             ossClient.putObject(bucket, remoteFilePath, file, metadata);
             log.info("文件上传成功,path:{}", filePath);
         } catch (Exception e) {
-            log.error("oss上传文件失败", e);
-            e.printStackTrace();
+            log.warn("oss上传文件失败,filePath:"+filePath, e);
         }
         return null;
     }
@@ -96,9 +94,9 @@ public class OssFileService extends AbstractFYunFileService {
             String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
+            log.info("上传文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
         } catch (Exception e) {
-            log.error("上传文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
-            e.printStackTrace();
+            log.warn(String.format("上传文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
         }
         return null;
     }
@@ -110,9 +108,9 @@ public class OssFileService extends AbstractFYunFileService {
             String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType);
             log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
             callshell(command);
+            log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
         } catch (Exception e) {
-            log.error("下载文件失败, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
-            e.printStackTrace();
+            log.warn(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
         }
     }
 
@@ -121,8 +119,7 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             ossClient.deleteObject(bucket, remoteFilePath);
         } catch (Exception e) {
-            log.error("OSS删除文件失败,key=" + remoteFilePath);
-            e.printStackTrace();
+            log.error("OSS删除文件失败,key:" + remoteFilePath, e);
         }
     }
 
@@ -137,8 +134,7 @@ public class OssFileService extends AbstractFYunFileService {
             request.setKeys(remoteFiles);
             ossClient.deleteObjects(request);
         } catch (Exception e) {
-            log.error("OSS删除文件失败,key=" + remoteFolderPath);
-            e.printStackTrace();
+            log.warn("OSS删除文件失败,key:" + remoteFolderPath, e);
         }
     }
 
@@ -149,7 +145,7 @@ public class OssFileService extends AbstractFYunFileService {
                 uploadFile(bucket, entry.getKey(), entry.getValue(), null);
             }
         } catch (Exception e) {
-            log.error("OSS批量上传文件失败!");
+            log.warn("OSS批量上传文件失败!");
         }
     }
 
@@ -178,8 +174,7 @@ public class OssFileService extends AbstractFYunFileService {
                 flag = objectListing.isTruncated();
             } while (flag);
         } catch (Exception e) {
-            log.error("获取文件列表失败,path=" + sourcePath, e);
-            e.printStackTrace();
+            log.warn("获取文件列表失败,path:" + sourcePath, e);
         }
         return keyList;
     }
@@ -195,7 +190,7 @@ public class OssFileService extends AbstractFYunFileService {
                 ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
             });
         } catch (Exception e) {
-            log.error("列举文件目录失败,key=" + sourcePath);
+            log.warn("列举文件目录失败,key:" + sourcePath, e);
         }
     }
 
@@ -209,14 +204,13 @@ public class OssFileService extends AbstractFYunFileService {
                 copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
             }
         } catch (Exception e) {
-            log.error("批量复制文件失败!");
+            log.warn(String.format("批量复制文件失败, sourceBucketName:%s, targetBucketName:%s", sourceBucketName, targetBucketName), e);
         }
     }
 
     @Override
     public String getFileContent(String bucketName, String remoteFilePath) {
-        try {
-            OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath);
+        try (OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath)){
             InputStream objectContent = ossObject.getObjectContent();
             StringBuilder contentJson = new StringBuilder();
             try (BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))) {
@@ -226,14 +220,13 @@ public class OssFileService extends AbstractFYunFileService {
                     contentJson.append(line);
                 }
             } catch (IOException e) {
-                log.error("读取文件流失败,{}", remoteFilePath, e);
+                throw e;
             }
-            ossObject.close();
             return contentJson.toString();
         } catch (Exception e) {
-            log.error("获取文件内容失败:{}", remoteFilePath);
-            return null;
+            log.warn("获取文件内容失败:key:"+remoteFilePath, e);
         }
+        return null;
     }
 
     @Override
@@ -241,9 +234,9 @@ public class OssFileService extends AbstractFYunFileService {
         try {
             return ossClient.doesObjectExist(bucket, objectName);
         } catch (Exception e) {
-            log.error("判断文件是否存在失败:{}", objectName);
-            return false;
+            log.warn("判断文件是否存在失败,key:"+objectName, e);
         }
+        return false;
     }
 
     @Override
@@ -266,8 +259,7 @@ public class OssFileService extends AbstractFYunFileService {
             request.setEnableCheckpoint(true);
             ossClient.downloadFile(request);
         } catch (Throwable throwable) {
-            log.error("文件下载失败:{}", remoteFilePath);
-            throwable.printStackTrace();
+            log.warn("文件下载失败,key:"+remoteFilePath, throwable);
         }
     }