第一步,引入maven依赖。
<!-- 七牛云 --> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.2.0, 7.2.99]</version> </dependency>
第二步,创建QiniuController.java。
package com.allen.blog.controller; import com.allen.blog.util.QiniuUtil; 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; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; @Controller @RequestMapping("allenblog/Qiniu") public class QiniuController { @RequestMapping(value = "/qiniuUpLoadImage", method = RequestMethod.POST) @ResponseBody public Map<String, Object> qiniuUpLoadImage(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile) throws IOException { Map<String, Object> root = new HashMap<String, Object>(); if (!multipartFile.isEmpty()) { String fileType = multipartFile.getContentType(); if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpg")) { FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream(); String uuid = UUID.randomUUID().toString(); String path = QiniuUtil.uploadQNImg(inputStream, uuid); // KeyUtil.genUniqueKey()生成图片的随机名 System.out.println("七牛云返回的图片链接:" + path); root.put("name", path); root.put("url", path);//前端根据是否存在该字段来判断上传是否成功 root.put("result_msg", "图片上传成功"); return root; }else{ root.put("result_msg", "图片格式不正确"); System.out.println("图片格式不正确"); } } root.put("result_msg", "图片上传失败"); return root; } }
第三步,创建QiniuUtil.java。
package com.allen.blog.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.BucketManager; import com.qiniu.storage.Configuration; import com.qiniu.storage.Region; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; import java.io.FileInputStream; /** * 七牛云图片操作工具类, 写在服务端 */ public class QiniuUtil { // 设置需要操作的账号的AK和SK private static final String ACCESS_KEY = "************************"; private static final String SECRET_KEY = "************************"; // 要上传的空间名称 private static final String BUCKETNAME = "************************"; // 密钥 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 ""; } /** * 通过key删除七牛云上的图片 */ public void delete(String key) throws QiniuException { Configuration cfg = new Configuration(Region.region0()); Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); BucketManager bucketManager = new BucketManager(auth, cfg); try { bucketManager.delete("liqinglin0314", key); } catch (QiniuException ex) { //如果遇到异常,说明删除失败 System.err.println(ex.code()); System.err.println(ex.response.toString()); } } }
这样就可以上传图片到七牛云啦。