springboot调用七牛云上传功能代码实现
springboot调用七牛云上传功能代码实现
引入依赖
- maven引入
1
2
3
4
5<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.7</version>
</dependency> - 在application.yml文件中配置
1
2
3
4
5
6oss:
qiniu:
domain: http://rwg3rc3zm.hd-bkt.clouddn.com
accessKey: ****************
secretKey: **************
bucketName: tongyouyun - 新建配置类上面我们将上传文件的配置封装起来。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
public class ColudConfig {
/**
* 七牛域名domain
*/
private String qiniuDomain;
/**
* 七牛ACCESS_KEY
*/
private String qiniuAccessKey;
/**
* 七牛SECRET_KEY
*/
private String qiniuSecretKey;
/**
* 七牛空间名
*/
private String qiniuBucketName;
} - 新建抽象类
1
2
3
4
5
6
7
8import com.personapi.config.ColudConfig;
import java.io.FileInputStream;
public abstract class UploadImageService {
protected ColudConfig config;
public abstract String uploadQNImg(FileInputStream file, String path);
} - 实现抽象类在util包中新建工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57import com.google.gson.Gson;
import com.personapi.config.ColudConfig;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
public class UploadImageServiceImpl extends UploadImageService{
// 七牛文件上传管理器
private UploadManager uploadManager;
private String token;
// 七牛认证管理
private Auth auth;
public UploadImageServiceImpl(ColudConfig config){
this.config = config;
init();
}
private void init(){
// 构造一个带指定Zone对象的配置类, 注意这里的Zone.zone0需要根据主机选择
uploadManager = new UploadManager(new Configuration(Zone.zone0()));
auth = Auth.create(config.getQiniuAccessKey(), config.getQiniuSecretKey());
// 根据命名空间生成的上传token
token = auth.uploadToken(config.getQiniuBucketName());
}
public String uploadQNImg(FileInputStream file, String key) {
try{
// 上传图片文件
Response res = uploadManager.put(file, key, token, null, null);
if (!res.isOK()) {
throw new RuntimeException("上传七牛出错:" + res.toString());
}
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(res.bodyString(), DefaultPutRet.class);
String path = config.getQiniuDomain() + "/" + putRet.key;
// 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
return path;
}catch (QiniuException e){
e.printStackTrace();
}
return "";
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public static String getRandomImgName(String fileName) {
int index = fileName.lastIndexOf(".");
if ((fileName == null || fileName.isEmpty()) || index == -1){
throw new IllegalArgumentException();
}
// 获取文件后缀
String suffix = fileName.substring(index);
// 生成UUID
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
// 生成上传至云服务器的路径
String path = "code/test/" + DateUtil.today() + "-" + uuid + suffix;
return path;
} - 返回保存路径
- 接下来就是在控制类中封装接口了七牛云的上传功能就整合完成了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36import com.personapi.Util.StringUtil;
import com.personapi.pojo.UploadImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.io.IOException;
public class UploadController {
UploadImageService uploadImageService;
private ResponseEntity<String> upLoadImage( MultipartFile file) throws IOException {
// 获取文件的名称
String fileName = file.getOriginalFilename();
// 使用工具类根据上传文件生成唯一图片名称
String imgName = StringUtil.getRandomImgName(fileName);
if (!file.isEmpty()) {
FileInputStream inputStream = (FileInputStream) file.getInputStream();
String path = uploadImageService.uploadQNImg(inputStream, imgName);
System.out.print("七牛云返回的图片链接:" + path);
return ResponseEntity.ok(path);
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("error");
}
}
Comments
Comment plugin failed to load
Loading comment plugin