新增七牛云功能
This commit is contained in:
parent
488d871fa4
commit
d7597eef12
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.ghy.web.controller.tool;
|
||||||
|
|
||||||
|
import com.ghy.common.core.domain.AjaxResult;
|
||||||
|
import com.ghy.common.utils.QiniuUtils;
|
||||||
|
import io.swagger.annotations.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
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.ResponseBody;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 七牛云
|
||||||
|
*
|
||||||
|
* @author clunt
|
||||||
|
*/
|
||||||
|
@Api
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/tool/qiniu")
|
||||||
|
public class QiniuController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(QiniuController.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 七牛云上传(单个)
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/upload")
|
||||||
|
public AjaxResult uploadFile(@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
|
||||||
|
try {
|
||||||
|
// 上传后返回的文件路径
|
||||||
|
String fileUrl = QiniuUtils.upload(file.getBytes());
|
||||||
|
logger.info("upload return url is : " + fileUrl);
|
||||||
|
AjaxResult ajax = AjaxResult.success();
|
||||||
|
ajax.put("url", fileUrl);
|
||||||
|
return ajax;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return AjaxResult.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -41,7 +41,7 @@ public class SwaggerConfig
|
||||||
// 扫描所有有注解的api,用这种方式更灵活
|
// 扫描所有有注解的api,用这种方式更灵活
|
||||||
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
||||||
// 扫描指定包中的swagger注解
|
// 扫描指定包中的swagger注解
|
||||||
.apis(RequestHandlerSelectors.basePackage("com.ghy.web.controller.goods"))
|
.apis(RequestHandlerSelectors.basePackage("com.ghy.web.controller.tool"))
|
||||||
// 扫描所有 .apis(RequestHandlerSelectors.any())
|
// 扫描所有 .apis(RequestHandlerSelectors.any())
|
||||||
.paths(PathSelectors.any())
|
.paths(PathSelectors.any())
|
||||||
.build();
|
.build();
|
||||||
|
|
|
||||||
|
|
@ -139,4 +139,10 @@ xss:
|
||||||
# Swagger配置
|
# Swagger配置
|
||||||
swagger:
|
swagger:
|
||||||
# 是否开启swagger
|
# 是否开启swagger
|
||||||
enabled: false
|
enabled: true
|
||||||
|
|
||||||
|
qiniu:
|
||||||
|
accessKey: 'QTNOppkvtufxTxLjt1V7YZwvzV2Rc6WLD5yXLBVY'
|
||||||
|
secretKey: 'V8SM9nkbO-dft4JmG7UaCH6RYxXdqzrvQ0zWO2W3'
|
||||||
|
bucketName: 'gqz'
|
||||||
|
mediaUrl: 'http://gqz.opsoul.com/'
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,16 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.ghy.common.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 七牛云配置
|
||||||
|
* @author clunt
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "qiniu")
|
||||||
|
public class QiniuConfig {
|
||||||
|
|
||||||
|
private static String accessKey;
|
||||||
|
|
||||||
|
private static String secretKey;
|
||||||
|
|
||||||
|
private static String bucketName;
|
||||||
|
|
||||||
|
private static String mediaUrl;
|
||||||
|
|
||||||
|
public static String getAccessKey() {
|
||||||
|
return accessKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setAccessKey(String accessKey) {
|
||||||
|
QiniuConfig.accessKey = accessKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSecretKey() {
|
||||||
|
return secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setSecretKey(String secretKey) {
|
||||||
|
QiniuConfig.secretKey = secretKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getBucketName() {
|
||||||
|
return bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBucketName(String bucketName) {
|
||||||
|
QiniuConfig.bucketName = bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getMediaUrl() {
|
||||||
|
return mediaUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setMediaUrl(String mediaUrl) {
|
||||||
|
QiniuConfig.mediaUrl = mediaUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.ghy.common.utils;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.qiniu.common.Zone;
|
||||||
|
import com.qiniu.http.Response;
|
||||||
|
import com.qiniu.storage.Configuration;
|
||||||
|
import com.ghy.common.config.QiniuConfig;
|
||||||
|
import com.ghy.common.utils.uuid.UUID;
|
||||||
|
import com.qiniu.common.QiniuException;
|
||||||
|
import com.qiniu.storage.Region;
|
||||||
|
import com.qiniu.storage.UploadManager;
|
||||||
|
import com.qiniu.storage.model.DefaultPutRet;
|
||||||
|
import com.qiniu.util.Auth;
|
||||||
|
|
||||||
|
public class QiniuUtils {
|
||||||
|
|
||||||
|
public static String getUpToken() {
|
||||||
|
Auth auth = Auth.create(QiniuConfig.getAccessKey(), QiniuConfig.getSecretKey());
|
||||||
|
return auth.uploadToken(QiniuConfig.getBucketName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String upload(byte[] uploadBytes) throws QiniuException {
|
||||||
|
|
||||||
|
String key = UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
|
|
||||||
|
Configuration cfg = new Configuration(Region.region0());//设置华南的服务器
|
||||||
|
UploadManager uploadManager = new UploadManager(cfg);
|
||||||
|
|
||||||
|
String upToken = getUpToken();
|
||||||
|
Response response = uploadManager.put(uploadBytes, key, upToken);
|
||||||
|
//解析上传成功的结果
|
||||||
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||||
|
|
||||||
|
return QiniuConfig.getMediaUrl() + putRet.key;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -45,6 +45,9 @@ public class Goods extends BaseEntity {
|
||||||
@Excel(name = "商品图片", cellType = Excel.ColumnType.IMAGE)
|
@Excel(name = "商品图片", cellType = Excel.ColumnType.IMAGE)
|
||||||
private String goodsImgUrl;
|
private String goodsImgUrl;
|
||||||
|
|
||||||
|
@Excel(name = "商品视频", cellType = Excel.ColumnType.STRING)
|
||||||
|
private String goodsVideoUrl;
|
||||||
|
|
||||||
@Excel(name = "商品库存,-1则表示无限制", cellType = Excel.ColumnType.NUMERIC)
|
@Excel(name = "商品库存,-1则表示无限制", cellType = Excel.ColumnType.NUMERIC)
|
||||||
private Integer goodsNumber;
|
private Integer goodsNumber;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
<result property="goodsSort" column="goods_sort"/>
|
<result property="goodsSort" column="goods_sort"/>
|
||||||
<result property="goodsCategoryId" column="goods_category_id"/>
|
<result property="goodsCategoryId" column="goods_category_id"/>
|
||||||
<result property="goodsImgUrl" column="goods_img_url"/>
|
<result property="goodsImgUrl" column="goods_img_url"/>
|
||||||
|
<result property="goodsVideoUrl" column="goods_video_url"/>
|
||||||
<result property="goodsNumber" column="goods_number"/>
|
<result property="goodsNumber" column="goods_number"/>
|
||||||
<result property="status" column="status"/>
|
<result property="status" column="status"/>
|
||||||
<result property="createBy" column="create_by" />
|
<result property="createBy" column="create_by" />
|
||||||
|
|
|
||||||
15
pom.xml
15
pom.xml
|
|
@ -34,11 +34,26 @@
|
||||||
<velocity.version>2.3</velocity.version>
|
<velocity.version>2.3</velocity.version>
|
||||||
<log4j2.version>2.17.1</log4j2.version>
|
<log4j2.version>2.17.1</log4j2.version>
|
||||||
<lombok.version>1.18.12</lombok.version>
|
<lombok.version>1.18.12</lombok.version>
|
||||||
|
<qiniu.version>7.4.0</qiniu.version>
|
||||||
|
<gson.version>2.8.5</gson.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<!-- 依赖声明 -->
|
<!-- 依赖声明 -->
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
<version>${qiniu.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue