|
| 1 | +package com.xkcoding.upload.config; |
| 2 | + |
| 3 | +import com.qiniu.common.Zone; |
| 4 | +import com.qiniu.storage.BucketManager; |
| 5 | +import com.qiniu.storage.UploadManager; |
| 6 | +import com.qiniu.util.Auth; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 12 | +import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties; |
| 13 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.web.multipart.MultipartResolver; |
| 17 | +import org.springframework.web.multipart.support.StandardServletMultipartResolver; |
| 18 | +import org.springframework.web.servlet.DispatcherServlet; |
| 19 | + |
| 20 | +import javax.servlet.MultipartConfigElement; |
| 21 | +import javax.servlet.Servlet; |
| 22 | + |
| 23 | +/** |
| 24 | + * <p> |
| 25 | + * 上传配置 |
| 26 | + * </p> |
| 27 | + * |
| 28 | + * @package: com.xkcoding.upload.config |
| 29 | + * @description: 上传配置 |
| 30 | + * @author: yangkai.shen |
| 31 | + * @date: Created in 2018/10/23 14:09 |
| 32 | + * @copyright: Copyright (c) 2018 |
| 33 | + * @version: V1.0 |
| 34 | + * @modified: yangkai.shen |
| 35 | + */ |
| 36 | +@Configuration |
| 37 | +@ConditionalOnClass({Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class}) |
| 38 | +@ConditionalOnProperty(prefix = "spring.http.multipart", name = "enabled", matchIfMissing = true) |
| 39 | +@EnableConfigurationProperties(MultipartProperties.class) |
| 40 | +public class UploadConfig { |
| 41 | + @Value("${qiniu.accessKey}") |
| 42 | + private String accessKey; |
| 43 | + |
| 44 | + @Value("${qiniu.secretKey}") |
| 45 | + private String secretKey; |
| 46 | + |
| 47 | + private final MultipartProperties multipartProperties; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + public UploadConfig(MultipartProperties multipartProperties) { |
| 51 | + this.multipartProperties = multipartProperties; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 上传配置 |
| 56 | + */ |
| 57 | + @Bean |
| 58 | + @ConditionalOnMissingBean |
| 59 | + public MultipartConfigElement multipartConfigElement() { |
| 60 | + return this.multipartProperties.createMultipartConfig(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 注册解析器 |
| 65 | + */ |
| 66 | + @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) |
| 67 | + @ConditionalOnMissingBean(MultipartResolver.class) |
| 68 | + public StandardServletMultipartResolver multipartResolver() { |
| 69 | + StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); |
| 70 | + multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily()); |
| 71 | + return multipartResolver; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 华东机房 |
| 76 | + */ |
| 77 | + @Bean |
| 78 | + public com.qiniu.storage.Configuration qiniuConfig() { |
| 79 | + return new com.qiniu.storage.Configuration(Zone.zone0()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 构建一个七牛上传工具实例 |
| 84 | + */ |
| 85 | + @Bean |
| 86 | + public UploadManager uploadManager() { |
| 87 | + return new UploadManager(qiniuConfig()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * 认证信息实例 |
| 92 | + */ |
| 93 | + @Bean |
| 94 | + public Auth auth() { |
| 95 | + return Auth.create(accessKey, secretKey); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * 构建七牛空间管理实例 |
| 100 | + */ |
| 101 | + @Bean |
| 102 | + public BucketManager bucketManager() { |
| 103 | + return new BucketManager(auth(), qiniuConfig()); |
| 104 | + } |
| 105 | +} |
0 commit comments