Browse Source

重构国际化,网关开发,鉴权开发。

master
caojianbin 8 months ago
parent
commit
cd36eec271
  1. 21
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/annotation/SystemLog.java
  2. 109
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/aspect/SystemLogAspect.java
  3. 27
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysRoleMenu.java
  4. 129
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysUser.java
  5. 28
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysUserRole.java
  6. 195
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/User.java
  7. 89
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/UserDeviceBindApp.java
  8. 39
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/VersionRatio.java
  9. 29
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/VersionRatioHistory.java
  10. 244
      ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/utlis/UploadUtil.java

21
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/annotation/SystemLog.java

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
package com.ecell.internationalize.common.sysytem.annotation;
import java.lang.annotation.*;
/**
* @author borui
*/
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
/**
* 功能模块信息
*/
String msg() default "";
/**
* 操作描述
*/
String operation() default "";
}

109
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/aspect/SystemLogAspect.java

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
package com.ecell.internationalize.common.sysytem.aspect;
import com.ecell.internationalize.common.core.utils.ServletUtils;
import com.ecell.internationalize.common.core.utils.StringUtils;
import com.ecell.internationalize.common.core.utils.ip.IpUtils;
import com.ecell.internationalize.common.core.utils.locale.LocaleUtil;
import com.ecell.internationalize.common.core.utils.uuid.UUID;
import com.ecell.internationalize.common.security.utils.SecurityUtils;
import com.ecell.internationalize.common.sysytem.annotation.SystemLog;
import com.ecell.internationalize.common.sysytem.constant.FieldConstant;
import com.ecell.internationalize.common.sysytem.entity.SysOperateInfo;
import com.ecell.internationalize.common.sysytem.feign.SysOperatorInfoFeignClient;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
/**
* @author borui
*/
@Aspect
@Component
public class SystemLogAspect {
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);
@Autowired
private SysOperatorInfoFeignClient sysOperatorInfoFeignClient;
/**
* 处理完请求后执行
* @param joinPoint 切点
*/
@AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, SystemLog controllerLog, Map<String,Object> jsonResult) {
handleLog(joinPoint, controllerLog, null, jsonResult);
}
/**
* 拦截异常操作
*
* @param joinPoint 切点
* @param e 异常
*/
@AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, SystemLog controllerLog, Exception e) {
handleLog(joinPoint, controllerLog, e, null);
}
protected void handleLog(final JoinPoint joinPoint, SystemLog controllerLog, final Exception e, Map<String,Object> jsonResult) {
try {
// ========数据库日志=========*/
SysOperateInfo sysOperateInfo = new SysOperateInfo();
String id= UUID.randomUUID().toString(true);
sysOperateInfo.setId(id);
String username = SecurityUtils.getUsername();
if (StringUtils.isNotBlank(username)) {
sysOperateInfo.setAccount(username);
sysOperateInfo.setCreateUser(username);
}
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
System.out.println("获取到的操作机器的IP:"+ip);
sysOperateInfo.setLoginIp(ip);
sysOperateInfo.setAccessTime(new Date());
sysOperateInfo.setStatus(FieldConstant.OPERATOR_SUCCESS);
if (e != null) {
sysOperateInfo.setErrorMsg(LocaleUtil.getMessage("messages.system.error"));
sysOperateInfo.setStatus(FieldConstant.OPERATOR_FAIL);
}
else {
if (FieldConstant.CODE_FIVE == Integer.parseInt(jsonResult.get(FieldConstant.CODE).toString())){
//操作失败,记录原因
sysOperateInfo.setErrorMsg(jsonResult.get(FieldConstant.MSG).toString());
sysOperateInfo.setStatus(FieldConstant.OPERATOR_FAIL);
}
}
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, sysOperateInfo, jsonResult);
// 保存数据库
sysOperatorInfoFeignClient.save(sysOperateInfo);
}
catch (Exception exp) {
// 记录本地异常日志
logger.error("==前置通知异常==");
logger.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
* @param systemLog 日志
* @param sysOperateInfo 操作日志
* @throws Exception
*/
public void getControllerMethodDescription(JoinPoint joinPoint, SystemLog systemLog, SysOperateInfo sysOperateInfo, Map<String,Object> jsonResult) {
// 设置action动作
sysOperateInfo.setOperationModule(LocaleUtil.getMessage(systemLog.operation()));
// 设置标题
sysOperateInfo.setMsg(LocaleUtil.getMessage(systemLog.msg()));
}
}

27
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysRoleMenu.java

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
package com.ecell.internationalize.common.sysytem.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SysRoleMenu implements Serializable {
private static final long serialVersionUID=1L;
/**
* 角色ID
*/
private String roleId;
/**
* 菜单ID
*/
private String menuId;
}

129
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysUser.java

