原文地址:https://www.ak47007.cn/article/details/8e133cc0-faaa-49a7-8c52-a159e464af86
首先是页面,页面模板引擎用thymeleaf,所以需要引入一个依赖
<!--thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后是页面代码,要使用th属性需要在html标签中添加xmlns:th="http://www.thymeleaf.org"
<form th:action="@{~/uploadPictures}"
method="post" enctype="multipart/form-data">
<div id="outbox">
<input type="submit" value="提交"/>
<div id="dropbox">
<input id="imgUpload" type="file" multiple name="files">
<span>选取图片</span>
</div>
</div>
<div id="preview"></div>
</form>
上传图片需要引入上传文件的依赖
<!--上传文件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
接下来,我们先配置一个SPRINGBOOT的配置文件
qiniu:
accessKey: AK
secretKey: SK
bucket: 存储名称
path: 外链地址
spring:
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
suffix: .html
encoding: UTF-8
mode: LEGACYHTML5
cache: false
#设置上传文件大小
servlet:
multipart:
max-request-size: 30MB
max-file-size: 30MB
上传到七牛云需要引入七牛云的依赖
<!--七牛云-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
<!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
然后我们创建一个七牛云的配置类
这是QiNiuYunUtil类的代码
QiNiuYunUtil中会有4个属性,这4个属性都是跟配置文件中的对应,并且还需要getter/setter
package cn.ak47007.springboot.util;
import com.google.gson.Gson;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.FileInputStream;
/**
* @Author AK47007
* @Date 2019/6/5 20:41
* Describe:
* @Component 将类实例化到SPRING容器中
* @ConfigurationProperties 指定前缀读取配置文件
*/
@Component
@ConfigurationProperties(prefix = "qiniu")
public class QiNiuYunUtil {
/**
* AccessKey
*/
private String accessKey;
/**
* SecretKey
*/
private String secretKey;
/**
* 存储空间名
*/
private String bucket;
/**
* 外链
*/
private String path;
/**
* 将图片上传到七牛云
*/
public String uploadQiniuYun(FileInputStream file) {
System.out.println("accessKey=" + accessKey);
// zone0华东区域,zone1是华北区域,zone2是华南区域
Configuration cfg = new Configuration(Zone.zone2());
UploadManager uploadManager = new UploadManager(cfg);
// 生成上传凭证,然后准备上传
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file, null, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return path + "/" + putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
配置类写完后就到Controller写一个上传的方法
@Autowired
private QiNiuYunUtil qiNiuYunUtil;
@RequestMapping("/uploadPictures")
public String uploadPictures(@RequestParam("files") MultipartFile[] files) {
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
try {
FileInputStream fileInputStream = (FileInputStream) files[i].getInputStream();
String url = qiNiuYunUtil.uploadQiniuYun(fileInputStream);
//输出url上传后的,可以复制url到浏览器访问
System.out.println("url=" + url);
try {
//延迟两秒让七牛云缓一下
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
}
至此已完成!