标签:
public class UploadFile {
//设置好账号的ACCESS_KEY和SECRET_KEY
String ACCESS_KEY = "akey";
String SECRET_KEY = "skey";
//要上传的空间
String bucketname = "bucketname";//
//下载路径(空间里的外链复制过来)
String downPath = "http://o6wcyzstb.bkt.clouddn.com/";
//密钥配置
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
//创建上传对象
UploadManager uploadManager = new UploadManager();
//批量下载文件
public void download() throws IOException {
//实例化一个BucketManager对象
BucketManager bucketManager = new BucketManager(auth);
FileListIterator flit = bucketManager.createFileListIterator(bucketname, "temp");//此处的temp,换成你想要下载的文件有共同字母开头的名称的字符串(相当于like的意思)
String path = "";
while(flit.hasNext()){
FileInfo[] fi = flit.next();
for(int i=0;i<fi.length;i++){
path = downPath + fi[i].key;
String downloadRUL = auth.privateDownloadUrl(path,3600);
URL url = new URL(downloadRUL);
String filename = fi[i].key.replaceAll("/", "-");//这是把文件名字带有/的替换成-
File outFile = new File("D:\\shipin\\"+filename);//下载存放的位置
OutputStream os = new FileOutputStream(outFile);
InputStream is = url.openStream();
byte[] buff = new byte[1024];
while(true) {
int readed = is.read(buff);
if(readed == -1) {
break;
}
byte[] temp = new byte[readed];
System.arraycopy(buff, 0, temp, 0, readed);
os.write(temp);
}
is.close();
os.close();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/lays/p/5555122.html