新增获取最新文件方法
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -10,12 +10,12 @@
|
||||
<repository>
|
||||
<id>nexus-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>http://192.168.1.13:8001/nexus/content/repositories/releases/</url>
|
||||
<url>http://192.168.1.22:8001/nexus/content/repositories/releases/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>nexus-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>http://192.168.1.13:8001/nexus/content/repositories/snapshots/</url>
|
||||
<url>http://192.168.1.22:8001/nexus/content/repositories/snapshots/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<properties>
|
||||
@@ -56,4 +56,4 @@
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.njcn.huawei.obs.util;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njcn.huawei.obs.config.HuaweiObsProperties;
|
||||
import com.njcn.huawei.obs.constant.ExceptionConstant;
|
||||
@@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
@@ -208,6 +210,45 @@ public class OBSUtil {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 目前只给审计日志使用
|
||||
*
|
||||
* @author cdf
|
||||
* @date 2024/10/15
|
||||
*/
|
||||
public File getLastFile(String directory) {
|
||||
File file = null;
|
||||
ObsClient obsClient = null;
|
||||
try {
|
||||
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(huaweiObsProperties.getObs().getBucket());
|
||||
listObjectsRequest.setPrefix(directory);
|
||||
obsClient = this.huaweiObsProperties.getInstance();
|
||||
ObjectListing objectListing = obsClient.listObjects(listObjectsRequest);
|
||||
ObsObject latestObsObject = null;
|
||||
for (ObsObject obsObject : objectListing.getObjects()) {
|
||||
System.out.println("Object Key: " + obsObject.getMetadata());
|
||||
|
||||
ObjectMetadata item = obsObject.getMetadata();
|
||||
if (latestObsObject == null || item.getLastModified().after(latestObsObject.getMetadata().getLastModified())) {
|
||||
latestObsObject = obsObject;
|
||||
}
|
||||
}
|
||||
if (latestObsObject != null) {
|
||||
System.out.println("文件对象--------------" + latestObsObject.getObjectKey());
|
||||
InputStream stream = latestObsObject.getObjectContent();
|
||||
file = File.createTempFile("lastObsLogs", "xlsx");
|
||||
FileUtil.writeFromStream(stream, file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取指定目录下面最新文件失败");
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
huaweiObsProperties.destroy(obsClient);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 暂时未知河北现场的桶是否固定,将多个业务文件存在一个桶内也没问题
|
||||
* 判断是否存在桶,不存在则创建通
|
||||
|
||||
Reference in New Issue
Block a user