[Java] 纯文本查看 复制代码
package cn.stone.demo.qiniu;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
/**
*
* [url=home.php?mod=space&uid=686208]@AuThor[/url] xiaoshitou
* [url=home.php?mod=space&uid=686237]@date[/url] 2018年5月4日 上午8:29:32
* @version 1.0 说明: 将图片上传至七牛云
*/
@RestController
public class QiNiuUp {
// 设置好账号的ACCESS_KEY和SECRET_KEY
@Value("${QiNiu.ACCESS_KEY}")
String accessKey;
@Value("${QiNiu.SECRET_KEY}")
String secretKey;
// 要上传的空间名--
@Value("${QiNiu.bucketname}")
String bucket;
@RequestMapping("/upFile")
public String upFile() {
// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
UploadManager uploadManager = new UploadManager(cfg);
// 指定上传的文件名 为空就是自动生成
String key = "保存文件名.jpg";
String localFilePath = "c:/headimg_dl.jpg";
Auth auth = Auth.create(accessKey, secretKey);
// 获取上传凭证
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
}
return "";
}
@RequestMapping("/getFile")
public String getfile() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String key = "保存文件名.jpg";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
FileInfo fileInfo = null ;
try {
fileInfo = bucketManager.stat(bucket, key);
System.out.println(fileInfo.hash);
System.out.println(fileInfo.fsize);
System.out.println(fileInfo.mimeType);
System.out.println(fileInfo.putTime);
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
return "";
}
@RequestMapping("/updateFile")
public String updateFile() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String key = "保存文件名.jpg";
String newMimeType = "xiaoshitou.jpg";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
//修改文件类型
try {
bucketManager.changeMime(bucket, key, newMimeType);
} catch (QiniuException ex) {
System.out.println(ex.response.toString());
}
return newMimeType;
}
@RequestMapping("/deleteFile")
public String deleteFile() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String key = "xiaoshitou.jpg";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(bucket, key);
} catch (QiniuException ex) {
//如果遇到异常,说明删除失败
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
return key;
}
@RequestMapping("/getFileList")
public String getFileList() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
//文件名前缀
String prefix = "";
//每次迭代的长度限制,最大1000,推荐值 1000
int limit = 1000;
//指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
String delimiter = "";
//列举空间文件列表
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
while (fileListIterator.hasNext()) {
//处理获取的file list结果
FileInfo[] items = fileListIterator.next();
for (FileInfo item : items) {
System.out.println(item.key);
System.out.println(item.hash);
System.out.println(item.fsize);
System.out.println(item.mimeType);
System.out.println(item.putTime);
System.out.println(item.endUser);
}
}
return "";
}
@RequestMapping("/getForUrl")
public String getForUrl() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
String key = "qiniu.png";
String remoteSrcUrl = "http://devtools.qiniu.com/qiniu.png";
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
//抓取网络资源到空间
try {
FetchRet fetchRet = bucketManager.fetch(remoteSrcUrl, bucket, key);
System.out.println(fetchRet.hash);
System.out.println(fetchRet.key);
System.out.println(fetchRet.mimeType);
System.out.println(fetchRet.fsize);
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
return remoteSrcUrl;
}
@RequestMapping("/getFiles")
public String getFiles() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
String[] keyList = new String[]{
"qiniu.jpg",
"xiaoshitou.jpg",
"Alan Walker - Faded.mp3",
};
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addStatOps(bucket, keyList);
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < keyList.length; i++) {
BatchStatus status = batchStatusList[i];
String key = keyList[i];
System.out.print(key+"\t");
if (status.code == 200) {
//文件存在
System.out.println(status.data.hash);
System.out.println(status.data.mimeType);
System.out.println(status.data.fsize);
System.out.println(status.data.putTime);
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
return "";
}
@RequestMapping("/updateFiles")
public String updateFiles() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
HashMap<String, String> keyMimeMap = new HashMap<>();
keyMimeMap.put("qiniu.jpg", "image/jpg");
keyMimeMap.put("qiniu.png", "image/png");
keyMimeMap.put("qiniu.mp4", "video/mp4");
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
//添加指令
for (Map.Entry<String, String> entry : keyMimeMap.entrySet()) {
String key = entry.getKey();
String newMimeType = entry.getValue();
batchOperations.addChgmOp(bucket, key, newMimeType);
}
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
int index = 0;
for (Map.Entry<String, String> entry : keyMimeMap.entrySet()) {
String key = entry.getKey();
System.out.print(key + "\t");
BatchStatus status = batchStatusList[index];
if (status.code == 200) {
System.out.println("change mime success");
} else {
System.out.println(status.data.error);
}
index += 1;
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
return "";
}
@RequestMapping("/deleteFiles")
public String deleteFiles() {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
//单次批量请求的文件数量不得超过1000
String[] keyList = new String[]{
"qiniu.jpg",
"qiniu.mp4",
"qiniu.png",
};
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
batchOperations.addDeleteOp(bucket, keyList);
Response response = bucketManager.batch(batchOperations);
BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
for (int i = 0; i < keyList.length; i++) {
BatchStatus status = batchStatusList[i];
String key = keyList[i];
System.out.print(key + "\t");
if (status.code == 200) {
System.out.println("delete success");
} else {
System.out.println(status.data.error);
}
}
} catch (QiniuException ex) {
System.err.println(ex.response.toString());
}
return "";
}
}