11111
parent
003440340d
commit
84069ba64e
|
@ -38,9 +38,9 @@ public class DownloadOssSynchronization implements ApplicationRunner {
|
|||
/**
|
||||
* 服务器项目路径放文件
|
||||
*/
|
||||
private static String serverClassPath ="home/lib/com/muyu/rule/common/engine/value/";
|
||||
private static String serverClassPath = "home/lib/com/muyu/rule/common/engine/value/";
|
||||
|
||||
private static String bucketName = "zcz-vfd-1000";
|
||||
private static String bucketName = "zcz-vfd-1000";
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
|
@ -52,43 +52,40 @@ private static String bucketName = "zcz-vfd-1000";
|
|||
// 循环获取存储桶中的所有对象
|
||||
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
||||
String key = objectSummary.getKey();
|
||||
downloadFile(ossClient, bucketName, key,serverClassPath );
|
||||
downloadFile(ossClient, bucketName, key, serverClassPath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 关闭OSSClient
|
||||
ossClient.shutdown();
|
||||
|
||||
//下载完成实现批量批量类加载
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath){
|
||||
private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
||||
|
||||
//创建服务器项目容器的文件目录路径
|
||||
File serverFile = new File(serverPath, key);
|
||||
//如果目录不存在,则创建目录
|
||||
if (!serverFile.getParentFile().exists()){
|
||||
if (!serverFile.getParentFile().exists()) {
|
||||
serverFile.getParentFile().mkdirs();
|
||||
}
|
||||
//下载文件到项目中
|
||||
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
||||
|
||||
try(FileOutputStream fos = new FileOutputStream(serverFile);){
|
||||
try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int byteRead;
|
||||
while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1){
|
||||
fos.write(buffer,0,byteRead);
|
||||
while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, byteRead);
|
||||
}
|
||||
log.info("文件 " +key +"已经下载到" + serverFile.getAbsolutePath());
|
||||
}catch (IOException e){
|
||||
log.error("下载文件失败:"+key);
|
||||
log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
log.error("下载文件失败:" + key);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue