Skip to content

Commit 1cc2d2d

Browse files
committed
springboot-qiniu init
1 parent 07d9c79 commit 1cc2d2d

File tree

12 files changed

+377
-2
lines changed

12 files changed

+377
-2
lines changed

springboot-actuator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Spring Boot执行器(Actuator)提供安全端点,用于监视和管理Spring B
44

55
## 导入依赖
66

7-
```java
7+
```
88
<!-- 执行器 -->
99
<dependency>
1010
<groupId>org.springframework.boot</groupId>

springboot-qiniu/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-qiniu/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-qiniu</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-qiniu</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
</dependency>
37+
38+
<!--七牛云sdk-->
39+
<dependency>
40+
<groupId>com.qiniu</groupId>
41+
<artifactId>qiniu-java-sdk</artifactId>
42+
<version>[7.2.0, 7.2.99]</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.alibaba</groupId>
46+
<artifactId>fastjson</artifactId>
47+
<version>1.2.56</version>
48+
<scope>compile</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootQiniuApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootQiniuApplication.class, args);
11+
}
12+
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.tellsea.config;
2+
3+
import com.google.gson.Gson;
4+
import com.qiniu.common.Zone;
5+
import com.qiniu.storage.BucketManager;
6+
import com.qiniu.storage.UploadManager;
7+
import com.qiniu.util.Auth;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@Configuration
14+
@EnableConfigurationProperties(QiniuProperties.class)
15+
public class QiniuFileConfig {
16+
17+
@Autowired
18+
private QiniuProperties qiniuProperties;
19+
20+
/**
21+
* 华东机房,配置自己空间所在的区域
22+
*/
23+
@Bean
24+
public com.qiniu.storage.Configuration qiniuConfig() {
25+
return new com.qiniu.storage.Configuration(Zone.zone0());
26+
}
27+
28+
/**
29+
* 构建一个七牛上传工具实例
30+
*/
31+
@Bean
32+
public UploadManager uploadManager() {
33+
return new UploadManager(qiniuConfig());
34+
}
35+
36+
/**
37+
* 认证信息实例
38+
* @return
39+
*/
40+
@Bean
41+
public Auth auth() {
42+
return Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
43+
}
44+
45+
/**
46+
* 构建七牛空间管理实例
47+
*/
48+
@Bean
49+
public BucketManager bucketManager() {
50+
return new BucketManager(auth(), qiniuConfig());
51+
}
52+
53+
@Bean
54+
public Gson gson() {
55+
return new Gson();
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.tellsea.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties(prefix = "qiniu")
8+
public class QiniuProperties {
9+
10+
private String accessKey;
11+
12+
private String secretKey;
13+
14+
private String bucket;
15+
16+
private String prefix;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.tellsea.service;
2+
3+
import com.qiniu.common.QiniuException;
4+
import com.qiniu.http.Response;
5+
6+
import java.io.File;
7+
import java.io.InputStream;
8+
9+
public interface QiniuService {
10+
11+
String uploadFile(File file) throws QiniuException;
12+
13+
String uploadFile(InputStream inputStream) throws QiniuException;
14+
15+
Response delete(String key) throws QiniuException;
16+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.config.QiniuProperties;
4+
import cn.tellsea.service.QiniuService;
5+
import com.alibaba.fastjson.JSON;
6+
import com.qiniu.common.QiniuException;
7+
import com.qiniu.http.Response;
8+
import com.qiniu.storage.BucketManager;
9+
import com.qiniu.storage.UploadManager;
10+
import com.qiniu.storage.model.DefaultPutRet;
11+
import com.qiniu.util.Auth;
12+
import com.qiniu.util.StringMap;
13+
import lombok.extern.slf4j.Slf4j;
14+
import org.springframework.beans.factory.InitializingBean;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
17+
import org.springframework.stereotype.Service;
18+
19+
import java.io.File;
20+
import java.io.InputStream;
21+
22+
@Slf4j
23+
@Service
24+
@EnableConfigurationProperties(QiniuProperties.class)
25+
public class QiniuServiceImpl implements QiniuService, InitializingBean {
26+
27+
@Autowired
28+
private QiniuProperties qiniuProperties;
29+
30+
@Autowired
31+
private UploadManager uploadManager;
32+
33+
@Autowired
34+
private BucketManager bucketManager;
35+
36+
@Autowired
37+
private Auth auth;
38+
39+
// 定义七牛云上传的相关策略
40+
private StringMap putPolicy;
41+
42+
43+
/**
44+
* 以文件的形式上传
45+
*
46+
* @param file
47+
* @return
48+
* @throws QiniuException
49+
*/
50+
@Override
51+
public String uploadFile(File file) throws QiniuException {
52+
Response response = this.uploadManager.put(file, null, getUploadToken());
53+
int retry = 0;
54+
while (response.needRetry() && retry < 3) {
55+
response = this.uploadManager.put(file, null, getUploadToken());
56+
retry++;
57+
}
58+
//解析结果
59+
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
60+
String return_path = qiniuProperties.getPrefix() + "/" + putRet.key;
61+
log.info("文件名称={}", return_path);
62+
return return_path;
63+
}
64+
65+
/**
66+
* 以流的形式上传
67+
*
68+
* @param inputStream
69+
* @return
70+
* @throws QiniuException
71+
*/
72+
@Override
73+
public String uploadFile(InputStream inputStream) throws QiniuException {
74+
Response response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
75+
int retry = 0;
76+
while (response.needRetry() && retry < 3) {
77+
response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
78+
retry++;
79+
}
80+
//解析结果
81+
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
82+
String return_path = qiniuProperties.getPrefix() + "/" + putRet.key;
83+
log.info("文件名称={}", return_path);
84+
return return_path;
85+
}
86+
87+
/**
88+
* 删除七牛云上的相关文件
89+
*
90+
* @param key
91+
* @return
92+
* @throws QiniuException
93+
*/
94+
@Override
95+
public Response delete(String key) throws QiniuException {
96+
Response response = bucketManager.delete(qiniuProperties.getBucket(), key);
97+
int retry = 0;
98+
while (response.needRetry() && retry++ < 3) {
99+
response = bucketManager.delete(qiniuProperties.getBucket(), key);
100+
}
101+
return response;
102+
}
103+
104+
105+
@Override
106+
public void afterPropertiesSet() throws Exception {
107+
this.putPolicy = new StringMap();
108+
putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
109+
// 自定义文件名字
110+
// putPolicy.put("saveKey", UUID.randomUUID().timestamp());
111+
}
112+
113+
/**
114+
* 获取上传凭证
115+
*
116+
* @return
117+
*/
118+
private String getUploadToken() {
119+
return this.auth.uploadToken(qiniuProperties.getBucket(), null, 3600, putPolicy);
120+
}
121+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.tellsea.web;
2+
3+
import cn.tellsea.service.QiniuService;
4+
import com.qiniu.http.Response;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import org.springframework.web.multipart.MultipartFile;
8+
9+
import java.io.IOException;
10+
11+
@RestController
12+
@RequestMapping("/qiniu")
13+
public class QiniuController {
14+
15+
@Autowired
16+
private QiniuService qiniuService;
17+
18+
/**
19+
* 以流的形式上传图片
20+
*
21+
* @param file
22+
* @return 返回访问路径
23+
* @throws IOException
24+
*/
25+
@PostMapping("upload")
26+
public String uploadFile(@RequestParam(value = "file") MultipartFile file) throws IOException {
27+
return qiniuService.uploadFile(file.getInputStream());
28+
}
29+
30+
/**
31+
* 删除文件
32+
*
33+
* @param key
34+
* @return
35+
* @throws IOException
36+
*/
37+
@GetMapping("delete/{key}")
38+
public Response deleteFile(@PathVariable String key) throws IOException {
39+
return qiniuService.delete(key);
40+
}
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
qiniu:
2+
accessKey: \u4E2A\u4EBA\u4E2D\u5FC3-->\u79D8\u94A5\u7BA1\u7406-->AccessKey
3+
secretKey: \u4E2A\u4EBA\u4E2D\u5FC3-->\u79D8\u94A5\u7BA1\u7406-->SecretKey
4+
bucket: \u5B58\u50A8\u7A7A\u95F4\u540D\u79F0
5+
prefix: \u8BBF\u95EE\u57DF\u540D
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.tellsea;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringbootQiniuApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)