一、七牛云入门测试
1、注册七牛云账号,完成后选择对象存储
2、在里面创建空间,一个空间相当于一个文件夹,就是将对象上传到的地方
3、查看个人秘钥,注册完成账号后,会有一个秘钥,上传文件的时候进行授权和认证。
4、文件上传测试
进入七牛云开发者中心
导入坐标:
<!--导入七牛云需要坐标-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.25</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
<scope>compile</scope>
</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>
文件上传:
UserController
package com.ztt.springboot_web_05.controller;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
* @version 1.0
* @Author 甜甜
* @since 2024/8/12
*/
@Controller
public class UserController {
//进入测试页面
@RequestMapping("/show")
public String show(){
return "index";
}
//文件上传
@RequestMapping("/fileupload")
public String fileupload(String uname, MultipartFile upic, HttpServletRequest request){
System.out.println("用户名:"+uname);
System.out.println(upic);
System.out.println(upic.getOriginalFilename());
System.out.println(upic.getName());
// 方式1.将文件upic以流的方式写入当前服务器磁盘(应用服务器)
// 方式2.文件服务器(七牛云)
// 构造一个带指定 Region 对象的配置类
Configuration cfg = new Configuration(Region.autoRegion());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "HURQbCScsb6FUTMzlKsWLh5kWMO-rznq-FyfnWb-";
String secretKey = "OpcrtNSw_6dZpt7zgCHz_C11o9QLxX1A6d4Az0nh";
String bucket = "ztiantian";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;
String name = null;
try {
byte[] uploadBytes = upic.getBytes();
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
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);//获取文件hash值
name = putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (Exception ex) {
//ignore
}
request.getSession().setAttribute("picname",name);
return "success";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
文件上传:
<ol>
<li>坐标</li>
<li>制作页面-form表单编码</li>
<li>通过*****接受文件</li>
</ol>
<hr/>
<form action="fileupload" method="post" enctype="multipart/form-data">
用户名:<input name="uname"/><br/>
图片:<input name="upic" type="file"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
测试结果:
5.文件删除测试
文件删除:
DeleteTest
package com.ztt.springboot_web_05.controller;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
* @author 甜甜
* @version 1.0
* @since 2024/8/12
*/
@Controller
public class DeleteTest {
//进入测试页面
@RequestMapping("/show1")
public String show() {
return "delete";
}
//文件上传
@RequestMapping("/filedeleteload")
public String fileupload(String uname, MultipartFile upic, HttpServletRequest request) {
System.out.println("用户名:" + uname);
System.out.println(upic);
System.out.println(upic.getOriginalFilename());
System.out.println(upic.getName());
//构造一个带指定 Region 对象的配置类
com.qiniu.storage.Configuration cfg = new Configuration(Region.autoRegion());
//...其他参数参考类注释
String accessKey = "HURQbCScsb6FUTMzlKsWLh5kWMO-rznq-FyfnWb-";
String secretKey = "OpcrtNSw_6dZpt7zgCHz_C11o9QLxX1A6d4Az0nh";
String bucket = "ztiantian";
String key = "Fi274-cC2a_OFGZZ6-RsAO-nH3nr"; //七牛云中保存的文件名
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 "success";
}
}
delete.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件删除</title>
</head>
<body>
<hr/>
文件删除:
<form action="filedeleteload" method="post" enctype="multipart/form-data">
用户名:<input name="uname"/><br/>
图片:<input name="upic" type="file"/><br/>
<input type="submit" value="删除"/>
</form>
</body>
</html>
测试结果: