1. 常见的图片存储方案

方案一:使用nginx搭建图片服务器
方案二:使用开源的分布式文件存储系统,例如Fastdfs、HDFS等(需要运维搭建)
方案三:使用云存储,例如阿里云、七牛云等(目前最流行)

哪些图片需要部署到图片服务器上?
image.png

2. 七牛云图片存储

2.1 七牛云依赖坐标

  1. <dependency>
  2. <groupId>com.qiniu</groupId>
  3. <artifactId>qiniu-java-sdk</artifactId>
  4. <version>7.2.0</version>
  5. </dependency>

2.2 Java SDK操作七牛云

本章节我们就需要使用七牛云提供的Java SDK完成图片上传和删除,我们可以参考官方提供的例子。

上传文件案例

  1. //构造一个带指定Zone对象的配置类
  2. Configuration cfg = new Configuration(Zone.zone0());
  3. //...其他参数参考类注释
  4. UploadManager uploadManager = new UploadManager(cfg);
  5. //...生成上传凭证,然后准备上传
  6. String accessKey = "your access key";
  7. String secretKey = "your secret key";
  8. //存储空间的名称
  9. String bucket = "your bucket name";
  10. //如果是Windows情况下,格式是 D:\\qiniu\\test.png
  11. String localFilePath = "/home/qiniu/test.png";
  12. //默认不指定key的情况下,以文件内容的hash值(七牛云自己命名)作为文件名
  13. //设置key的值后,以key值为文件名上传
  14. String key = null;
  15. Auth auth = Auth.create(accessKey, secretKey);
  16. String upToken = auth.uploadToken(bucket);
  17. try {
  18. Response response = uploadManager.put(localFilePath, key, upToken);
  19. //解析上传成功的结果
  20. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  21. System.out.println(putRet.key);
  22. System.out.println(putRet.hash);
  23. } catch (QiniuException ex) {
  24. Response r = ex.response;
  25. System.err.println(r.toString());
  26. try {
  27. System.err.println(r.bodyString());
  28. } catch (QiniuException ex2) {
  29. //ignore
  30. }
  31. }

删除文件案例

  1. //构造一个带指定Zone对象的配置类
  2. Configuration cfg = new Configuration(Zone.zone0());
  3. //...其他参数参考类注释
  4. String accessKey = "your access key";
  5. String secretKey = "your secret key";
  6. String bucket = "your bucket name";
  7. String key = "your file key";
  8. Auth auth = Auth.create(accessKey, secretKey);
  9. BucketManager bucketManager = new BucketManager(auth, cfg);
  10. try {
  11. bucketManager.delete(bucket, key);
  12. } catch (QiniuException ex) {
  13. //如果遇到异常,说明删除失败
  14. System.err.println(ex.code());
  15. System.err.println(ex.response.toString());
  16. }

封装工具类

