Commit aa707d9c authored by fangxinjiang's avatar fangxinjiang

新增档案服务

parent 2601bd81
package com.yifu.cloud.plus.v1.yifu.archives.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
/**
* 附件信息表
*
* @author pwang
* @date 2021-02-19 16:16:07
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_atta_info")
@Tag(name = "附件信息表")
public class TAttaInfo extends Model<TAttaInfo> {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(type = IdType.ASSIGN_ID)
@Schema(description = "主键")
private String id;
/**
* 资源名称
*/
@NotBlank(message = "资源名称不能为空")
@Length(max = 50, message = "资源名称不能超过50个字符")
@Schema(description = "资源名称")
private String attaName;
/**
* 资源地址
*/
@NotBlank(message = "资源地址不能为空")
@Length(max = 100, message = "资源地址不能超过100个字符")
@Schema(description = "资源地址")
private String attaSrc;
/**
* 资源大小
*/
@Schema(description = "资源大小")
private Long attaSize;
/**
* 资源类型
*/
@Length(max = 10, message = "资源类型不能超过10个字符")
@Schema(description = "资源类型")
private String attaType;
/**
* 创建人
*/
@Length(max = 32, message = "创建人不能超过32个字符")
@Schema(description = "创建人")
private String createUser;
/**
* 创建时间
*/
@Schema(description = "创建时间")
private LocalDateTime createTime;
/**
* 关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单)
*/
@NotBlank(message = "关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单)不能为空")
@Length(max = 2, message = "关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单)不能超过2个字符")
@Schema(description = "关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单)")
private String relationType;
/**
* 实体id和关系类型共同确定附件所属
*/
@NotBlank(message = "实体id和关系类型共同确定附件所属不能为空")
@Length(max = 32, message = "实体id和关系类型共同确定附件所属不能超过32个字符")
@Schema(description = "实体id和关系类型共同确定附件所属")
private String domainId;
}
package com.yifu.cloud.plus.v1.yifu.yifu.base.feign;
\ No newline at end of file
package com.yifu.cloud.plus.v1.yifu.yifu.base;
package com.yifu.cloud.plus.v1.yifu.archives;
import com.yifu.cloud.plus.v1.yifu.common.security.annotation.EnableYifuResourceServer;
import org.springframework.boot.SpringApplication;
......
package com.yifu.cloud.plus.v1.yifu.archives.controller;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
/**
* @author fang
* 文件上传接口
*/
@RestController
@RequestMapping("/fileUpload")
@Tag(name = "文件上传接口")
@Slf4j
public class FileUploadController {
@Autowired
private OSSUtil ossUtil;
@Autowired
private TAttaInfoService tAttaInfoService;
/**
* @Author fxj
* @Description
* @Date 13:49 2022/6/17
* @Param
* @return
**/
@Operation(description = "OSS文件上传接口")
/*@ApiImplicitParams({
@ApiImplicitParam(name = "file", value = "Form文件上传", required = true, dataType = "__file", paramType = "form"),
@ApiImplicitParam(name = "filePath", value = "文件上传路径", required = false, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "type", value = "关系类型(0客户附件1联系人附件2跟进记录附件3合同附件4合同归档附件5报价单6招标信息7业务评估)", required = true, dataType = "int", paramType = "form"),
@ApiImplicitParam(name = "domain", value = "实体id(传入就插入对应关系,用于编辑)", required = false, paramType = "form")
})*/
@PostMapping(value = "/ossUploadFile")
public R<FileVo> uploadImg(@RequestBody MultipartFile file, String filePath, Integer type, String domain) throws IOException {
if (null == file) {
return R.failed("文件删除异常,请重新上传!");
}
String fileName = file.getOriginalFilename();
if (Common.isEmpty(fileName)) {
return R.failed("文件名不能为空!");
}
if (fileName.length() > 100) {
return R.failed("文件名不能超过100个字符!");
}
//filePath不传默认存储空间的根目录
//jpg,jpeg,png,bmp
String key = "";
if (Common.isNotNull(filePath)) {
key = filePath + "/" + System.currentTimeMillis() + fileName;
} else {
key = System.currentTimeMillis() + fileName;
}
if (!Common.checkFile(key)) {
return R.failed("非法上传类型!");
}
boolean flag = ossUtil.uploadFileByStream(file.getInputStream(), key, null);
FileVo fileVo;
TAttaInfo attaInfo;
URL url;
if (flag) {
log.info("文件:" + fileName + "上传至存储空间" + ossUtil.getBucketName() + "成功!");
attaInfo = initUnitAttaForInsert(fileName, key, file.getSize());
attaInfo.setRelationType(String.valueOf(type));
try {
attaInfo = tAttaInfoService.add(attaInfo);
} catch (Exception e) {
log.error("OSS文件上传接口异常:" + e.getMessage());
ossUtil.deleteObject(null, key);
return R.failed("fail:" + e.getMessage());
}
//有实体id则插入关系,用于编辑
if (Common.isNotNull(domain)) {
attaInfo.setDomainId(domain);
url = ossUtil.getObjectUrl(null, attaInfo.getAttaSrc());
fileVo = new FileVo(attaInfo.getId(), attaInfo.getAttaName(), url.toString());
return R.ok(fileVo, "success");
}
url = ossUtil.getObjectUrl(null, attaInfo.getAttaSrc());
fileVo = new FileVo(attaInfo.getId(), attaInfo.getAttaName(), url.toString());
return R.ok(fileVo, "success");
} else {
return R.failed("fail:上传失败");
}
}
/**
* @Author fxj
* @Description
* @Date 13:51 2022/6/17
* @Param
* @return
**/
@Operation(description = "附件删除")
@SysLog("附件删除")
@DeleteMapping("/ossFileDelete/{id}")
public R<Boolean> ossFileDelete(@PathVariable String id) {
try {
TAttaInfo attaInfo = tAttaInfoService.getById(id);
if (null == attaInfo) {
return R.failed("无对应附件信息");
}
if (tAttaInfoService.removeById(id)) {
ossUtil.deleteObject(null, attaInfo.getAttaSrc());
return R.ok(null,"删除成功!");
}
} catch (Exception e) {
log.error("附件删除异常:" + e.getMessage());
return R.failed("附件删除异常!");
}
return R.ok(null,"删除失败!");
}
/**
* 对象初始化
*
* @param fileName
* @param key
* @param fileSize
* @return
*/
private TAttaInfo initUnitAttaForInsert(String fileName, String key, long fileSize) {
TAttaInfo unitAtta = new TAttaInfo();
char slash = '/';
char point = '.';
if (fileName.lastIndexOf(slash) >= 0) {
unitAtta.setAttaName(fileName.substring(fileName.lastIndexOf(slash)));
} else {
unitAtta.setAttaName(fileName);
}
unitAtta.setAttaSize(fileSize);
unitAtta.setAttaSrc(key);
if (key.lastIndexOf(point) >= 0) {
unitAtta.setAttaType(key.substring(key.lastIndexOf(point)));
}
unitAtta.setCreateTime(LocalDateTime.now());
YifuUser user = SecurityUtils.getUser();
unitAtta.setCreateUser(null == user ? "" : user.getId().toString());
return unitAtta;
}
}
package com.yifu.cloud.plus.v1.yifu.archives.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
/**
* 附件信息表
*
* @author pwang
* @date 2021-02-19 16:16:07
*/
@Mapper
public interface TAttaInfoMapper extends BaseMapper<TAttaInfo> {
/**
* 附件信息表简单分页查询
*
* @param tAttaInfo 附件信息表
* @return
*/
IPage<TAttaInfo> getTAttaInfoPage(Page<TAttaInfo> page, @Param("tAttaInfo") TAttaInfo tAttaInfo);
/**
* @param tAttaInfo
* @Description: 获取list
* @Author: hgw
* @Date: 2021/5/28 16:02
* @return: java.util.List<com.yifu.cloud.v1.crm.api.entity.TAttaInfo>
**/
List<TAttaInfo> getTAttaInfoList(@Param("tAttaInfo") TAttaInfo tAttaInfo);
List<TAttaInfo> getKeenAtta(@Param("dayStart")LocalDateTime dayStart, @Param("values")Collection<String> values);
}
package com.yifu.cloud.plus.v1.yifu.archives.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
/**
* 附件信息表
*
* @author pwang
* @date 2021-02-19 16:16:07
*/
public interface TAttaInfoService extends IService<TAttaInfo> {
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
IPage<TAttaInfo> getTAttaInfoPage(Page<TAttaInfo> page, TAttaInfo tAttaInfo);
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
List<TAttaInfo> getTAttaInfoList(TAttaInfo tAttaInfo);
TAttaInfo add(TAttaInfo attaInfo);
void update(List<TAttaInfo> attaList, String domainId, String relationType);
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
Boolean batchOssFileDelete(List<TAttaInfo> delAttaList);
List<TAttaInfo> getKeenAtta(LocalDateTime dayStart, Collection<String> values);
}
package com.yifu.cloud.plus.v1.yifu.archives.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TAttaInfoMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
/**
* 附件信息表
*
* @author pwang
* @date 2021-02-19 16:16:07
*/
@Service
public class TAttaInfoServiceImpl extends ServiceImpl<TAttaInfoMapper, TAttaInfo> implements TAttaInfoService {
@Autowired
private OSSUtil ossUtil;
/**
* 附件信息表简单分页查询
*
* @param tAttaInfo 附件信息表
* @return
*/
@Override
public IPage<TAttaInfo> getTAttaInfoPage(Page<TAttaInfo> page, TAttaInfo tAttaInfo) {
return baseMapper.getTAttaInfoPage(page, tAttaInfo);
}
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
@Override
public List<TAttaInfo> getTAttaInfoList(TAttaInfo tAttaInfo) {
return baseMapper.getTAttaInfoList(tAttaInfo);
}
@Override
public TAttaInfo add(TAttaInfo attaInfo) {
this.save(attaInfo);
return attaInfo;
}
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
@Override
public void update(List<TAttaInfo> attaInfoList, String domainId, String relationType) {
if (attaInfoList != null) {
for (TAttaInfo attaInfo : attaInfoList) {
attaInfo.setDomainId(domainId);
attaInfo.setRelationType(relationType);
this.updateById(attaInfo);
}
}
}
/**
* @Author fxj
* @Description
* @Date 13:48 2022/6/17
* @Param
* @return
**/
@Override
public Boolean batchOssFileDelete(List<TAttaInfo> delAttaList) {
try {
for (TAttaInfo delAtta : delAttaList) {
if (this.removeById(delAtta.getId())) {
ossUtil.deleteObject(null, delAtta.getAttaSrc());
}
}
return true;
} catch (Exception e) {
log.error("附件删除异常:" + e.getMessage());
return false;
}
}
@Override
public List<TAttaInfo> getKeenAtta(LocalDateTime dayStart, Collection<String> values) {
return baseMapper.getKeenAtta(dayStart,values);
}
}
package com.yifu.cloud.plus.v1.yifu.yifu.base.controller;
\ No newline at end of file
package com.yifu.cloud.plus.v1.yifu.yifu.base.service;
\ No newline at end of file
......@@ -43,10 +43,10 @@ security:
# 文件上传相关 支持阿里云、华为云、腾讯、minio
oss:
endpoint: http://minio.yifu4cloud.com
accessKey: lengleng
secretKey: lengleng
bucket-name: tmp
endpoint: http://oss-cn-beijing.aliyuncs.com
accessKey: LTAIDh9goA3jgpun
secretKey: eN8EHPAZxglNfcUCMhAT02cy93omLa
bucket-name: yf-hr-static-source
# swagger 配置
#swagger:
# enabled: true
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yifu.cloud.plus.v1.yifu.archives.mapper.TAttaInfoMapper">
<resultMap id="tAttaInfoMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo">
<id property="id" column="id"/>
<result property="attaName" column="atta_name"/>
<result property="attaSrc" column="atta_src"/>
<result property="attaSize" column="atta_size"/>
<result property="attaType" column="atta_type"/>
<result property="createUser" column="create_user"/>
<result property="createTime" column="create_time"/>
<result property="relationType" column="relation_type"/>
<result property="domainId" column="domain_id"/>
</resultMap>
<sql id="Base_Column_List">
a.id,
a.atta_name,
a.atta_src,
a.atta_size,
a.atta_type,
a.create_user,
a.create_time,
a.relation_type,
a.domain_id
</sql>
<sql id="tAttaInfo_where">
<if test="tAttaInfo != null">
<if test="tAttaInfo.id != null and tAttaInfo.id.trim() != ''">
AND a.id = #{tAttaInfo.id}
</if>
<if test="tAttaInfo.attaName != null and tAttaInfo.attaName.trim() != ''">
AND a.atta_name = #{tAttaInfo.attaName}
</if>
<if test="tAttaInfo.attaSrc != null and tAttaInfo.attaSrc.trim() != ''">
AND a.atta_src = #{tAttaInfo.attaSrc}
</if>
<if test="tAttaInfo.attaSize != null">
AND a.atta_size = #{tAttaInfo.attaSize}
</if>
<if test="tAttaInfo.attaType != null and tAttaInfo.attaType.trim() != ''">
AND a.atta_type = #{tAttaInfo.attaType}
</if>
<if test="tAttaInfo.createUser != null and tAttaInfo.createUser.trim() != ''">
AND a.create_user = #{tAttaInfo.createUser}
</if>
<if test="tAttaInfo.createTime != null">
AND a.create_time = #{tAttaInfo.createTime}
</if>
<if test="tAttaInfo.relationType != null and tAttaInfo.relationType.trim() != ''">
AND a.relation_type = #{tAttaInfo.relationType}
</if>
<if test="tAttaInfo.domainId != null and tAttaInfo.domainId.trim() != ''">
AND a.domain_id = #{tAttaInfo.domainId}
</if>
</if>
</sql>
<!--tAttaInfo简单分页查询-->
<select id="getTAttaInfoPage" resultMap="tAttaInfoMap">
SELECT
<include refid="Base_Column_List"/>
FROM t_atta_info a
<where>
1=1
<include refid="tAttaInfo_where"/>
</where>
</select>
<!--tAttaInfo 获取list-->
<select id="getTAttaInfoList" resultMap="tAttaInfoMap">
SELECT
<include refid="Base_Column_List"/>
FROM t_atta_info a
<where>
1=1
<include refid="tAttaInfo_where"/>
</where>
</select>
<select id="getKeenAtta" resultMap="tAttaInfoMap">
SELECT
<include refid="Base_Column_List"/>
FROM t_atta_info a
<where>
1=1
<if test="values != null and values.size() > 0">
and a.domain_id in
<foreach collection="values" item="param" index="index" open="(" close=")" separator=",">
#{param}
</foreach>
</if>
and a.create_time >= #{dayStart}
and a.relation_type in ("3","4")
</where>
</select>
</mapper>
......@@ -72,6 +72,11 @@
<version>2.6</version>
<scope>compile</scope>
</dependency>
<!-- aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
</project>
......@@ -139,4 +139,28 @@ public interface CommonConstants {
* @author fxj
*/
int ONE_INT = 1;
/**
* 个位数字(阿里编码规约不允许直接使用‘魔法值’)
* @Author pwang
* @Date 2019-08-02 16:39
**/
public static int[] dingleDigitIntArray = {0,1,2,3,4,5,6,7,8,9};
/**
* 个位数字串(阿里编码规约不允许直接使用‘魔法值’)
* @Author pwang
* @Date 2019-08-02 16:39
**/
public static String[] dingleDigitStrArray = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"};
/**
* 逗号
* @Author fxj
* @Date
* @param null
* @return
**/
public static final char COMMA_CHAR = ',';
public static final char SPOT_CHAR = '.';
}
package com.yifu.cloud.plus.v1.yifu.common.core.util;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import java.util.regex.Matcher;
......@@ -54,4 +55,38 @@ public class Common {
Matcher match = pattern.matcher(str);
return match.matches() != false;
}
/**
* 允许上传的文件类型
* @Author pwang
* @Date 2020-01-07 15:52
* @param null
* @return
**/
private static final String suffixList = "gif|jpg|jpeg|png|bmp|xls|xlsx|csv|pdf|docx|doc|m4a|mp3|zip|rar|txt";
/**
* 判断是否为允许的上传文件类型,true表示允许
*/
public static boolean checkFile(String fileName) {
// 获取文件后缀
String suffix = getSuffix(fileName);
if(null != suffix){
if (suffixList.contains(suffix.trim().toLowerCase())) {
return true;
}
}
return false;
}
/**
* 判断是否为允许的上传文件类型,true表示允许
*/
public static String getSuffix(String fileName) {
if(isEmpty(fileName) || fileName.lastIndexOf(CommonConstants.SPOT_CHAR) < 0 ){
return null;
}
// 获取文件后缀
return fileName.substring(fileName.lastIndexOf(CommonConstants.SPOT_CHAR)
+ 1, fileName.length());
}
}
package com.yifu.cloud.plus.v1.yifu.common.core.util;
import cn.hutool.core.io.IoUtil;
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.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.bean.OSSObjectBean;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author fang
* @Description 阿里云 OSS存储操作工具类
*/
@Component
@ConfigurationProperties(prefix = "oss")
@Data
@Slf4j
public class OSSUtil {
// Endpoint以北京为例,其它Region请按实际情况填写。
private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
private String openEndpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
private String accessKeyId = "LTAIDh9goA3jgpun";
private String accessKeySecret = "eN8EHPAZxglNfcUCMhAT02cy93omLa";
//存储空间名称
private String bucketName = "yf-hr-static-source";
private String errorMessage ="Error Message: ";
//存储 文件夹/文件名称 或 文件名称
private OSS ossClient = null;
//存储 文件夹/文件名称 或 文件名称
private OSS openOssClient = null; //存储 文件夹/文件名称 或 文件名称
// OSSClient实例化
private OSS initOssClient(int type){
if(CommonConstants.dingleDigitIntArray[0] == type){
if(null == ossClient) {
ossClient = new OSSClientBuilder().build(this.endpoint, this.accessKeyId, this.accessKeySecret);
}
return ossClient;
}else if(CommonConstants.dingleDigitIntArray[1] == type){
if(null == ossClient) {
openOssClient = new OSSClientBuilder().build(this.openEndpoint, this.accessKeyId, this.accessKeySecret);
}
return openOssClient;
}
return null;
}
// 创建 Bucket 存储空间 默认 private 写需要身份校验 读不需要
private void initBucket(String bucketName){
ossClient.createBucket(bucketName);
CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
createBucketRequest.setCannedACL(CannedAccessControlList.Private);
ossClient.createBucket(createBucketRequest);
}
/**
* @author fang
* @param file 传文件
* @param key 存储文件名称 可包含文件夹 如:文件夹/文件名 或 文件名
* @param bucketName 存储空间名称 如果不存在新增存储空间 属性 公告读取 不传参默认配置文件的存储空间
* @return
* @throws IOException
*/
public Boolean uploadFileByFile( File file, String key, String bucketName) {
try {
OSS client = null;
//OSSClient实例化
client = initOssClient(CommonConstants.dingleDigitIntArray[0]);
//为空默认使用配置的存储空间
if(!Common.isNotNull(bucketName)) {
bucketName = this.bucketName;
}
//查看Bucket存储空间 是否已存在,不存在 Create a new OSS bucket
if (!client.doesBucketExist(bucketName)) {
initBucket(bucketName);
}
/*
* Determine whether an object residents in your bucket 查看存储空间文件是否存在
*/
boolean exists = client.doesObjectExist(bucketName, key);
/*
* Upload an object to your bucket 上传一个文件到 存储空间
*/
//不存在就存储 存在 直接进行下一步
if(!exists){
client.putObject(new PutObjectRequest(bucketName, key, file));
}
return true;
} catch (OSSException oe) {
ossexceptionlog(oe);
} catch (ClientException ce) {
clientExceptionLog(ce);
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
}
return false;
}
public boolean uploadFileByStream(InputStream inputStream, String key, String bucketName) {
return uploadFileByStream(inputStream,key,bucketName, CommonConstants.dingleDigitIntArray[0]);
}
/**
* @author fang
* @param inputStream 传文件流
* @param key 存储文件名称 可包含文件夹 如:文件夹/文件名 或 文件名
* @param bucketName 存储空间名称 如果不存在新增存储空间 属性 公告读取 不传参默认配置文件的存储空间
* @param clientIndex 0人力私有空间客户端 1开发空间的客户端
* @return
* @throws IOException
*/
public boolean uploadFileByStream(InputStream inputStream, String key, String bucketName,int clientIndex) {
try {
//OSSClient实例化
OSS client = initOssClient(clientIndex);
if(null == client){
log.error("oss客户端获取错误,无此客户端",clientIndex);
return Boolean.FALSE;
}
//为空默认使用配置的存储空间
if(!Common.isNotNull(bucketName)) {
bucketName = this.bucketName;
}
//查看Bucket存储空间 是否已存在,不存在 Create a new OSS bucket
if (!client.doesBucketExist(bucketName)) {
initBucket(bucketName);
}
/*
* Determine whether an object residents in your bucket 查看存储空间文件是否存在
*/
boolean exists = client.doesObjectExist(bucketName, key);
/*
* Upload an object to your bucket 上传一个文件到 存储空间
*/
//不存在就存储 存在 直接进行下一步
if(!exists){
client.putObject(new PutObjectRequest(bucketName, key, inputStream));
}
return true;
} catch (OSSException oe) {
ossexceptionlog(oe);
} catch (ClientException ce) {
clientExceptionLog(ce);
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
}
return false;
}
/**
* @author fang
* 查看文件是否存在
* @param bucketName 默认用配置的
* @param key 不可为空
* @return
*/
public Boolean doesObjectExist(String bucketName,String key){
return doesObjectExist(bucketName,key);
}
public Boolean doesObjectExist(String bucketName,String key,int clientIndex){
try{
bucketName = initBucketName(bucketName);
if(!Common.isNotNull(key)){
return false;
}
//OSSClient实例化
OSS client = initOssClient(clientIndex);
if(null == client){
log.error("oss客户端获取错误,无此客户端",clientIndex);
return Boolean.FALSE;
}
return client.doesObjectExist(bucketName, key);
} catch (OSSException oe) {
ossexceptionlog(oe);
} catch (ClientException ce) {
clientExceptionLog(ce);
}
return false;
}
/**
* @author fang
* @param bucketName 默认用配置的
* @param key 不可为空w 文件名称
* @return
* 如果Object是公共读/公共读写权限,那么访问URL的格式为:Bucket名称.Endpoint/Object名称
* 如果Object是私有权限,则必须进行签名操作。访问URL的格式为:Bucket名称.Endpoint/Object名称?签名参数
* 此次统一按签名方式返回
*/
public URL getObjectUrl(String bucketName,String key){
try{
bucketName = initBucketName(bucketName);
if(!Common.isNotNull(key)){
return null;
}
initOssClient(CommonConstants.dingleDigitIntArray[0]);
// 生成URL 时效 1小时
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
return url;
} catch (OSSException oe) {
ossexceptionlog(oe);
} catch (ClientException ce) {
clientExceptionLog(ce);
}
return null;
}
public void getObjectZip(String bucketName, List<OSSObjectBean> srcList, ZipOutputStream zip){
getObjectZip(bucketName,srcList,zip,CommonConstants.dingleDigitIntArray[0]);
}
public void getObjectZip(String bucketName, List<OSSObjectBean> srcList,ZipOutputStream zip,int clientIndex){
InputStream inputStream = null;
try{
bucketName = initBucketName(bucketName);
//OSSClient实例化
OSS client = initOssClient(clientIndex);
if(null == client){
log.error("oss客户端获取错误,无此客户端",clientIndex);
}else{
OSSObject ossObject = null;
for(OSSObjectBean key:srcList){
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
ossObject = client.getObject(bucketName, key.getKey());
if(null != ossObject){
// 读取文件内容。
log.info("Object content:");
inputStream = ossObject.getObjectContent();
//添加到zip
zip.putNextEntry(new ZipEntry(key.getName()));
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) != -1){
zip.write(buf, 0, len);
}
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
IoUtil.close(inputStream);
zip.closeEntry();
}
}
// 关闭OSSClient。
} catch (OSSException oe) {
ossexceptionlog(oe);
} catch (ClientException ce) {
clientExceptionLog(ce);
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(inputStream);
}
}
/**
* @author fang
* @param bucketName 默认用配置的
* @param key 不可为空
* @return
*/
public Boolean deleteObject(String bucketName,String key) {
return deleteObject(bucketName, key,CommonConstants.dingleDigitIntArray[0]);
}
public Boolean deleteObject(String bucketName,String key,int clientIndex) {
try{
bucketName = initBucketName(bucketName);
if (!Common.isNotNull(key)) {
return false;
}
//OSSClient实例化
OSS client = initOssClient(clientIndex);
if(null == client){
log.error("oss客户端获取错误,无此客户端",clientIndex);
return Boolean.FALSE;
}
ossClient.deleteObject(bucketName, key);
return true;
} catch (OSSException oe) {
log.error("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
log.error(errorMessage + oe.getErrorCode());
log.error("Error Code: " + oe.getErrorCode());
log.error("Request ID: " + oe.getRequestId());
log.error("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
clientExceptionLog(ce);
}
return false;
}
/**
* @author fang
* @param bucketName 默认用配置的
* @return
*/
private String initBucketName(String bucketName){
if(!Common.isNotNull(bucketName)){
return this.bucketName;
}else{
return bucketName;
}
}
private void ossexceptionlog(OSSException oe){
log.error("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
log.error(errorMessage + oe.getErrorCode());
log.error("Error Code: " + oe.getErrorCode());
log.error("Request ID: " + oe.getRequestId());
log.error("Host ID: " + oe.getHostId());
}
private void clientExceptionLog(ClientException ce){
log.error("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
log.error(errorMessage + ce.getMessage());
}
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.bean;
import lombok.Data;
/**
* @program: master
* @description: 文件打包工具类
* @author: pwang
* @create: 2019-10-30 15:48
**/
@Data
public class OSSObjectBean {
/**
* 生成文件的名称(要带后缀)
* @Author pwang
* @Date 2019-10-30 19:03
* @param null
* @return
**/
String name;
/**
* oss获取文件的key
* @Author pwang
* @Date 2019-10-30 19:05
* @param null
* @return
**/
String key;
}
package com.yifu.cloud.plus.v1.yifu.common.core.vo;
import lombok.Data;
import java.io.Serializable;
/**
* @program: master
* @description: 前端要求返回结构
* @author: wangan
**/
@Data
public class FileVo implements Serializable {
protected String uid;
protected String name;
protected String status;
protected String url;
protected String type;
public FileVo(String uid, String name, String url) {
this.uid = uid;
this.name = name;
this.status = "done";
this.url = url;
}
}
......@@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @date 2018年06月21日 用户统一管理系统
*/
@EnableYifuResourceServer
//@EnableYifuFeignClients
@SpringBootApplication
public class YifuAdminApplication {
......
......@@ -36,21 +36,6 @@
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-swagger</artifactId>
</dependency>
<!--注册中心客户端-->
<!--<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>-->
<!--配置中心客户端-->
<!--<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>-->
<!--断路器依赖-->
<!--<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-feign</artifactId>
</dependency>-->
<!--数据源-->
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
......
......@@ -25,10 +25,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author lengleng
* @date 2020/03/11 代码生成模块
*/
//@EnableYifuDoc
@EnableDynamicDataSource
//@EnableYifuFeignClients
//@EnableDiscoveryClient
@SpringBootApplication
@EnableYifuResourceServer
public class YifuCodeGenApplication {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment