first commit
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.peanut.common.exception.RRException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public class AliyunCloudStorageService extends CloudStorageService {
|
||||
private OSSClient client;
|
||||
|
||||
public AliyunCloudStorageService(CloudStorageConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
|
||||
config.getAliyunAccessKeySecret());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
return upload(new ByteArrayInputStream(data), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
client.putObject(config.getAliyunBucketName(), path, inputStream);
|
||||
} catch (Exception e){
|
||||
throw new RRException("上传文件失败,请检查配置信息", e);
|
||||
}
|
||||
|
||||
return config.getAliyunDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getAliyunPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getAliyunPrefix(), suffix));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
|
||||
import com.peanut.common.validator.group.AliyunGroup;
|
||||
import com.peanut.common.validator.group.QcloudGroup;
|
||||
import com.peanut.common.validator.group.QiniuGroup;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Data
|
||||
public class CloudStorageConfig implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
//类型 1:七牛 2:阿里云 3:腾讯云
|
||||
@Range(min=1, max=3, message = "类型错误")
|
||||
private Integer type;
|
||||
|
||||
//七牛绑定的域名
|
||||
@NotBlank(message="七牛绑定的域名不能为空", groups = QiniuGroup.class)
|
||||
@URL(message = "七牛绑定的域名格式不正确", groups = QiniuGroup.class)
|
||||
private String qiniuDomain;
|
||||
//七牛路径前缀
|
||||
private String qiniuPrefix;
|
||||
//七牛ACCESS_KEY
|
||||
@NotBlank(message="七牛AccessKey不能为空", groups = QiniuGroup.class)
|
||||
private String qiniuAccessKey;
|
||||
//七牛SECRET_KEY
|
||||
@NotBlank(message="七牛SecretKey不能为空", groups = QiniuGroup.class)
|
||||
private String qiniuSecretKey;
|
||||
//七牛存储空间名
|
||||
@NotBlank(message="七牛空间名不能为空", groups = QiniuGroup.class)
|
||||
private String qiniuBucketName;
|
||||
|
||||
//阿里云绑定的域名
|
||||
@NotBlank(message="阿里云绑定的域名不能为空", groups = AliyunGroup.class)
|
||||
@URL(message = "阿里云绑定的域名格式不正确", groups = AliyunGroup.class)
|
||||
private String aliyunDomain;
|
||||
//阿里云路径前缀
|
||||
private String aliyunPrefix;
|
||||
//阿里云EndPoint
|
||||
@NotBlank(message="阿里云EndPoint不能为空", groups = AliyunGroup.class)
|
||||
private String aliyunEndPoint;
|
||||
//阿里云AccessKeyId
|
||||
@NotBlank(message="阿里云AccessKeyId不能为空", groups = AliyunGroup.class)
|
||||
private String aliyunAccessKeyId;
|
||||
//阿里云AccessKeySecret
|
||||
@NotBlank(message="阿里云AccessKeySecret不能为空", groups = AliyunGroup.class)
|
||||
private String aliyunAccessKeySecret;
|
||||
//阿里云BucketName
|
||||
@NotBlank(message="阿里云BucketName不能为空", groups = AliyunGroup.class)
|
||||
private String aliyunBucketName;
|
||||
|
||||
//腾讯云绑定的域名
|
||||
@NotBlank(message="腾讯云绑定的域名不能为空", groups = QcloudGroup.class)
|
||||
@URL(message = "腾讯云绑定的域名格式不正确", groups = QcloudGroup.class)
|
||||
private String qcloudDomain;
|
||||
//腾讯云路径前缀
|
||||
private String qcloudPrefix;
|
||||
//腾讯云AppId
|
||||
@NotNull(message="腾讯云AppId不能为空", groups = QcloudGroup.class)
|
||||
private Integer qcloudAppId;
|
||||
//腾讯云SecretId
|
||||
@NotBlank(message="腾讯云SecretId不能为空", groups = QcloudGroup.class)
|
||||
private String qcloudSecretId;
|
||||
//腾讯云SecretKey
|
||||
@NotBlank(message="腾讯云SecretKey不能为空", groups = QcloudGroup.class)
|
||||
private String qcloudSecretKey;
|
||||
//腾讯云BucketName
|
||||
@NotBlank(message="腾讯云BucketName不能为空", groups = QcloudGroup.class)
|
||||
private String qcloudBucketName;
|
||||
//腾讯云COS所属地区
|
||||
@NotBlank(message="所属地区不能为空", groups = QcloudGroup.class)
|
||||
private String qcloudRegion;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
import com.peanut.common.utils.DateUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 云存储(支持七牛、阿里云、腾讯云、又拍云)
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public abstract class CloudStorageService {
|
||||
/** 云存储配置信息 */
|
||||
CloudStorageConfig config;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
public String getPath(String prefix, String suffix) {
|
||||
//生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
//文件路径
|
||||
String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;
|
||||
|
||||
if(StringUtils.isNotBlank(prefix)){
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
|
||||
return path + suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param data 文件字节数组
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String upload(byte[] data, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param data 文件字节数组
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String uploadSuffix(byte[] data, String suffix);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param inputStream 字节流
|
||||
* @param path 文件路径,包含文件名
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String upload(InputStream inputStream, String path);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param inputStream 字节流
|
||||
* @param suffix 后缀
|
||||
* @return 返回http地址
|
||||
*/
|
||||
public abstract String uploadSuffix(InputStream inputStream, String suffix);
|
||||
|
||||
}
|
||||
44
src/main/java/com/peanut/modules/oss/cloud/OSSFactory.java
Normal file
44
src/main/java/com/peanut/modules/oss/cloud/OSSFactory.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
|
||||
import com.peanut.common.utils.ConfigConstant;
|
||||
import com.peanut.common.utils.Constant;
|
||||
import com.peanut.common.utils.SpringContextUtils;
|
||||
import com.peanut.modules.sys.service.SysConfigService;
|
||||
|
||||
/**
|
||||
* 文件上传Factory
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public final class OSSFactory {
|
||||
private static SysConfigService sysConfigService;
|
||||
|
||||
static {
|
||||
OSSFactory.sysConfigService = (SysConfigService) SpringContextUtils.getBean("sysConfigService");
|
||||
}
|
||||
|
||||
public static CloudStorageService build(){
|
||||
//获取云存储配置信息
|
||||
CloudStorageConfig config = sysConfigService.getConfigObject(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);
|
||||
|
||||
if(config.getType() == Constant.CloudService.QINIU.getValue()){
|
||||
return new QiniuCloudStorageService(config);
|
||||
}else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
|
||||
return new AliyunCloudStorageService(config);
|
||||
}else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
|
||||
return new QcloudCloudStorageService(config);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.peanut.common.exception.RRException;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.request.UploadFileRequest;
|
||||
import com.qcloud.cos.sign.Credentials;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public class QcloudCloudStorageService extends CloudStorageService {
|
||||
private COSClient client;
|
||||
|
||||
public QcloudCloudStorageService(CloudStorageConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
|
||||
config.getQcloudSecretKey());
|
||||
|
||||
//初始化客户端配置
|
||||
ClientConfig clientConfig = new ClientConfig();
|
||||
//设置bucket所在的区域,华南:gz 华北:tj 华东:sh
|
||||
clientConfig.setRegion(config.getQcloudRegion());
|
||||
|
||||
client = new COSClient(clientConfig, credentials);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
//腾讯云必需要以"/"开头
|
||||
if(!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
|
||||
//上传到腾讯云
|
||||
UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
|
||||
String response = client.uploadFile(request);
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
if(jsonObject.getInteger("code") != 0) {
|
||||
throw new RRException("文件上传失败," + jsonObject.getString("message"));
|
||||
}
|
||||
|
||||
return config.getQcloudDomain() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
byte[] data = IOUtils.toByteArray(inputStream);
|
||||
return this.upload(data, path);
|
||||
} catch (IOException e) {
|
||||
throw new RRException("上传文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getQcloudPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.cloud;
|
||||
|
||||
import com.peanut.common.exception.RRException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 七牛云存储
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public class QiniuCloudStorageService extends CloudStorageService {
|
||||
private UploadManager uploadManager;
|
||||
private String token;
|
||||
|
||||
public QiniuCloudStorageService(CloudStorageConfig config){
|
||||
this.config = config;
|
||||
|
||||
//初始化
|
||||
init();
|
||||
}
|
||||
|
||||
private void init(){
|
||||
uploadManager = new UploadManager(new Configuration(Zone.autoZone()));
|
||||
token = Auth.create(config.getQiniuAccessKey(), config.getQiniuSecretKey()).
|
||||
uploadToken(config.getQiniuBucketName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String path) {
|
||||
try {
|
||||
Response res = uploadManager.put(data, path, token);
|
||||
if (!res.isOK()) {
|
||||
throw new RuntimeException("上传七牛出错:" + res.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RRException("上传文件失败,请核对七牛配置信息", e);
|
||||
}
|
||||
|
||||
return config.getQiniuDomain() + "/" + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(InputStream inputStream, String path) {
|
||||
try {
|
||||
byte[] data = IOUtils.toByteArray(inputStream);
|
||||
return this.upload(data, path);
|
||||
} catch (IOException e) {
|
||||
throw new RRException("上传文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(byte[] data, String suffix) {
|
||||
return upload(data, getPath(config.getQiniuPrefix(), suffix));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadSuffix(InputStream inputStream, String suffix) {
|
||||
return upload(inputStream, getPath(config.getQiniuPrefix(), suffix));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.peanut.modules.oss.controller;
|
||||
|
||||
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.modules.oss.service.OssService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/oss/fileoss")
|
||||
@CrossOrigin
|
||||
@Api("oss")
|
||||
public class OssController {
|
||||
|
||||
@Autowired
|
||||
private OssService ossService;
|
||||
|
||||
//上传头像的方法
|
||||
@PostMapping
|
||||
@ApiOperation("上传")
|
||||
public R uploadOssFile(MultipartFile file) {
|
||||
//获取上传文件 MultipartFile
|
||||
//返回上传到oss的路径
|
||||
String url = ossService.uploadFileAvatar(file);
|
||||
return R.ok().put("url",url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.peanut.common.exception.RRException;
|
||||
import com.peanut.common.utils.ConfigConstant;
|
||||
import com.peanut.common.utils.Constant;
|
||||
import com.peanut.common.utils.PageUtils;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.common.validator.ValidatorUtils;
|
||||
import com.peanut.modules.oss.cloud.CloudStorageConfig;
|
||||
import com.peanut.modules.oss.cloud.OSSFactory;
|
||||
import com.peanut.modules.oss.entity.SysOssEntity;
|
||||
import com.peanut.modules.oss.service.SysOssService;
|
||||
import com.peanut.modules.sys.service.SysConfigService;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("sys/oss")
|
||||
public class SysOssController {
|
||||
@Autowired
|
||||
private SysOssService sysOssService;
|
||||
@Autowired
|
||||
private SysConfigService sysConfigService;
|
||||
|
||||
private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@RequiresPermissions("sys:oss:all")
|
||||
public R list(@RequestParam Map<String, Object> params){
|
||||
PageUtils page = sysOssService.queryPage(params);
|
||||
|
||||
return R.ok().put("page", page);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 云存储配置信息
|
||||
*/
|
||||
@GetMapping("/config")
|
||||
@RequiresPermissions("sys:oss:all")
|
||||
public R config(){
|
||||
CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
|
||||
|
||||
return R.ok().put("config", config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存云存储配置信息
|
||||
*/
|
||||
@PostMapping("/saveConfig")
|
||||
@RequiresPermissions("sys:oss:all")
|
||||
public R saveConfig(@RequestBody CloudStorageConfig config){
|
||||
//校验类型
|
||||
ValidatorUtils.validateEntity(config);
|
||||
ValidatorUtils.validateEntity(config, Constant.CloudService.getByValue(config.getType()));
|
||||
|
||||
sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
@RequiresPermissions("sys:oss:all")
|
||||
public R upload(@RequestParam("file") MultipartFile file) throws Exception {
|
||||
if (file.isEmpty()) {
|
||||
throw new RRException("上传文件不能为空");
|
||||
}
|
||||
|
||||
//上传文件
|
||||
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||
String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
|
||||
|
||||
//保存文件信息
|
||||
SysOssEntity ossEntity = new SysOssEntity();
|
||||
ossEntity.setUrl(url);
|
||||
ossEntity.setCreateDate(new Date());
|
||||
sysOssService.save(ossEntity);
|
||||
|
||||
return R.ok().put("url", url);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
@PostMapping("/delete")
|
||||
@RequiresPermissions("sys:oss:all")
|
||||
public R delete(@RequestBody Long[] ids){
|
||||
sysOssService.removeByIds(Arrays.asList(ids));
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
||||
23
src/main/java/com/peanut/modules/oss/dao/SysOssDao.java
Normal file
23
src/main/java/com/peanut/modules/oss/dao/SysOssDao.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.peanut.modules.oss.entity.SysOssEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysOssDao extends BaseMapper<SysOssEntity> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_oss")
|
||||
public class SysOssEntity implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId
|
||||
private Long id;
|
||||
//URL地址
|
||||
private String url;
|
||||
//创建时间
|
||||
private Date createDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.peanut.modules.oss.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface OssService {
|
||||
//上传头像到oss
|
||||
String uploadFileAvatar(MultipartFile file);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.peanut.common.utils.PageUtils;
|
||||
import com.peanut.modules.oss.entity.SysOssEntity;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
public interface SysOssService extends IService<SysOssEntity> {
|
||||
|
||||
PageUtils queryPage(Map<String, Object> params);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.peanut.modules.oss.service.impl;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.peanut.common.utils.ConstantPropertiesUtils;
|
||||
import com.peanut.modules.oss.service.OssService;
|
||||
import org.joda.time.DateTime;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class OssServiceImpl implements OssService {
|
||||
|
||||
//上传头像到oss
|
||||
@Override
|
||||
public String uploadFileAvatar(MultipartFile file) {
|
||||
// 工具类获取值
|
||||
String endpoint = ConstantPropertiesUtils.END_POIND;
|
||||
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
|
||||
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
|
||||
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
|
||||
|
||||
try {
|
||||
// 创建OSS实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
//获取上传文件输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
//获取文件名称
|
||||
String fileName = file.getOriginalFilename();
|
||||
|
||||
//1 在文件名称里面添加随机唯一的值
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||
// yuy76t5rew01.jpg
|
||||
fileName = uuid+fileName;
|
||||
|
||||
//2 把文件按照日期进行分类
|
||||
//获取当前日期
|
||||
// 2019/11/12
|
||||
String datePath = new DateTime().toString("yyyy/MM/dd");
|
||||
//拼接
|
||||
// 2019/11/12/ewtqr313401.jpg
|
||||
fileName = datePath+"/"+fileName;
|
||||
|
||||
//调用oss方法实现上传
|
||||
//第一个参数 Bucket名称
|
||||
//第二个参数 上传到oss文件路径和文件名称 aa/bb/1.jpg
|
||||
//第三个参数 上传文件输入流
|
||||
ossClient.putObject(bucketName,fileName , inputStream);
|
||||
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
|
||||
//把上传之后文件路径返回
|
||||
//需要把上传到阿里云oss路径手动拼接出来
|
||||
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
|
||||
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
|
||||
return url;
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.modules.oss.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.peanut.common.utils.PageUtils;
|
||||
import com.peanut.common.utils.Query;
|
||||
import com.peanut.modules.oss.dao.SysOssDao;
|
||||
import com.peanut.modules.oss.entity.SysOssEntity;
|
||||
import com.peanut.modules.oss.service.SysOssService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Service("sysOssService")
|
||||
public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> implements SysOssService {
|
||||
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
IPage<SysOssEntity> page = this.page(
|
||||
new Query<SysOssEntity>().getPage(params)
|
||||
);
|
||||
|
||||
return new PageUtils(page);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user