| @@ -22,6 +22,11 @@ | |||||
| <artifactId>mybatis-multi-tenancy</artifactId> | <artifactId>mybatis-multi-tenancy</artifactId> | ||||
| <version>1.0</version> | <version>1.0</version> | ||||
| </dependency> | </dependency> | ||||
| <dependency> | |||||
| <groupId>commons-fileupload</groupId> | |||||
| <artifactId>commons-fileupload</artifactId> | |||||
| <version>1.3.3</version> | |||||
| </dependency> | |||||
| </dependencies> | </dependencies> | ||||
| <build> | <build> | ||||
| <plugins> | <plugins> | ||||
| @@ -5,6 +5,7 @@ import org.rocketmq.starter.annotation.EnableRocketMQ; | |||||
| import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration; | |||||
| import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | ||||
| import tk.mybatis.spring.annotation.MapperScan; | import tk.mybatis.spring.annotation.MapperScan; | ||||
| @@ -12,7 +13,7 @@ import tk.mybatis.spring.annotation.MapperScan; | |||||
| * @author chenkx | * @author chenkx | ||||
| * @date 2017-12-26 | * @date 2017-12-26 | ||||
| */ | */ | ||||
| @SpringBootApplication | |||||
| @SpringBootApplication(exclude = {MultipartAutoConfiguration.class}) | |||||
| @MapperScan(basePackages = {"com.iformall.mapper"}) | @MapperScan(basePackages = {"com.iformall.mapper"}) | ||||
| @EnableEncryptableProperties | @EnableEncryptableProperties | ||||
| @EnableRocketMQ | @EnableRocketMQ | ||||
| @@ -17,6 +17,8 @@ import org.springframework.context.annotation.Configuration; | |||||
| import org.springframework.http.converter.HttpMessageConverter; | import org.springframework.http.converter.HttpMessageConverter; | ||||
| import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||||
| import org.springframework.web.multipart.MultipartResolver; | |||||
| import org.springframework.web.multipart.commons.CommonsMultipartResolver; | |||||
| import org.springframework.web.servlet.config.annotation.*; | import org.springframework.web.servlet.config.annotation.*; | ||||
| import java.math.BigDecimal; | import java.math.BigDecimal; | ||||
| @@ -111,4 +113,14 @@ public class WebMvcConfig implements WebMvcConfigurer { | |||||
| registrationBean.setName("koalaSignFilter"); | registrationBean.setName("koalaSignFilter"); | ||||
| return registrationBean; | return registrationBean; | ||||
| } | } | ||||
| @Bean(name = "multipartResolver") | |||||
| public MultipartResolver multipartResolver() { | |||||
| CommonsMultipartResolver resolver = new CommonsMultipartResolver(); | |||||
| resolver.setDefaultEncoding("UTF-8"); | |||||
| resolver.setResolveLazily(true); // resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常 | |||||
| // resolver.setMaxInMemorySize(40960); | |||||
| resolver.setMaxUploadSize(2*1024*1024);// 上传文件大小 5M 5*1024*1024 | |||||
| return resolver; | |||||
| } | |||||
| } | } | ||||
| @@ -31,7 +31,7 @@ import java.net.URL; | |||||
| import java.util.*; | import java.util.*; | ||||
| @RestController | @RestController | ||||
| @RequestMapping(value = "upload") | |||||
| @RequestMapping(value = "/api/upload") | |||||
| @Api(description = "文件上传接口") | @Api(description = "文件上传接口") | ||||
| public class UploadController extends BaseController { | public class UploadController extends BaseController { | ||||
| @@ -45,7 +45,7 @@ public class UploadController extends BaseController { | |||||
| private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName) { | private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName) { | ||||
| ResultData data = new ResultData(); | ResultData data = new ResultData(); | ||||
| try { | try { | ||||
| if(s3 == null) { | |||||
| if (s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | s3 = AmazonS3ClientBuilder.standard() | ||||
| .withRegion(awsProperty.getClientRegion()) | .withRegion(awsProperty.getClientRegion()) | ||||
| .withCredentials(new AWSCredentialsProvider() { | .withCredentials(new AWSCredentialsProvider() { | ||||
| @@ -143,10 +143,10 @@ public class UploadController extends BaseController { | |||||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) { | public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) { | ||||
| logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload"); | logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload"); | ||||
| if(files.length > 0){ | |||||
| if (files.length > 0) { | |||||
| ResultData data = new ResultData(); | ResultData data = new ResultData(); | ||||
| List<Map<String,String>> dataList = new ArrayList<Map<String,String>>(); | |||||
| for(MultipartFile multipartFile: files) { | |||||
| List<Map<String, String>> dataList = new ArrayList<Map<String, String>>(); | |||||
| for (MultipartFile multipartFile : files) { | |||||
| Map<String, String> map = new HashMap<>(); | Map<String, String> map = new HashMap<>(); | ||||
| ObjectMetadata metadata = new ObjectMetadata(); | ObjectMetadata metadata = new ObjectMetadata(); | ||||
| @@ -167,8 +167,8 @@ public class UploadController extends BaseController { | |||||
| } | } | ||||
| ResultData data1 = awsUpload(multipartFile, metadata, fileName); | ResultData data1 = awsUpload(multipartFile, metadata, fileName); | ||||
| if(data1.code == ResultData.SUCCESS) { | |||||
| Map _data = (Map)data1.data; | |||||
| if (data1.code == ResultData.SUCCESS) { | |||||
| Map _data = (Map) data1.data; | |||||
| map.put("url", (String) _data.get("url")); | map.put("url", (String) _data.get("url")); | ||||
| dataList.add(map); | dataList.add(map); | ||||
| } else { | } else { | ||||
| @@ -22,6 +22,11 @@ | |||||
| <artifactId>mybatis-multi-tenancy</artifactId> | <artifactId>mybatis-multi-tenancy</artifactId> | ||||
| <version>1.0</version> | <version>1.0</version> | ||||
| </dependency> | </dependency> | ||||
| <dependency> | |||||
| <groupId>commons-fileupload</groupId> | |||||
| <artifactId>commons-fileupload</artifactId> | |||||
| <version>1.3.3</version> | |||||
| </dependency> | |||||
| </dependencies> | </dependencies> | ||||
| <build> | <build> | ||||
| <plugins> | <plugins> | ||||
| @@ -5,6 +5,7 @@ import org.rocketmq.starter.annotation.EnableRocketMQ; | |||||
| import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration; | |||||
| import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | ||||
| import tk.mybatis.spring.annotation.MapperScan; | import tk.mybatis.spring.annotation.MapperScan; | ||||
| @@ -12,7 +13,7 @@ import tk.mybatis.spring.annotation.MapperScan; | |||||
| * @author chenkx | * @author chenkx | ||||
| * @date 2017-12-26 | * @date 2017-12-26 | ||||
| */ | */ | ||||
| @SpringBootApplication | |||||
| @SpringBootApplication(exclude = {MultipartAutoConfiguration.class}) | |||||
| @MapperScan(basePackages = {"com.iformall.mapper"}) | @MapperScan(basePackages = {"com.iformall.mapper"}) | ||||
| @EnableEncryptableProperties | @EnableEncryptableProperties | ||||
| @EnableRocketMQ | @EnableRocketMQ | ||||
| @@ -0,0 +1,52 @@ | |||||
| package com.iformall.config; | |||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
| import org.springframework.stereotype.Component; | |||||
| /** | |||||
| * @author Stormeye | |||||
| */ | |||||
| @Component | |||||
| @ConfigurationProperties(prefix = "aws") | |||||
| public class AwsProperty { | |||||
| // AWS ACCESS KEY | |||||
| private String access; | |||||
| private String secret; | |||||
| private String clientRegion; | |||||
| private String bucketName; | |||||
| public String getAccess() { | |||||
| return access; | |||||
| } | |||||
| public void setAccess(String access) { | |||||
| this.access = access; | |||||
| } | |||||
| public String getSecret() { | |||||
| return secret; | |||||
| } | |||||
| public void setSecret(String secret) { | |||||
| this.secret = secret; | |||||
| } | |||||
| public String getClientRegion() { | |||||
| return clientRegion; | |||||
| } | |||||
| public void setClientRegion(String clientRegion) { | |||||
| this.clientRegion = clientRegion; | |||||
| } | |||||
| public String getBucketName() { | |||||
| return bucketName; | |||||
| } | |||||
| public void setBucketName(String bucketName) { | |||||
| this.bucketName = bucketName; | |||||
| } | |||||
| } | |||||
| @@ -17,6 +17,8 @@ import org.springframework.context.annotation.Configuration; | |||||
| import org.springframework.http.converter.HttpMessageConverter; | import org.springframework.http.converter.HttpMessageConverter; | ||||
| import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||||
| import org.springframework.web.multipart.MultipartResolver; | |||||
| import org.springframework.web.multipart.commons.CommonsMultipartResolver; | |||||
| import org.springframework.web.servlet.config.annotation.*; | import org.springframework.web.servlet.config.annotation.*; | ||||
| import java.math.BigDecimal; | import java.math.BigDecimal; | ||||
| @@ -111,4 +113,14 @@ public class WebMvcConfig implements WebMvcConfigurer { | |||||
| registrationBean.setName("koalaSignFilter"); | registrationBean.setName("koalaSignFilter"); | ||||
| return registrationBean; | return registrationBean; | ||||
| } | } | ||||
| @Bean(name = "multipartResolver") | |||||
| public MultipartResolver multipartResolver() { | |||||
| CommonsMultipartResolver resolver = new CommonsMultipartResolver(); | |||||
| resolver.setDefaultEncoding("UTF-8"); | |||||
| resolver.setResolveLazily(true); // resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常 | |||||
| // resolver.setMaxInMemorySize(40960); | |||||
| resolver.setMaxUploadSize(2*1024*1024);// 上传文件大小 5M 5*1024*1024 | |||||
| return resolver; | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,211 @@ | |||||
| package com.iformall.controller; | |||||
| import com.amazonaws.AmazonClientException; | |||||
| import com.amazonaws.AmazonServiceException; | |||||
| import com.amazonaws.auth.AWSCredentials; | |||||
| import com.amazonaws.auth.AWSCredentialsProvider; | |||||
| import com.amazonaws.auth.BasicAWSCredentials; | |||||
| import com.amazonaws.services.s3.AmazonS3; | |||||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |||||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | |||||
| import com.amazonaws.services.s3.model.ObjectMetadata; | |||||
| import com.amazonaws.services.s3.model.PutObjectRequest; | |||||
| import com.iformall.common.ResultData; | |||||
| import com.iformall.config.AwsProperty; | |||||
| import io.swagger.annotations.Api; | |||||
| import io.swagger.annotations.ApiOperation; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.web.bind.annotation.PostMapping; | |||||
| import org.springframework.web.bind.annotation.RequestMapping; | |||||
| import org.springframework.web.bind.annotation.RequestParam; | |||||
| import org.springframework.web.bind.annotation.RestController; | |||||
| import org.springframework.web.multipart.MultipartFile; | |||||
| import java.io.BufferedInputStream; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | |||||
| import java.net.URL; | |||||
| import java.util.*; | |||||
| @RestController | |||||
| @RequestMapping(value = "/api/upload") | |||||
| @Api(description = "文件上传接口") | |||||
| public class UploadController extends BaseController { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||||
| @Autowired | |||||
| private AwsProperty awsProperty; | |||||
| private AmazonS3 s3 = null; | |||||
| private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName) { | |||||
| ResultData data = new ResultData(); | |||||
| try { | |||||
| if (s3 == null) { | |||||
| s3 = AmazonS3ClientBuilder.standard() | |||||
| .withRegion(awsProperty.getClientRegion()) | |||||
| .withCredentials(new AWSCredentialsProvider() { | |||||
| @Override | |||||
| public AWSCredentials getCredentials() { | |||||
| return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); | |||||
| } | |||||
| @Override | |||||
| public void refresh() { | |||||
| } | |||||
| }) | |||||
| .build(); | |||||
| } | |||||
| s3.putObject( | |||||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, multiReq.getInputStream(), metadata) | |||||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||||
| logger.info(url.toString()); | |||||
| data.code = ResultData.SUCCESS; | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| map.put("url", url.toString()); | |||||
| data.data = map; | |||||
| } catch (AmazonServiceException ase) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonServiceException, which " + | |||||
| "means your request made it " + | |||||
| "to Amazon S3, but was rejected with an error response" + | |||||
| " for some reason."); | |||||
| logger.warn(ase.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (AmazonClientException ace) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an AmazonClientException, which " + | |||||
| "means the client encountered " + | |||||
| "an internal error while trying to " + | |||||
| "communicate with S3, " + | |||||
| "such as not being able to access the network."); | |||||
| logger.warn("Error Message: " + ace.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } catch (IOException ioe) { | |||||
| data.code = ResultData.ERROR; | |||||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||||
| data.code = ResultData.ERROR; | |||||
| data.message = "上传失败"; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| /** | |||||
| * 上传文件 | |||||
| * | |||||
| * @param multiReq | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| @PostMapping(value = "/awsFileUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||||
| @ApiOperation("上传文件") | |||||
| public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) { | |||||
| logger.info("[" + getIpAddr() + "] UploadController::awsfileUpload"); | |||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | |||||
| if (dot >= 0) { | |||||
| fileName = getTenantId() + "/" + fileName + multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | |||||
| } else { | |||||
| fileName = getTenantId() + "/" + fileName; | |||||
| } | |||||
| ResultData data = awsUpload(multiReq, metadata, fileName); | |||||
| return data; | |||||
| } | |||||
| /** | |||||
| * 多文件上传 | |||||
| * | |||||
| * @param files | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| @PostMapping(value = "/awsFilesUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||||
| @ApiOperation("多文件上传") | |||||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) { | |||||
| logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload"); | |||||
| if (files.length > 0) { | |||||
| ResultData data = new ResultData(); | |||||
| List<Map<String, String>> dataList = new ArrayList<Map<String, String>>(); | |||||
| for (MultipartFile multipartFile : files) { | |||||
| Map<String, String> map = new HashMap<>(); | |||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multipartFile.getContentType()); | |||||
| metadata.setContentLength(multipartFile.getSize()); | |||||
| FileOutputStream fos = null; | |||||
| BufferedInputStream fs = null; | |||||
| map.put("key", multipartFile.getOriginalFilename()); | |||||
| String fileName = UUID.randomUUID().toString(); | |||||
| int dot = multipartFile.getOriginalFilename().lastIndexOf('.'); | |||||
| if (dot >= 0) { | |||||
| fileName = getTenantId() + "/" + fileName + multipartFile.getOriginalFilename().substring(dot, multipartFile.getOriginalFilename().length()); | |||||
| } else { | |||||
| fileName = getTenantId() + "/" + fileName; | |||||
| } | |||||
| ResultData data1 = awsUpload(multipartFile, metadata, fileName); | |||||
| if (data1.code == ResultData.SUCCESS) { | |||||
| Map _data = (Map) data1.data; | |||||
| map.put("url", (String) _data.get("url")); | |||||
| dataList.add(map); | |||||
| } else { | |||||
| // 部分成功 | |||||
| data.code = ResultData.SUCCESS; | |||||
| data.data = dataList; | |||||
| return data; | |||||
| } | |||||
| } | |||||
| data.code = ResultData.SUCCESS; | |||||
| data.data = dataList; | |||||
| return data; | |||||
| } | |||||
| return new ResultData(); | |||||
| } | |||||
| /** | |||||
| * C端上传图片文件 | |||||
| * | |||||
| * @param multiReq | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| @PostMapping(value = "/cimgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||||
| @ApiOperation("C端上传图片文件") | |||||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) { | |||||
| logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | |||||
| ObjectMetadata metadata = new ObjectMetadata(); | |||||
| metadata.setContentType(multiReq.getContentType()); | |||||
| metadata.setContentLength(multiReq.getSize()); | |||||
| String fileName = multiReq.getOriginalFilename(); | |||||
| fileName = "cimg/" + fileName; | |||||
| ResultData data = awsUpload(multiReq, metadata, fileName); | |||||
| return data; | |||||
| } | |||||
| } | |||||