添加文件异步下载的功能

This commit is contained in:
udf_admin@163.com
2017-12-17 21:46:47 +08:00
parent 092388508f
commit a52987c155
8 changed files with 126 additions and 11 deletions
@@ -109,9 +109,9 @@ public class ExportExcel {
final int num16 = 16;
final int num30 = 30;
final int num256 = 256;
this.wb = new SXSSFWorkbook(rowAccessWindowSize); //有没有被关闭的风险
this.wb = new SXSSFWorkbook(rowAccessWindowSize); //设置刷新行数大小
this.wb.setCompressTempFiles(true); // 缓冲区文件压缩
this.sheet = this.wb.createSheet("Export");
this.sheet = this.wb.createSheet(title);
this.styles = createStyles();
// 创建标题
if (StringUtils.isNotBlank(title)) {
@@ -18,17 +18,21 @@ public class DownConstant {
*/
public enum DOWNLOAD_PROCESS_STATUS {
/**
* 执行中
* 待执行
*/
STATUS_1(1),
/**
* 执行完成
* 执行中
*/
STATUS_2(2),
/**
* 执行完成
*/
STATUS_3(3),
/**
* 执行错误
*/
STATUS_3(3);
STATUS_4(4);
/**
* value
@@ -0,0 +1,12 @@
package org.itkk.udf.file.aliyun.oss.api.download;
import java.lang.annotation.*;
/**
* DownLoad
*/
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DownLoad {
}
@@ -19,6 +19,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.File;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
/**
@@ -66,7 +67,9 @@ public class DownLoadProcessAspect {
* @param proceedingJoinPoint proceedingJoinPoint
* @return Object
*/
@Around("this(org.itkk.udf.file.aliyun.oss.api.download.IDownLoadProcess) && args(org.itkk.udf.file.aliyun.oss.api.download.DownLoadParam)")
@Around("this(org.itkk.udf.file.aliyun.oss.api.download.IDownLoadProcess)" +
" && @annotation(org.itkk.udf.file.aliyun.oss.api.download.DownLoad)" +
" && args(org.itkk.udf.file.aliyun.oss.api.download.DownLoadParam)")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
//定义返回值
Object result = null;
@@ -81,7 +84,7 @@ public class DownLoadProcessAspect {
info.setId(param.getId());
try {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_1.value());
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_2.value());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//执行
result = proceedingJoinPoint.proceed();
@@ -90,14 +93,14 @@ public class DownLoadProcessAspect {
//上传文件到阿里云OSS
String objectKey = ossWarpper.uploadFile(param.getOssCode(), file);
//删除本地文件
file.delete();
Files.delete(file.toPath());
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_2.value());
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_3.value());
info.setObjectKey(objectKey);
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
} catch (Throwable e) {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_3.value());
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_4.value());
info.setErrorMsg(e.getMessage());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//抛出异常
@@ -20,7 +20,7 @@ public class DownloadInfo {
/**
* status
*/
@ApiModelProperty(value = "下载状态 ( 1 : 执行中 , 2 : 执行完成 , 3 : 执行错误 )", required = true, dataType = "int")
@ApiModelProperty(value = "下载状态 ( 1 : 待执行 , 2 : 执行中 , 3 : 执行完成 , 4 : 执行错误 )", required = true, dataType = "int")
private Integer status;
/**
@@ -0,0 +1,66 @@
package org.itkk.udf.file.aliyun.oss.service;
import org.itkk.udf.cache.redis.CacheRedisProperties;
import org.itkk.udf.file.aliyun.oss.api.download.DownConstant;
import org.itkk.udf.file.aliyun.oss.api.download.DownLoadProcessAspect;
import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* DownLoadProcessService
*/
@Service
public class DownLoadProcessService {
/**
* redisTemplate
*/
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* cacheRedisProperties
*/
@Autowired
private CacheRedisProperties cacheRedisProperties;
/**
* generateDownLoadId
*
* @return String
*/
public String generateDownLoadId() {
//生成ID
String id = UUID.randomUUID().toString();
//获得key
String key = cacheRedisProperties.getPrefix() + DownLoadProcessAspect.DOWNLOAD_CACHE_PREFIX + id;
//构造对象
DownloadInfo info = new DownloadInfo();
info.setId(id);
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_1.value());
//更新缓存
redisTemplate.opsForValue().set(key, info, DownLoadProcessAspect.DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//返回ID
return id;
}
/**
* get
*
* @param id id
* @return DownloadInfo
*/
public DownloadInfo getDownloadInfo(String id) {
//获得key
String key = cacheRedisProperties.getPrefix() + DownLoadProcessAspect.DOWNLOAD_CACHE_PREFIX + id;
//获得缓存值
Object cacheValue = redisTemplate.opsForValue().get(key);
//返回
return cacheValue == null ? null : (DownloadInfo) cacheValue;
}
}
@@ -10,6 +10,8 @@ import org.itkk.udf.core.RestResponse;
import org.itkk.udf.core.domain.aliyun.oss.OssParam;
import org.itkk.udf.file.aliyun.oss.api.OssWarpper;
import org.itkk.udf.file.aliyun.oss.api.domain.PolicyResult;
import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo;
import org.itkk.udf.file.aliyun.oss.service.DownLoadProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
@@ -32,6 +34,12 @@ public class AliyunOssController implements IAliuyinOssController {
@Autowired
private OssWarpper ossWarpper;
/**
* downLoadProcessService
*/
@Autowired
private DownLoadProcessService downLoadProcessService;
@Override
public RestResponse<PolicyResult> policy(@PathVariable String code) throws IOException {
return new RestResponse<>(ossWarpper.getPolicy(code));
@@ -52,4 +60,9 @@ public class AliyunOssController implements IAliuyinOssController {
public RestResponse<Boolean> checkExist(@RequestBody @Valid OssParam ossParam) {
return new RestResponse<>(ossWarpper.checkExist(ossParam.getCode(), ossParam.getObjectKey()));
}
@Override
public RestResponse<DownloadInfo> getDownloadInfo(@PathVariable String id) {
return new RestResponse<>(downLoadProcessService.getDownloadInfo(id));
}
}
@@ -13,6 +13,7 @@ import io.swagger.annotations.ApiOperation;
import org.itkk.udf.core.RestResponse;
import org.itkk.udf.core.domain.aliyun.oss.OssParam;
import org.itkk.udf.file.aliyun.oss.api.domain.PolicyResult;
import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -93,4 +94,20 @@ public interface IAliuyinOssController {
@RequestMapping(value = "check/exist", method = RequestMethod.POST)
RestResponse<Boolean> checkExist(@RequestBody @Valid OssParam ossParam);
/**
* 获得文件下载信息(供客户端轮询)
*
* @param id id
* @return RestResponse<DownloadInfo>
*/
@ApiOperation(value = "FILE_ALIYUN_OSS_DOWNLOAD_INFO", notes = "获得文件下载信息(供客户端轮询)")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "path", name = "id", value = "文件下载ID", required = true, dataType = "string")
})
@RequestMapping(value = "download/info/{id}", method = RequestMethod.GET)
RestResponse<DownloadInfo> getDownloadInfo(@PathVariable String id);
}