参考
准备
创建空间
上传文件并访问
在此处点击上传文件,选择文件,上传完成后
创建密钥
开发文档
点击文档-开发者中心,进入开发文档
基本使用
引入依赖
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.13.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
上传文件
@Test
public void testUploadQiniu() {
//构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.huanan()); // 指定区域
cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "~~~";
String secretKey = "~~~";
String bucket = "zzhua-space";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = "20240826/naughty.gif"; // 文件名
try {
// byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
String filePath = "C:\\Users\\zzhua195\\Desktop\\naughty.gif";
File file = new File(filePath);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
byte[] uploadBytes = StreamUtils.copyToByteArray(new FileInputStream(file));
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
// 访问路径为: {外链域名}/20240826/naughty.gif
} catch (Exception ex) {
//ignore
System.out.println("发生错误" + ex);
}
}
由于是公有空间,所以能直接访问
下载文件
@Test
void testPublicDownloadQiniu() throws Exception {
String fileName = "soft-dev.png";
// domainOfBucket 中的域名为用户 bucket 绑定的下载域名,下面域名仅为示例,不可使用
String domainOfBucket = "sit9jits9.hn-bkt.clouddn.com";
String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
// 示例: sit9jits9.hn-bkt.clouddn.com/20240826%2Fnaughty.gif
System.out.println(finalUrl);
// domain 用户 bucket 绑定的下载域名 eg: mock.qiniu.com【必须】
// useHttps 是否使用 https【必须】
// key 下载资源在七牛云存储的 key【必须】
DownloadUrl url = new DownloadUrl(domainOfBucket, false, fileName);
/*url.setAttname(attname) // 配置 attname
.setFop(fop) // 配置 fop
.setStyle(style, styleSeparator, styleParam) // 配置 style*/
String urlString = url.buildURL();
// 示例: http://sit9jits9.hn-bkt.clouddn.com/20240826/naughty.gif
System.out.println(urlString);
}
@Test
void testPrivateDownloadQiniu() throws Exception {
String fileName = "soft-dev.png";
String domainOfBucket = "sit9jits9.hn-bkt.clouddn.com";
String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
String accessKey = "~~~";
String secretKey = "~~~";
Auth auth = Auth.create(accessKey, secretKey);
long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
System.out.println(finalUrl);
// domain 下载 domain, eg: qiniu.com【必须】
// useHttps 是否使用 https【必须】
// key 下载资源在七牛云存储的 key【必须】
DownloadUrl url = new DownloadUrl(domainOfBucket, false, fileName);
/*url.setAttname(attname) // 配置 attname
.setFop(fop) // 配置 fop
.setStyle(style, styleSeparator, styleParam) // 配置 style*/
// 带有效期
long expireInSeconds2 = 3600L;//1小时,可以自定义链接过期时间
long deadline = System.currentTimeMillis()/1000 + expireInSeconds2;
Auth auth2 = Auth.create(accessKey, secretKey);
String urlString = url.buildURL(auth2, deadline);
System.out.println(urlString);
}