@ -0,0 +1,129 @@ @@ -0,0 +1,129 @@
package com.ecell.internationalize.common.sysytem.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user")
public class SysUser implements Serializable {
private static final long serialVersionUID=1L;
/**
* 主键Id
*/
@TableId("user_id")
private String userId;
/**
* 用户昵称
*/
private String nickName;
/**
* 用户账号
*/
private String account;
/**
* 用户类型
*/
private String userType;
/**
* 用户头像
*/
private String headImg;
/**
* 密码
*/
private String password;
/**
* 新密码
*/
@TableField(exist = false)
private String newPassword;
/**
* 帐号状态0正常 1停用
*/
private String status;
/**
* 删除标识0已删除1正常
*/
private String delFlag;
/**
* 登录IP
*/
private String loginIp;
/**
* 最后登录时间
*/
private String loginDate;
/**
* 创建人
*/
private String createUser;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 修改人
*/
private String updateUser;
/**
* 修改时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 厂商Id
*/
private String firmId;
/**
* 代理商Id
*/
private String secondFirmId;
/**
* 代理商名称
*/
@TableField(exist = false)
private String secondFirmName;
/**
* 标识(1:厂商2代理商)
*/
private String firmFlag;
/**
* 厂商名称
*/
@TableField(exist = false)
private String firmName;
/** 角色 */
@TableField(exist = false)
private String roleId;
@TableField(exist = false)
private String roleName;
}

28
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/SysUserRole.java

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
package com.ecell.internationalize.common.sysytem.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SysUserRole implements Serializable {
private static final long serialVersionUID=1L;
/**
* 用户ID
*/
private String userId;
/**
* 角色ID
*/
private String roleId;
}

195
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/User.java

@ -0,0 +1,195 @@ @@ -0,0 +1,195 @@
package com.ecell.internationalize.common.sysytem.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="user对象",description="用户对象user")
public class User implements Serializable {
private static final long serialVersionUID=1L;
/**
* 主键Id
*/
@TableId("user_id")
private String userId;
/**
* accessToken
*/
private String accessToken;
/**
* accessToken创建时的时间戳
*/
private Date accessTokenTime;
/**
* 密码
*/
private String password;
/**
* 名字
*/
@ApiModelProperty(example = "姓名")
private String name;
/**
* 性别
*/
private String sex;
/**
* 手机号
*/
@ApiModelProperty(example = "手机号")
private String phone;
/**
* 监听腕表功能用的手机号
*/
private String monitorWatchPhone;
/**
* 头像文件名 xxx.jpg
*/
@ApiModelProperty(example = "头像")
private String image;
/**
* 是否接收其他人聊天群消息(0接收 1不接收)
*/
private String chatFlag;
/**
* 聊天群组id的未读信息个数有多个聊天群就有多条字段
*/
private String groupId;
/**
* 总未读信息个数
*/
private Integer unreadMsgTotal;
/**
* 用户删除消息中心时间戳
*/
private Date delMsgTime;
/**
* 用户删除历史轨迹时间戳
*/
private Date delLocationTime;
/**
* 用户删除话费流量消息时间戳
*/
private Date delCostTime;
/**
* 所属国家
*/
private String country;
/**
* 用户状态
*/
private String status;
/**
* 国家编码
*/
private String countryCode;
/**
* 用户最近一次使用时间
*/
private Date lastUseTime;
/**
* 用户注册时间
*/
private Date registerTime;
/**
* 用户最后一次登录时间
*/
private Date lastLoginTime;
/**
* 登录机型[iosandroid]
*/
private String mobileType;
/**
* 用户使用什么渠道APP登录的
*/
private String channel;
/**
* 创建人
*/
private String createUser;
/**
* 创建时间
*/
@TableField(value = "create_time",fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 修改人
*/
private String updateUser;
/**
* 修改时间
*/
@TableField(value = "update_time",fill = FieldFill.UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 设备imei
*/
@TableField(exist = false)
private String imei;
/**
* 用户邮箱
*/
private String userEmail;
/**
* APP相关信息
*/
private String userAgent;
@TableField(exist = false)
private String token;
private String completeUserAgent;
@TableField(exist = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date userLastTime;
@TableField(exist = false)
private String channelName;
@TableField(exist = false)
private String version;
@TableField(exist = false)
private String relation;
}

89
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/UserDeviceBindApp.java

@ -0,0 +1,89 @@ @@ -0,0 +1,89 @@
package com.ecell.internationalize.common.sysytem.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("user_device_bind_app")
public class UserDeviceBindApp implements Serializable {
private static final long serialVersionUID=1L;
/**
* 主键Id
*/
private String id;
/**
* 用户Id
*/
private String userId;
/**
* 设备imei
*/
private String imei;
/**
* 用户身份默认0游客1普通用户(申请关注的人),2:管理员
*/
private String identity;
/**
* 与孩子关系
*/
private String relation;
/**
* 与孩子关系头像文件名ID xxx.jpg
*/
private String relationImageId;
/**
* 创建人
*/
private String createUser;
/**
* 创建时间
*/
@TableField(value = "create_time",fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 修改人
*/
private String updateUser;
/**
* 修改时间
*/
@TableField(value = "update_time",fill = FieldFill.UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 是否绑定 0未绑定1绑定
*/
private String bindStatus;
/**
* 是否审批(0:待审批 ,1已审批,2:已拒绝)
*/
private String approveStatus;
/**
* 用户邮箱
*/
private String userEmail;
private String phone;
}

39
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/VersionRatio.java

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
package com.ecell.internationalize.common.sysytem.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="VersionRatio对象", description="用户版本统计")
public class VersionRatio implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty(value = "主键Id")
private String id;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "渠道")
private String channelName;
@ApiModelProperty(value = "版本号")
private String version;
@ApiModelProperty(value = "用户id")
private String userId;
private Integer timestamp;
}

29
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/entity/VersionRatioHistory.java

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
package com.ecell.internationalize.common.sysytem.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @author borui
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="VersionRatioHistory对象", description="历史活跃数")
public class VersionRatioHistory implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty(value = "主键Id")
private String id;
@ApiModelProperty(value = "活跃时间")
private String historyTime;
@ApiModelProperty(value = "每天活跃数")
private Integer historyTotal;
}