为了方便操作七牛云存储服务,我们可以将官方提供的案例简单改造成一个工具类QiniuUtils

  1. package com.itheima.util;
  2. import com.google.gson.Gson;
  3. import com.qiniu.common.QiniuException;
  4. import com.qiniu.common.Zone;
  5. import com.qiniu.http.Response;
  6. import com.qiniu.storage.BucketManager;
  7. import com.qiniu.storage.Configuration;
  8. import com.qiniu.storage.UploadManager;
  9. import com.qiniu.storage.model.DefaultPutRet;
  10. import com.qiniu.util.Auth;
  11. /**
  12. * 七牛云工具类
  13. */
  14. public class QiniuUtils {
  15. public static String accessKey = "dulF9Wze9bxujtuRvu3yyYb9JX1Sp23jzd3tO708";
  16. public static String secretKey = "vZkhW7iot3uWwcWz9vXfbaP4JepdWADFDHVLMZOe";
  17. public static String bucket = "qiniutest";
  18. public static void upload2Qiniu(String filePath,String fileName){
  19. //构造一个带指定Zone对象的配置类
  20. Configuration cfg = new Configuration(Zone.zone0());
  21. UploadManager uploadManager = new UploadManager(cfg);
  22. Auth auth = Auth.create(accessKey, secretKey);
  23. String upToken = auth.uploadToken(bucket);
  24. try {
  25. Response response = uploadManager.put(filePath, fileName, upToken);
  26. //解析上传成功的结果
  27. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  28. } catch (QiniuException ex) {
  29. Response r = ex.response;
  30. try {
  31. System.err.println(r.bodyString());
  32. } catch (QiniuException ex2) {
  33. //ignore
  34. }
  35. }
  36. }
  37. //上传文件
  38. public static void upload2Qiniu(byte[] bytes, String fileName){
  39. //构造一个带指定Zone对象的配置类
  40. Configuration cfg = new Configuration(Zone.zone0());
  41. //...其他参数参考类注释
  42. UploadManager uploadManager = new UploadManager(cfg);
  43. //默认不指定key的情况下,以文件内容的hash值作为文件名
  44. String key = fileName;
  45. Auth auth = Auth.create(accessKey, secretKey);
  46. String upToken = auth.uploadToken(bucket);
  47. try {
  48. Response response = uploadManager.put(bytes, key, upToken);
  49. //解析上传成功的结果
  50. DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
  51. System.out.println(putRet.key);
  52. System.out.println(putRet.hash);
  53. } catch (QiniuException ex) {
  54. Response r = ex.response;
  55. System.err.println(r.toString());
  56. try {
  57. System.err.println(r.bodyString());
  58. } catch (QiniuException ex2) {
  59. //ignore
  60. }
  61. }
  62. }
  63. //删除文件
  64. public static void deleteFileFromQiniu(String fileName){
  65. //构造一个带指定Zone对象的配置类
  66. Configuration cfg = new Configuration(Zone.zone0());
  67. String key = fileName;
  68. Auth auth = Auth.create(accessKey, secretKey);
  69. BucketManager bucketManager = new BucketManager(auth, cfg);
  70. try {
  71. bucketManager.delete(bucket, key);
  72. } catch (QiniuException ex) {
  73. //如果遇到异常,说明删除失败
  74. System.err.println(ex.code());
  75. System.err.println(ex.response.toString());
  76. }
  77. }
  78. }

3. 图片上传代码实现

3.1 图片上传并回显流程

图片上传回显.png
文件上传过程.png

3.2 在Controller层接收上传的文件

  1. package com.itheima.controller;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.itheima.constant.MessageConstant;
  4. import com.itheima.entity.PageResult;
  5. import com.itheima.entity.QueryPageBean;
  6. import com.itheima.entity.Result;
  7. import com.itheima.pojo.CheckGroup;
  8. import com.itheima.pojo.Setmeal;
  9. import com.itheima.service.CheckGroupService;
  10. import com.itheima.service.SetmealService;
  11. import com.itheima.utils.QiniuUtils;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.ServletContext;
  18. import javax.servlet.http.HttpServletRequest;
  19. import java.io.File;
  20. import java.util.List;
  21. import java.util.UUID;
  22. /**
  23. * 套餐管理
  24. */
  25. @RestController
  26. @RequestMapping("/setmeal")
  27. public class SetmealController {
  28. @Reference
  29. private SetmealService setmealService;
  30. //图片上传
  31. @RequestMapping("/upload")
  32. public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
  33. try{
  34. //获取原始文件名
  35. String originalFilename = imgFile.getOriginalFilename();
  36. int lastIndexOf = originalFilename.lastIndexOf(".");
  37. //获取文件后缀
  38. String suffix = originalFilename.substring(lastIndexOf);
  39. //使用UUID随机产生文件名称,防止同名文件覆盖
  40. String fileName = UUID.randomUUID().toString() + suffix;
  41. QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
  42. //图片上传成功
  43. Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
  44. result.setData(fileName);
  45. return result;
  46. }catch (Exception e){
  47. e.printStackTrace();
  48. //图片上传失败
  49. return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
  50. }
  51. }
  52. }

文件上传时,需要对文件名进行处理,保证文件名是唯一的,防止被覆盖。(实现方案:文件名使用UUID)

注意:别忘了在spring配置文件中配置文件上传组件

  1. <!--文件上传组件-->
  2. <bean id="multipartResolver"
  3. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  4. <property name="maxUploadSize" value="104857600" />
  5. <property name="maxInMemorySize" value="4096" />
  6. <property name="defaultEncoding" value="UTF-8"/>
  7. </bean>