将被下载的文件,上传阿里云OSS的时候进行压缩,节省空间和流量

This commit is contained in:
wangkang
2017-12-19 13:25:23 +08:00
parent 144530c319
commit 82c611f6cd
6 changed files with 284 additions and 5 deletions
+1
View File
@@ -21,6 +21,7 @@
<springfox-swagger2.version>2.7.0</springfox-swagger2.version>
<commons-collections4.version>4.1</commons-collections4.version>
<commons-lang3.version>3.7</commons-lang3.version>
<commons-compress.version>1.15</commons-compress.version>
<quartz.version>2.3.0</quartz.version>
<alibaba.druid.version>1.1.5</alibaba.druid.version>
<jasypt-spring-boot-starter.version>1.16</jasypt-spring-boot-starter.version>
+5
View File
@@ -97,6 +97,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
@@ -0,0 +1,214 @@
/**
* ZipFile.java
* Created at 2016-03-01
* Created by Administrator
* Copyright (C) 2016 LLSFW, All rights reserved.
*/
package org.itkk.udf.core.utils;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import java.io.*;
/**
* <p>
* ClassName: ZipFile
* </p>
* <p>
* Description: 压缩&解压文件
* </p>
* <p>
* Author: wangkang
* </p>
* <p>
* Date: 2014年1月4日
* </p>
*/
public class ZipUtil {
/**
* <p>
* Field byteSize: 字节数组长度
* </p>
*/
private static final int BYTE_SIZE = 1024;
/**
* <p>
* Field encoding: 编码格式
* </p>
*/
private String encoding;
/**
* <p>
* Description: 构造方法
* </p>
*
* @param encoding 编码
*/
public ZipUtil(String encoding) {
this.encoding = encoding;
}
/**
* <p>
* Description: 把zip文件解压到指定的文件夹
* </p>
*
* @param zipFilePath zip文件路径
* @param saveFileDir 解压后的文件存放路径
* @throws IOException 异常
*/
public void decompressZip(String zipFilePath, String saveFileDir) throws IOException {
File file = new File(zipFilePath);
if (file.exists()) {
this.decompressZip(file, saveFileDir);
} else {
throw new IOException("文件不存在");
}
}
/**
* <p>
* Description: 解压
* </p>
*
* @param file 压缩包文件
* @param saveFileDir 保存路径
* @throws IOException 异常
*/
private void decompressZip(File file, String saveFileDir) throws IOException {
InputStream is = null;
ZipArchiveInputStream zais = null;
try { //NOSONAR
is = new FileInputStream(file);
zais = new ZipArchiveInputStream(is, this.encoding);
ArchiveEntry archiveEntry;
// 把zip包中的每个文件读取出来
// 然后把文件写到指定的文件夹
while ((archiveEntry = zais.getNextEntry()) != null) {
// 如果是目录,则自动创建
if (archiveEntry.isDirectory()) {
File f = new File(saveFileDir + archiveEntry.getName());
boolean rs = f.mkdir();
if (rs) {
continue;
} else {
throw new IOException("目录创建失败");
}
}
// 构造解压出来的文件存放路径
this.decompressZip(saveFileDir, archiveEntry, zais);
}
} finally {
if (zais != null) {
zais.close();
}
if (is != null) {
is.close();
}
}
}
/**
* <p>
* Description: 解压
* </p>
*
* @param saveFileDir 存储路径
* @param archiveEntry 压缩相
* @param zais 压缩流
* @throws IOException 异常
*/
private void decompressZip(String saveFileDir, ArchiveEntry archiveEntry, ZipArchiveInputStream zais)
throws IOException {
String entryFilePath = saveFileDir + archiveEntry.getName();
byte[] content = new byte[BYTE_SIZE];
OutputStream os = null;
try { //NOSONAR
// 把解压出来的文件写到指定路径
File entryFile = new File(entryFilePath);
os = new FileOutputStream(entryFile);
int r;
while ((r = zais.read(content)) > 0) {
os.write(content, 0, r);
}
os.flush();
} finally {
if (os != null) {
os.close();
}
}
}
/**
* <p>
* Description: 压缩文件
* </p>
*
* @param files 需要压缩的文件
* @param zipFilePath zip文件
* @throws IOException 异常
*/
public void compressFiles2Zip(File[] files, String zipFilePath) throws IOException {
if (files != null && files.length > 0) {
ZipArchiveOutputStream zaos = null;
try { //NOSONAR
File zipFile = new File(zipFilePath);
zaos = new ZipArchiveOutputStream(zipFile);
// 将每个文件用ZipArchiveEntry封装
// 再用ZipArchiveOutputStream写到压缩文件中
for (File file : files) {
this.compressFiles2Zip(file, zaos);
}
zaos.finish();
} finally {
if (zaos != null) {
zaos.close();
}
}
}
}
/**
* <p>
* Description: 压缩
* </p>
*
* @param file 文件
* @param zaos 压缩流
* @throws IOException 异常
*/
private void compressFiles2Zip(File file, ZipArchiveOutputStream zaos) throws IOException {
final int ioBuffered = 1024;
if (file != null) {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
zaos.putArchiveEntry(zipArchiveEntry);
InputStream is = null;
try { //NOSONAR
is = new BufferedInputStream(new FileInputStream(file), ioBuffered);
byte[] buffer = new byte[BYTE_SIZE];
int len;
while ((len = is.read(buffer)) != -1) {
zaos.write(buffer, 0, len);
}
zaos.closeArchiveEntry();
} finally {
if (is != null) {
is.close();
}
}
}
}
}
@@ -11,7 +11,9 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.itkk.udf.cache.redis.CacheRedisProperties;
import org.itkk.udf.core.ApplicationConfig;
import org.itkk.udf.core.exception.SystemRuntimeException;
import org.itkk.udf.core.utils.ZipUtil;
import org.itkk.udf.file.aliyun.oss.api.OssWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
@@ -19,7 +21,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
@@ -41,7 +45,7 @@ public class DownLoadProcessAspect {
/**
* DOWNLOAD_WAITING_CACHE_EXPIRATION(待执行,执行中,缓存过期时间为1天)
*/
public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60 * 24;
public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60L * 24L;
/**
* DOWNLOAD_CACHE_EXPIRATION(执行完成,执行错误,缓存过期时间为10分钟)
@@ -66,6 +70,12 @@ public class DownLoadProcessAspect {
@Autowired
private OssWarpper ossWarpper;
/**
* applicationConfig
*/
@Autowired
private ApplicationConfig applicationConfig;
/**
* around
*
@@ -87,29 +97,56 @@ public class DownLoadProcessAspect {
//构造对象
DownloadInfo info = new DownloadInfo();
info.setId(param.getId());
//定义文件变量
File file = null;
File zipFile = null;
//开始处理
try {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_2.value());
info.setProcessStartDate(new Date());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_WAITING_CACHE_EXPIRATION, TimeUnit.MINUTES);
//执行
result = proceedingJoinPoint.proceed();
//获得本地文件
File file = (File) result;
file = (File) result;
//文件zip压缩
ZipUtil zipUtil = new ZipUtil(applicationConfig.getEncoding());
String zipFilePath = DownConstant.tempdir() + param.getId() + ".zip";
File[] files = {file};
zipUtil.compressFiles2Zip(files, zipFilePath);
zipFile = new File(zipFilePath);
//上传文件到阿里云OSS
String objectKey = ossWarpper.uploadFile(param.getOssCode(), file);
//删除本地文件
Files.delete(file.toPath());
String objectKey = ossWarpper.uploadFile(param.getOssCode(), zipFile);
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_3.value());
info.setProcessEndDate(new Date());
info.setObjectKey(objectKey);
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
} catch (Throwable e) {
//更新缓存
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_4.value());
info.setProcessEndDate(new Date());
info.setErrorMsg(e.getMessage());
redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES);
//抛出异常
throw new SystemRuntimeException(e);
} finally {
//删除本地文件
if (file != null) {
try {
Files.delete(file.toPath());
} catch (IOException e) {
log.error("error:", e);
}
}
if (zipFile != null) {
try {
Files.delete(zipFile.toPath());
} catch (IOException e) {
log.error("error:", e);
}
}
}
}
//返回
@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -46,6 +47,7 @@ public class DownLoadProcessService {
DownloadInfo info = new DownloadInfo();
info.setId(id);
info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_1.value());
info.setProcessInitDate(new Date());
//更新缓存
redisTemplate.opsForValue().set(key, info, DownLoadProcessAspect.DOWNLOAD_WAITING_CACHE_EXPIRATION, TimeUnit.MINUTES);
//返回ID
@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* DownloadInfo
*/
@@ -29,6 +31,24 @@ public class DownloadInfo {
@ApiModelProperty(value = "阿里云OSS的objectKey", required = true, dataType = "string")
private String objectKey;
/**
* 初始化时间
*/
@ApiModelProperty(value = "初始化时间", required = true, dataType = "date")
private Date processInitDate;
/**
* processStartDate
*/
@ApiModelProperty(value = "开始时间", required = true, dataType = "date")
private Date processStartDate;
/**
* processEndDate
*/
@ApiModelProperty(value = "结束时间", required = true, dataType = "date")
private Date processEndDate;
/**
* errorMsg
*/