1、controller
package com.example.upload_img.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping(value = "sys")
public class UpLoadController {
/**
* 上传文件
*
* @param image
* @param request
* @return
*/
@ResponseBody
@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
public String uploadImg(@RequestParam MultipartFile image, HttpServletRequest request) {
if (image.isEmpty()) {
return null;
}
String url = "";
try {
//使用base64方式上传到七牛云
//此处 zoneName 必须为1 选择存储区域 的时候,区域必须 与 构建配置类中的 Zone.zone0() 一 一对应
url = UploadQiNiuUtil.upload(image.getInputStream(), 1);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
}
2、UploadQiNiuUtil 工具类
package com.example.upload_img.controller;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import java.io.File;
import java.io.InputStream;
public class UploadQiNiuUtil {
//
/**
* AK/SK 存储空间名称
* 设置好账号的ACCESS_KEY和SECRET_KEY
*/
private static String ACCESS_KEY = "you access";
private static String SECRET_KEY = "you secret";
/**
* 要上传的空间
* 七牛云空间存储空间名称
*/
public static final String bucket = "you bucket";
/**
* 七牛绑定的自定义域名
*/
public static final String BUCKET_HOST_NAME = "https://miniapp.qiluyidian.mobi";
/**
* 你的文件上传路径
*/
public static final String DOMAIN = "";
public static String token(String bucketName) {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(bucketName);
return upToken;
}
/**
* 默认存储空间
*
* @return
*/
public static String token() {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(bucket);
return upToken;
}
/**
* 上传图片
*
* @param file 图片
* @param fileName 文件名字
* @param bucketName
* @param zoneName 获取存储区域名称
* @return
*/
public static String uploadFile(File file, String fileName, String bucketName, Integer zoneName) {
try {
Zone zone = UploadQiNiuUtil.getZone(zoneName);
Configuration cfg = new Configuration(zone);
UploadManager uploadManager = new UploadManager(cfg);
Response response = uploadManager.put(file, fileName, token(bucketName));
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
return putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.out.println(fileName + "----文件上传失败----" + r.toString());
try {
System.err.println(r.bodyString());
System.out.println(fileName + "----文件上传失败----" + r.toString());
} catch (QiniuException ex2) {
// ignore
}
return null;
}
}
/**
* 通过输入流上传
*
* @param inputStream
* @return
* @throws
*/
public static String upload(InputStream inputStream,Integer zoneName) {
Zone zone = UploadQiNiuUtil.getZone(zoneName);
String imgUrl = "";
Configuration cfg = new Configuration(zone);
//创建上传对象
UploadManager uploadManager = new UploadManager(cfg);
//返回
String key = String.valueOf(System.currentTimeMillis());
try {
try {
Response response = uploadManager.put(inputStream, key, UploadQiNiuUtil.token(), null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
imgUrl = BUCKET_HOST_NAME + "/" + putRet.key;
} catch (QiniuException ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return imgUrl;
}
/**
* 获取存储区域
*
* @param zoneName
* @return
*/
private static Zone getZone(Integer zoneName) {
Zone zone = Zone.zoneNa0();
if (null == zoneName) {
return zone;
}
if (zoneName == 0) {
//华南
zone = Zone.zone0();
}
if (zoneName == 1) {
//华北
zone = Zone.zone1();
}
if (zoneName == 2) {
//华东
zone = Zone.zone2();
}
return zone;
}
/**
* 根据Key值删除云端文件
*
* @param key
*/
public static Boolean deleteFile(String key) {
Configuration cfg = new Configuration(Zone.zone2());
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
Response result = bucketManager.delete(bucket, key);
System.err.println(new Gson().toJson(result));
if (result.statusCode == 200) {
return true;
}
} catch (QiniuException ex) {
//如果遇到异常,说明删除失败
System.out.println(key + "----删除失败----" + ex.code());
System.out.println(key + "----删除失败----" + ex.response.toString());
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
return false;
}
}
3、pom
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.qiniu/qiniu-java-sdk -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.17</version>
</dependency>