244
ecell-internationalize/ecell-internationalize-common/ecell-internationalize-common-system/src/main/java/com/ecell/internationalize/common/sysytem/utlis/UploadUtil.java

@ -0,0 +1,244 @@ @@ -0,0 +1,244 @@
package com.ecell.internationalize.common.sysytem.utlis;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.ecell.internationalize.common.core.exception.ServiceException;
import com.ecell.internationalize.common.core.utils.uuid.UUID;
import com.ecell.internationalize.common.sysytem.constant.OssClientConstants;
import com.ecell.internationalize.common.sysytem.entity.ChatOssDto;
import com.ecell.internationalize.common.sysytem.entity.CheckVersionUpload;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* @author borui
*/
public class UploadUtil {
public static String upload(MultipartFile file) throws IOException {
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(OssClientConstants.ENDPOINT, OssClientConstants.ACCESS_KEY_ID, OssClientConstants.ACCESS_KEY_SECRET);
// 上传文件流。
InputStream inputStream = null;
// 获取文件后缀名
String suffix =getSuffix(file);
//唯一key
String uuid= UUID.fastUUID().toString();
File tempFile = File.createTempFile(uuid, suffix);
//MultipartFile 转换为 File
file.transferTo(tempFile);
try {
//文件地址
inputStream = new FileInputStream(tempFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//上传到OSS,第一个参数为存储空间名,第二个为对象名,唯一值
ossClient.putObject(OssClientConstants.BUCKET_NAME, uuid+"."+suffix, inputStream);
// 设置URL过期时间为100年,默认这里是int型,转换为long型即可
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
// 生成URL
URL url = ossClient.generatePresignedUrl(OssClientConstants.BUCKET_NAME, uuid+"."+suffix, expiration);
System.out.println("url:"+url);
return url.toString();
// 关闭OSSClient。
// ossClient.shutdown();
}
public static ChatOssDto uploadChat(MultipartFile file) throws IOException {
ChatOssDto chatOssDto=new ChatOssDto();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(OssClientConstants.ENDPOINT, OssClientConstants.ACCESS_KEY_ID, OssClientConstants.ACCESS_KEY_SECRET);
// 上传文件流。
InputStream inputStream = null;
// 获取文件后缀名
// String suffix =getSuffix(file);
String suffix ="amr";
System.out.println("语音文件名后缀:"+suffix);
String fileName=file.getOriginalFilename()+"."+suffix;
System.out.println("fileName:"+fileName);
//唯一key
String uuid= UUID.fastUUID().toString();
File tempFile = File.createTempFile(uuid, suffix);
//MultipartFile 转换为 File
file.transferTo(tempFile);
try {
//文件地址
inputStream = new FileInputStream(tempFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//String fileName="static/message/"+uuid+"."+suffix;
//上传到OSS,第一个参数为存储空间名,第二个为对象名,唯一值
ossClient.putObject(OssClientConstants.BUCKET_NAME, fileName, inputStream);
// 设置URL过期时间为100年,默认这里是int型,转换为long型即可
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
// 生成URL
URL url = ossClient.generatePresignedUrl(OssClientConstants.BUCKET_NAME, fileName, expiration);
chatOssDto.setUrl(url.toString());
chatOssDto.setFileName(fileName);
System.out.println("chatOssDto:"+chatOssDto);
return chatOssDto;
// 关闭OSSClient。
// ossClient.shutdown();
}
public static CheckVersionUpload versionUpload(MultipartFile file) throws IOException {
System.out.println("开始文件上传");
CheckVersionUpload checkVersionUpload=new CheckVersionUpload();
String fileName=file.getOriginalFilename();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(OssClientConstants.ENDPOINT, OssClientConstants.ACCESS_KEY_ID, OssClientConstants.ACCESS_KEY_SECRET);
// 上传文件流。
InputStream inputStream = null;
// 获取文件后缀名
String suffix =getSuffix(file);
//唯一key
String uuid= UUID.fastUUID().toString();
File tempFile = File.createTempFile(uuid, suffix);
//MultipartFile 转换为 File
file.transferTo(tempFile);
try {
//文件地址
inputStream = new FileInputStream(tempFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//String fileName="static/message/"+uuid+"."+suffix;
//上传到OSS,第一个参数为存储空间名,第二个为对象名,唯一值
ossClient.putObject(OssClientConstants.BUCKET_NAME, fileName, inputStream);
// 设置URL过期时间为100年,默认这里是int型,转换为long型即可
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
// 生成URL
URL url = ossClient.generatePresignedUrl(OssClientConstants.BUCKET_NAME, fileName, expiration);
checkVersionUpload.setUrl(url.toString());
checkVersionUpload.setFileName(fileName);
checkVersionUpload.setGetSuffix(suffix);
return checkVersionUpload;
// 关闭OSSClient。
// ossClient.shutdown();
}
public static String uploadTwo(MultipartFile file,String inputBatch) throws IOException {
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(OssClientConstants.ENDPOINT, OssClientConstants.ACCESS_KEY_ID, OssClientConstants.ACCESS_KEY_SECRET);
// 上传文件流。
InputStream inputStream = null;
// 获取文件后缀名
String suffix =getSuffix(file);
//唯一key
//String uuid= UUID.fastUUID().toString();
File tempFile = File.createTempFile(inputBatch, suffix);
//MultipartFile 转换为 File
file.transferTo(tempFile);
URL url =null;
try {
//文件地址
inputStream = new FileInputStream(tempFile);
String fileName="device/"+inputBatch+"."+suffix;
PutObjectRequest putObjectRequest = new PutObjectRequest(OssClientConstants.BUCKET_NAME, fileName, inputStream);
//上传到OSS,第一个参数为存储空间名,第二个为对象名,唯一值
ossClient.putObject(putObjectRequest);
// 设置URL过期时间为100年,默认这里是int型,转换为long型即可
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
// 生成URL
url= ossClient.generatePresignedUrl(OssClientConstants.BUCKET_NAME, fileName, expiration);
System.out.println("url:"+url);
} catch (FileNotFoundException e) {
throw new ServiceException(e.getMessage());
}finally {
ossClient.shutdown();
}
return url.toString();
// 关闭OSSClient。
// ossClient.shutdown();
}
/**
* 上传excel
*
* @param workbook
* @param excelName 文件名
* @return
*/
public String uploadThree(Workbook workbook, String excelName) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
workbook.write(bos);
} catch (IOException e) {
} finally {
try {
bos.close();
} catch (IOException e) {
}
}
byte[] brray = bos.toByteArray();
InputStream is = new ByteArrayInputStream(brray);
OSS ossClient = new OSSClientBuilder().build(OssClientConstants.ENDPOINT, OssClientConstants.ACCESS_KEY_ID, OssClientConstants.ACCESS_KEY_SECRET);
try {
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(OssClientConstants.BUCKET_NAME, excelName, is);
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件
ossClient.putObject(putObjectRequest);
//设置过期时间
LocalDateTime time = LocalDateTime.now().plusDays(36500);
Date expiration = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
return ossClient.generatePresignedUrl(OssClientConstants.BUCKET_NAME, "device" + "/" + excelName, expiration).toString();
} catch (OSSException oe) {
throw new ServiceException(oe.getMessage());
} catch (ClientException ce) {
throw new ServiceException(ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 获取文件名后缀
* @Title: UploadFileUtil
* @Author: liy
* @Date: 2022/7/13 17:35
* @Description:
* @Version:1.0
*/
public static String getSuffix(MultipartFile file){
String originalFilename = file.getOriginalFilename();
assert originalFilename != null;
return originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
}
/**
* 获取文件名称
* @param file
* @param inputBatch
* @return
*/
public static String getMultipartFileName(MultipartFile file,String inputBatch){
String originalFilename = file.getOriginalFilename();
assert originalFilename != null;
String s= originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
String fileName="device"+inputBatch+"."+s;
return fileName;
}
}
Loading…
Cancel
Save