- 1.添加依赖
在pom.xml文件中的<dependencies>标签内添加
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
- 2.编写工具类
package com.example.demo.utils;
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 java.io.FileInputStream;
/**
* Created by on 2019/2/26.
* 七牛云图片操作工具类, 写在服务端
*/
public class QiniuUtil {
// 设置需要操作的账号的AK和SK
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
// 要上传的空间名称
private static final String BUCKETNAME = "test";
// 密钥
private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
// 外链默认域名
private static final String DOMAIN = "http://";
/**
* 将图片上传到七牛云
*/
public static String uploadQNImg(FileInputStream file, String key) {
// 构造一个带指定Zone对象的配置类, 注意这里的Zone.zone0需要根据主机选择
Configuration cfg = new Configuration(Zone.zone0());
// 其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
// 生成上传凭证,然后准备上传
try {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(BUCKETNAME);
try {
Response response = uploadManager.put(file, key, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
String returnPath = DOMAIN + "/" + putRet.key;
// 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
return returnPath;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
- 关于Zone对象和机房的关系如下
机房 | Zone对象 |
---|---|
华东 | Zone.zone0 |
华北 | Zone.zone1 |
华南 | Zone.zone2 |
北美 | Zone.zoneNa0() |
东南亚 | Zone.zoneAs0() |
- 上传方法配合Android端的使用
服务端
@RequestMapping(value = "/image", method = RequestMethod.POST)
private String postUserInforUpDate(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile) throws IOException {
// 用来获取其他参数
MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
String name = params.getParameter("username");
if (!multipartFile.isEmpty()) {
FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
String path = QiniuUtil.uploadQNImg(inputStream, NumberUtils.number() + "image"); // KeyUtil.genUniqueKey()生成图片的随机名
System.out.print("七牛云返回的图片链接:" + path);
return path;
}
return "上传失败";
}
android 端使用的是 Retrofit
// Api接口
@Multipart
@POST(ApiCommon.IMAGE_URL)
Observable<ResponseBody> postUpDataInfor(@Part("username") String username, @Part("file" + "\";filename=\"" + "user.png") RequestBody file);
// 具体实现
File file = mImgUtils.getFile();
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"),file);
// 这里进行了Mvp的封装
getPresenter().request(userName, requestBody);
网友评论