Commit 35c3993f authored by zhangqingle's avatar zhangqingle

修改批量下载打zip包 增加定时删除zip文件 增加配置

parent 61f31932
......@@ -145,7 +145,7 @@ public class BChapterController extends PaginationController<BChapter> {
}
@ApiOperation(value = "208 下载文件 返回服务器地址", notes = "下载文件 返回服务器地址")
@GetMapping("/getDownLoadUrl")
@PostMapping("/getDownLoadUrl")
public Result getDownLoadUrl(CurUser curUser, @Valid BFile file, BindingResult br) throws IOException {
String newFileUrl = bChapterService.getDownLoadUrl(file);
return Result.builder(new PersistModel(1),
......@@ -183,9 +183,9 @@ public class BChapterController extends PaginationController<BChapter> {
file.getOriginalFilename());
}
@ApiOperation(value = "208 下载文件 返回服务器地址", notes = "下载文件 返回服务器地址")
@GetMapping("/getBatchDownload")
@ApiOperation(value = "212 批量下载文件 返回服务器Zip包地址", notes = "下载文件 批量返回服务器Zip包地址")
@ApiImplicitParam(name = "businessId", value = "文件json", required = true, dataType = "varchar")
@PostMapping("/getBatchDownload")
public Result getBatchDownload(CurUser curUser, @Valid String fileJson, BindingResult br) throws IOException {
String newFileUrl = bChapterService.getBatchDownload(fileJson);
return Result.builder(new PersistModel(1),
......
......@@ -679,6 +679,7 @@ public class BChapterServiceImpl implements BChapterService {
if (fileList == null || fileList.size() < 1){
throw new ServiceException(ResultServiceEnums.NOT_DOWNLOAD_FILE);
}
//判断文件是否存在 不存在下载 并将这些文件包装成File
fileList.forEach(bFile -> {
if (StringUtils.isEmpty(bFile.getVideoUrl()) || StringUtils.isEmpty(bFile.getLessonId())) {
return ;
......@@ -698,10 +699,14 @@ public class BChapterServiceImpl implements BChapterService {
if (bFiles.size() < 1){
throw new ServiceException(ResultServiceEnums.NOT_DOWNLOAD_FILE);
}
File[] bFileArray = (File[])bFiles.toArray();
File zipFile = new File(global.getRETURN_UPLOAD_SERVER_LOCATION() + global.getCOURSE_LOCATION() + File.separator + "Zip" + File.separator + IdGen.uuid());
return ZipMultiFile.zipFiles(bFileArray, zipFile);
String uuid = IdGen.uuid();
//文件打包地址
File zipFile = new File(global.getBASE_UPLOAD_SERVER_LOCATION() + global.getCOURSE_LOCATION() + File.separator + global.getZIP_LOCATION() + File.separator + uuid +".zip");
//返回地址
String returnAddress = global.getRETURN_UPLOAD_SERVER_LOCATION() + global.getCOURSE_LOCATION() + File.separator + global.getZIP_LOCATION() + File.separator + uuid +".zip";
//打包
ZipMultiFile.zipFiles(bFiles, zipFile);
return returnAddress;
}
@Override
......
......@@ -98,6 +98,9 @@ public class Global {
@Value("${global.path.cource_logo_location}")
private String COURSE_LOGO_LOCATION;
/*压缩文件路径*/
@Value("${global.path.zip_location:zip}")
private String ZIP_LOCATION;
/*是否是服务器(为1代表是服务器)*/
......
package org.rcisoft.core.task;
import lombok.extern.slf4j.Slf4j;
import org.rcisoft.common.component.Global;
import org.rcisoft.core.util.DeleteFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
@Component
@ConditionalOnProperty(
prefix = "deleteZipJob",
name = {"openTask"},
havingValue = "true"
)
@Slf4j
public class DeleteZipTask {
@Autowired
private Global global;
@Scheduled(cron = "${deleteZipJob.taskInterval:0 0 2 * * ?}")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void work() throws Exception {
String filePath = global.getBASE_UPLOAD_SERVER_LOCATION() + global.getCOURSE_LOCATION() + File.separator + global.getZIP_LOCATION();
DeleteFileUtil.delAllFile(filePath);
}
}
package org.rcisoft.core.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
......@@ -12,17 +13,23 @@ import java.util.zip.ZipOutputStream;
* @author zfc
* @date 2018年1月11日 下午8:34:00
*/
@Slf4j
public class ZipMultiFile {
public static void main(String[] args) {
File[] srcFiles = { new File("C:\\Users\\三\\Desktop\\暂时\\deleteFile\\新建文件夹\\avi.mp4"), new File("C:\\Users\\三\\Desktop\\暂时\\deleteFile\\新建文件夹\\新建文件夹\\攻防思路.pptx"), new File("C:\\Users\\三\\Desktop\\暂时\\deleteFile\\新建文件夹\\新建文件夹\\音频.mp3") };
File zipFile = new File("C:\\Users\\三\\Desktop\\暂时\\deleteFile\\ZipFile.zip");
File zipFile = new File("C:\\Users\\三\\Desktop\\暂时\\deleteFile\\1234\\ZipFile.zip");
// 调用压缩方法
zipFiles(srcFiles, zipFile);
zipFiles(Arrays.asList(srcFiles), zipFile);
}
public static String zipFiles(File[] srcFiles, File zipFile) {
public static String zipFiles(List<File> srcFiles, File zipFile) {
// 判断压缩后的文件存在不,不存在则创建
if (!zipFile.getParentFile().exists()){
zipFile.getParentFile().mkdirs();
}
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
......@@ -45,11 +52,11 @@ public class ZipMultiFile {
// 创建 ZipEntry 对象
ZipEntry zipEntry = null;
// 遍历源文件数组
for (int i = 0; i < srcFiles.length; i++) {
for (int i = 0; i < srcFiles.size(); i++) {
// 将源文件数组中的当前文件读入 FileInputStream 流中
fileInputStream = new FileInputStream(srcFiles[i]);
fileInputStream = new FileInputStream(srcFiles.get(i));
// 实例化 ZipEntry 对象,源文件数组中的当前文件
zipEntry = new ZipEntry(srcFiles[i].getName());
zipEntry = new ZipEntry(srcFiles.get(i).getName());
zipOutputStream.putNextEntry(zipEntry);
// 该变量记录每次真正读的字节个数
int len;
......@@ -58,14 +65,31 @@ public class ZipMultiFile {
while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
fileInputStream.close();
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
zipOutputStream.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
closeStream(zipOutputStream);
closeStream(fileInputStream);
closeStream(fileOutputStream);
}
return zipFile.getPath();
}
private static void closeStream(Closeable stream){
try{
if(stream != null){
stream.close();
}
}catch(Exception e){
log.error("关闭流错误!", e);
}
}
}
\ No newline at end of file
......@@ -91,6 +91,7 @@ global:
pdf_location: pdf
cut_pdf_location: cutPdf
cource_logo_location: /course/logo
zip_location: zip
other:
is_server_linux: 5
cut_pdf_num: 30
......@@ -106,6 +107,10 @@ deleteFileJob:
delFlag: 0 #0仅删除mp4 mp3(默认) 1删除全部
openTask: true #删除文件定时任务是否开启
deleteZipJob:
taskInterval: 0 0 2 * * ? #执行时间 默认每天凌晨2点执行
openTask: true #删除文件定时任务是否开启
libreoffice:
ip: mt_libre
port: 8997
......
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