阿里云本地上传文件夹内所有内容代码
生活随笔
收集整理的這篇文章主要介紹了
阿里云本地上传文件夹内所有内容代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public static void main(String[] args){
// Endpoint以杭州為例,其它Region請(qǐng)按實(shí)際情況填寫。
String endpoint = "";
// 阿里云主賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問或日常運(yùn)維,請(qǐng)登錄 https://ram.console.aliyun.com 創(chuàng)建RAM賬號(hào)。
String accessKeyId = "";
String accessKeySecret = "";
// 創(chuàng)建OSSClient實(shí)例。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String path = "C:\\Users\\Administrator\\Desktop\\static";
CustomerCookiesSerializer cus = new CustomerCookiesSerializer();
cus.traverseFolder2(path, ossClient);
// 上傳文件。<yourLocalFile>由本地文件路徑加文件名包括后綴組成,例如/users/local/myfile.txt。
//ossClient.putObject("manhuicloud", "static", new File("E:\\static\\zTreeV3\\log v3.x.txt"));
// 關(guān)閉OSSClient。
ossClient.shutdown();
}
//遍歷文件夾上傳文件
public void traverseFolder2(String path, OSSClient ossClient) { File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夾是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夾:" + file2.getAbsolutePath());
traverseFolder2(file2.getAbsolutePath(), ossClient);
} else {
System.out.println("文件:" + file2.getAbsolutePath());
String absolutePath = file2.getAbsolutePath();
absolutePath = absolutePath.substring(absolutePath.indexOf("static"), absolutePath.length()).replaceAll("\\\\", "/");
//上傳文件到static目錄下面
System.out.println(absolutePath);
ossClient.putObject("manhuicloud", absolutePath, new File(file2.getAbsolutePath()));
}
}
}
} else {
System.out.println("文件不存在!");
}
}
// Endpoint以杭州為例,其它Region請(qǐng)按實(shí)際情況填寫。
String endpoint = "";
// 阿里云主賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問或日常運(yùn)維,請(qǐng)登錄 https://ram.console.aliyun.com 創(chuàng)建RAM賬號(hào)。
String accessKeyId = "";
String accessKeySecret = "";
// 創(chuàng)建OSSClient實(shí)例。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String path = "C:\\Users\\Administrator\\Desktop\\static";
CustomerCookiesSerializer cus = new CustomerCookiesSerializer();
cus.traverseFolder2(path, ossClient);
// 上傳文件。<yourLocalFile>由本地文件路徑加文件名包括后綴組成,例如/users/local/myfile.txt。
//ossClient.putObject("manhuicloud", "static", new File("E:\\static\\zTreeV3\\log v3.x.txt"));
// 關(guān)閉OSSClient。
ossClient.shutdown();
}
//遍歷文件夾上傳文件
public void traverseFolder2(String path, OSSClient ossClient) { File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夾是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夾:" + file2.getAbsolutePath());
traverseFolder2(file2.getAbsolutePath(), ossClient);
} else {
System.out.println("文件:" + file2.getAbsolutePath());
String absolutePath = file2.getAbsolutePath();
absolutePath = absolutePath.substring(absolutePath.indexOf("static"), absolutePath.length()).replaceAll("\\\\", "/");
//上傳文件到static目錄下面
System.out.println(absolutePath);
ossClient.putObject("manhuicloud", absolutePath, new File(file2.getAbsolutePath()));
}
}
}
} else {
System.out.println("文件不存在!");
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/baobaodaren/p/9907878.html
總結(jié)
以上是生活随笔為你收集整理的阿里云本地上传文件夹内所有内容代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腐蚀rust高速箭怎么做不了_腐蚀RUS
- 下一篇: php js实现录制mp4视频,并上传视