From 817bb83d5ec3e5ad379f10844362c74d8f836395 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Fri, 29 May 2020 16:15:25 +0800 Subject: [PATCH] Merge branch 'master' of https://gitee.com/chonggaoxiao/new_excoin.git --- src/main/java/com/xcong/excoin/common/contants/AppContants.java | 5 + src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java | 13 ++++ src/main/java/com/xcong/excoin/configurations/properties/AliOssProperties.java | 22 +++++++ src/test/java/com/xcong/excoin/KssframeworkApplicationTests.java | 11 +++ src/main/java/com/xcong/excoin/common/system/controller/CommonController.java | 24 ++++++-- src/main/java/com/xcong/excoin/utils/OssUtils.java | 33 +++++++++++ src/main/resources/application-test.yml | 7 ++ src/main/java/com/xcong/excoin/common/system/dto/Base64UploadDto.java | 20 ++++++ src/main/resources/application.yml | 7 ++ 9 files changed, 136 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/xcong/excoin/common/contants/AppContants.java b/src/main/java/com/xcong/excoin/common/contants/AppContants.java index 6ad7724..9bfbc23 100644 --- a/src/main/java/com/xcong/excoin/common/contants/AppContants.java +++ b/src/main/java/com/xcong/excoin/common/contants/AppContants.java @@ -64,4 +64,9 @@ */ public static final String VERIFY_CODE_PREFIX = "CODE_SMS_"; + /** + * 图片后缀 + */ + public static final String UPLOAD_IMAGE_SUFFIX = ".jpg"; + } diff --git a/src/main/java/com/xcong/excoin/common/system/controller/CommonController.java b/src/main/java/com/xcong/excoin/common/system/controller/CommonController.java index 8ba477e..f34d016 100644 --- a/src/main/java/com/xcong/excoin/common/system/controller/CommonController.java +++ b/src/main/java/com/xcong/excoin/common/system/controller/CommonController.java @@ -1,9 +1,13 @@ package com.xcong.excoin.common.system.controller; +import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.xcong.excoin.common.contants.AppContants; import com.xcong.excoin.common.response.Result; +import com.xcong.excoin.common.system.dto.Base64UploadDto; import com.xcong.excoin.common.system.service.CommonService; +import com.xcong.excoin.configurations.properties.AliOssProperties; +import com.xcong.excoin.utils.OssUtils; import com.xcong.excoin.utils.RedisUtils; import com.xcong.excoin.utils.SmsUtils; import com.xcong.excoin.utils.mail.SubMailSend; @@ -11,10 +15,8 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.HashMap; @@ -39,6 +41,9 @@ @Resource private CommonService commonservice; + + @Resource + private AliOssProperties aliOssProperties; @ApiOperation(value = "获取验证码接口", notes = "获取验证码通用接口") @GetMapping(value = "/verifyCode") @@ -89,7 +94,14 @@ @ApiOperation(value = "文件上次接口", notes = "文件上传") @GetMapping(value = "/uploadFile") - public Result uploadFile() { - return null; + public Result uploadFileBase64(@RequestBody @Validated Base64UploadDto uploadDto) { + String imageName = "uploadeFile/image/" + System.currentTimeMillis() + IdUtil.simpleUUID() + AppContants.UPLOAD_IMAGE_SUFFIX; + + boolean flag = OssUtils.uploadFileWithBase64(uploadDto.base64Str, imageName); + if (flag) { + String url = aliOssProperties.getBucketName() + "/" + imageName; + return Result.ok("success", url); + } + return Result.fail("上传失败"); } } diff --git a/src/main/java/com/xcong/excoin/common/system/dto/Base64UploadDto.java b/src/main/java/com/xcong/excoin/common/system/dto/Base64UploadDto.java new file mode 100644 index 0000000..c4fa5f0 --- /dev/null +++ b/src/main/java/com/xcong/excoin/common/system/dto/Base64UploadDto.java @@ -0,0 +1,20 @@ +package com.xcong.excoin.common.system.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * @author wzy + * @date 2020-05-29 + **/ +@Data +@ApiModel(value = "Base64UploadDto", description = "base64文件上传参数类") +public class Base64UploadDto { + + @NotBlank + @ApiModelProperty(value = "base64字符串") + public String base64Str; +} diff --git a/src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java b/src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java index ae63e6d..06994e0 100644 --- a/src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java +++ b/src/main/java/com/xcong/excoin/configurations/WebMvcConfig.java @@ -1,5 +1,8 @@ package com.xcong.excoin.configurations; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.xcong.excoin.configurations.properties.AliOssProperties; import com.xcong.excoin.configurations.security.UserAuthenticationArgumentResolver; import com.xcong.excoin.utils.SpringContextHolder; import lombok.extern.slf4j.Slf4j; @@ -10,6 +13,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import javax.annotation.Resource; import java.util.List; /** @@ -19,6 +23,10 @@ @SpringBootConfiguration @Slf4j public class WebMvcConfig implements WebMvcConfigurer { + + @Resource + private AliOssProperties aliOssProperties; + @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { @@ -38,6 +46,11 @@ .allowCredentials(true).maxAge(3600); } + @Bean + public OSS ossClient() { + return new OSSClientBuilder().build(aliOssProperties.getEndPoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret()); + } + // @Bean // public SpringContextHolder springContextHolder() { // return new SpringContextHolder(); diff --git a/src/main/java/com/xcong/excoin/configurations/properties/AliOssProperties.java b/src/main/java/com/xcong/excoin/configurations/properties/AliOssProperties.java new file mode 100644 index 0000000..117f13c --- /dev/null +++ b/src/main/java/com/xcong/excoin/configurations/properties/AliOssProperties.java @@ -0,0 +1,22 @@ +package com.xcong.excoin.configurations.properties; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author wzy + * @date 2020-05-29 + **/ +@Data +@Configuration +@ConfigurationProperties(prefix = "aliyun.oss") +public class AliOssProperties { + private String endPoint; + + private String bucketName; + + private String accessKeyId; + + private String accessKeySecret; +} diff --git a/src/main/java/com/xcong/excoin/utils/OssUtils.java b/src/main/java/com/xcong/excoin/utils/OssUtils.java new file mode 100644 index 0000000..eb9cc89 --- /dev/null +++ b/src/main/java/com/xcong/excoin/utils/OssUtils.java @@ -0,0 +1,33 @@ +package com.xcong.excoin.utils; + +import com.aliyun.oss.OSS; +import lombok.extern.slf4j.Slf4j; +import sun.misc.BASE64Decoder; + +import java.io.ByteArrayInputStream; + +/** + * @author wzy + * @date 2020-05-22 + **/ +@Slf4j +public class OssUtils { + + private static final OSS OSS_CLIENT = (OSS) SpringContextHolder.getBean("ossClient"); + + public static boolean uploadFileWithBase64(String base64, String pathName) { + ByteArrayInputStream stream = null; + try { + BASE64Decoder decoder = new BASE64Decoder(); + byte[] bytes = decoder.decodeBuffer(base64); + stream = new ByteArrayInputStream(bytes); + OSS_CLIENT.putObject("excoin", pathName, stream); + return true; + } catch (Exception e) { + log.error("#上传失败:{}#", e); + return false; + } + } + + +} diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index e626e2b..d168c0f 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -97,6 +97,13 @@ newest-price-update-job: true other-job: true +aliyun: + oss: + end-point: https://oss-cn-hangzhou.aliyuncs.com + bucket-name: https://excoin.oss-cn-hangzhou.aliyuncs.com + access-key-id: LTAI4GBuydqbJ5bTsDP97Lpd + access-key-secret: vbCjQtPxABWjqtUlQfzjlA0qAY96fh + rsa: public_key: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCf8UFZK54AiK4PRu7tNd+Z8qZ77o/QXCnk25DRmygVpOEu5mGNSAvfnWmKp2pEV2RljeXq3Rid/+LQkonaebMJeXKSF0yxL/VgyeT8JaQ5gNbOrdfdlc+mFkXJyzyJt8YkvApEdPRNSU2ENBn7mgRfD0BYPM4vZ6/rv+de38FJwIDAQAB private_key: MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIJ/xQVkrngCIrg9G7u0135nypnvuj9BcKeTbkNGbKBWk4S7mYY1IC9+daYqnakRXZGWN5erdGJ3/4tCSidp5swl5cpIXTLEv9WDJ5PwlpDmA1s6t192Vz6YWRcnLPIm3xiS8CkR09E1JTYQ0GfuaBF8PQFg8zi9nr+u/517fwUnAgMBAAECgYBhPt9NvpI4wbanvnndLczr2GJkxfzvSE+vwLCJF4C5FusFHVsxZINggQcg1V75bwRgCiXRMyYefreCSdrCditS43PzTOmE4RRrpxLlm8oubJc0C98LQ2qlN9AsUqL5IHpVGgbHDyWAwjc1GBID6nwXKpxq1/VodFqhahG9D5EZsQJBALnkb+5VTxQbiyQI4Uc9NIvAyVcNY1OisbvY6tvNgdBbJkADgAb78M1HWxxYjUqsvzggNHc7cWqWBHMgpnJaqm8CQQCztze4D7uAk7OC9MJHY5eE980J8Kk+GEZKxz4LahzU6V6dcb9GFac3wEtgilj/tOAn9y0/Q8sm9vvCIbMDzgzJAkEAqRYcqhF26LdVDOX25DHMBgLKISDQZFbsjA13M4/usHL4i+mjHrc0BcUOHu59NpuDI65HitzLAUSLr5zXSdUmiQJAW77wOg4GCejdXsB3IhzMsHwU97sdm26nC+vVV9xvJZ6Rx8zW+f9543NOx9U5BCmhuaVtOvvwDU9PTVcI3atmSQJAXAIJ5gGdtXx0DXiX4VvzNFHqgaqHMGvXyjNVkU2FYQbSAd2A6app4uRO+BkZu9dSjh14m+oXMnV2HzAN2rRnjA== diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 794fdc7..49a7449 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -100,6 +100,13 @@ #其他任务控制 other-job: false +aliyun: + oss: + end-point: https://oss-cn-hangzhou.aliyuncs.com + bucket-name: https://excoin.oss-cn-hangzhou.aliyuncs.com + access-key-id: LTAI4GBuydqbJ5bTsDP97Lpd + access-key-secret: vbCjQtPxABWj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2rRnjA== diff --git a/src/test/java/com/xcong/excoin/KssframeworkApplicationTests.java b/src/test/java/com/xcong/excoin/KssframeworkApplicationTests.java index df45836..7e13408 100644 --- a/src/test/java/com/xcong/excoin/KssframeworkApplicationTests.java +++ b/src/test/java/com/xcong/excoin/KssframeworkApplicationTests.java @@ -2,15 +2,18 @@ import com.xcong.excoin.common.enumerates.CoinTypeEnum; import com.xcong.excoin.common.enumerates.SymbolEnum; +import com.xcong.excoin.configurations.properties.AliOssProperties; import com.xcong.excoin.modules.test.dao.TestUserDao; import com.xcong.excoin.modules.test.entity.TestUserEntity; import com.xcong.excoin.modules.test.service.TestUserService; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.util.Date; +@Slf4j @SpringBootTest class KssframeworkApplicationTests { @@ -53,5 +56,13 @@ System.out.println(SymbolEnum.BCH.getValue()); } + @Resource + AliOssProperties aliOssProperties; + + @Test + public void aliyunOssTest() { + log.info("{}", aliOssProperties.getEndPoint()); + } + } -- Gitblit v1.9.1