阿里云本地上传文件夹内所有内容代码

public static void main(String[] args){
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
String accessKeyId = "";
String accessKeySecret = "";
// 创建OSSClient实例。
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"));
// 关闭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("文件不存在!");
}
}
posted @ 2018-11-05 10:51  包远志  阅读(791)  评论(0编辑  收藏  举报