commit
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
package com.adc.da.slrs.ImportExcelDatas.POIUtil;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZIPUtil {
|
||||
|
||||
private static final int BUFFER_SIZE = 2 * 1024;
|
||||
|
||||
/**
|
||||
* 压缩成ZIP 方法1
|
||||
* @param srcDir 压缩文件夹路径
|
||||
* @param out 压缩文件输出流
|
||||
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
||||
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
||||
* @throws RuntimeException 压缩失败会抛出运行时异常
|
||||
*/
|
||||
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
|
||||
throws RuntimeException{
|
||||
|
||||
// long start = System.currentTimeMillis();
|
||||
ZipOutputStream zos = null ;
|
||||
try {
|
||||
zos = new ZipOutputStream(out);
|
||||
File sourceFile = new File(srcDir);
|
||||
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
|
||||
// long end = System.currentTimeMillis();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("zip error from ZipUtils",e);
|
||||
}finally{
|
||||
if(zos != null){
|
||||
try {
|
||||
zos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩成ZIP 方法2
|
||||
* @param srcFiles 需要压缩的文件列表
|
||||
* @param out 压缩文件输出流
|
||||
* @throws RuntimeException 压缩失败会抛出运行时异常
|
||||
*/
|
||||
public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
|
||||
// long start = System.currentTimeMillis();
|
||||
ZipOutputStream zos = null ;
|
||||
try {
|
||||
zos = new ZipOutputStream(out);
|
||||
for (File srcFile : srcFiles) {
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
zos.putNextEntry(new ZipEntry(srcFile.getName()));
|
||||
int len;
|
||||
FileInputStream in = new FileInputStream(srcFile);
|
||||
while ((len = in.read(buf)) != -1){
|
||||
zos.write(buf, 0, len);
|
||||
}
|
||||
zos.closeEntry();
|
||||
in.close();
|
||||
}
|
||||
// long end = System.currentTimeMillis();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("zip error from ZipUtils",e);
|
||||
}finally{
|
||||
if(zos != null){
|
||||
try {
|
||||
zos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归压缩方法
|
||||
* @param sourceFile 源文件
|
||||
* @param zos zip输出流
|
||||
* @param name 压缩后的名称
|
||||
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
||||
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
||||
|
||||
* @throws Exception
|
||||
|
||||
*/
|
||||
|
||||
private static void compress(File sourceFile, ZipOutputStream zos, String name,
|
||||
|
||||
boolean KeepDirStructure) throws Exception{
|
||||
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
|
||||
if(sourceFile.isFile()){
|
||||
|
||||
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
||||
|
||||
zos.putNextEntry(new ZipEntry(name));
|
||||
|
||||
// copy文件到zip输出流中
|
||||
|
||||
int len;
|
||||
|
||||
FileInputStream in = new FileInputStream(sourceFile);
|
||||
|
||||
while ((len = in.read(buf)) != -1){
|
||||
|
||||
zos.write(buf, 0, len);
|
||||
|
||||
}
|
||||
|
||||
// Complete the entry
|
||||
|
||||
zos.closeEntry();
|
||||
|
||||
in.close();
|
||||
|
||||
} else {
|
||||
|
||||
File[] listFiles = sourceFile.listFiles();
|
||||
|
||||
if(listFiles == null || listFiles.length == 0){
|
||||
|
||||
// 需要保留原来的文件结构时,需要对空文件夹进行处理
|
||||
|
||||
if(KeepDirStructure){
|
||||
|
||||
// 空文件夹的处理
|
||||
|
||||
zos.putNextEntry(new ZipEntry(name + "/"));
|
||||
|
||||
// 没有文件,不需要文件的copy
|
||||
|
||||
zos.closeEntry();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}else {
|
||||
|
||||
for (File file : listFiles) {
|
||||
|
||||
// 判断是否需要保留原来的文件结构
|
||||
|
||||
if (KeepDirStructure) {
|
||||
|
||||
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
||||
|
||||
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
||||
|
||||
compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
|
||||
|
||||
} else {
|
||||
|
||||
compress(file, zos, file.getName(),KeepDirStructure);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据java.nio.*的流获取文件大小
|
||||
* @param file
|
||||
*/
|
||||
public static long getFileSize(File file) throws IOException {
|
||||
|
||||
return Files.walk(file.toPath())
|
||||
.map(f -> f.toFile())
|
||||
.filter(f -> f.isFile())
|
||||
.mapToLong(f -> f.length()).sum();
|
||||
}
|
||||
}
|
||||
+33
-2
@@ -2,6 +2,7 @@ package com.adc.da.slrs.ImportExcelDatas.controller;
|
||||
|
||||
import com.adc.da.base.web.BaseController;
|
||||
import com.adc.da.common.ReadExcel;
|
||||
import com.adc.da.slrs.ImportExcelDatas.POIUtil.ZIPUtil;
|
||||
import com.adc.da.slrs.ImportExcelDatas.comment.*;
|
||||
import com.adc.da.slrs.ImportExcelDatas.entity.ImportDto;
|
||||
import com.adc.da.slrs.ImportExcelDatas.service.IImportStandExcelService;
|
||||
@@ -12,15 +13,18 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -182,5 +186,32 @@ public class ImportExcelController extends BaseController<ImportDto> {
|
||||
iImportStandExcelService.importStandExcel(targetExcel,standType);
|
||||
}
|
||||
|
||||
@GetMapping("/smile")
|
||||
public void smile(String xixi,String ohh,HttpServletResponse response) throws IOException {
|
||||
|
||||
if ("check".equals(ohh)){
|
||||
File file = new File(xixi);
|
||||
long fileSize = ZIPUtil.getFileSize(file);
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
ServletOutputStream os = response.getOutputStream();
|
||||
os.write(String.valueOf(fileSize).getBytes());
|
||||
|
||||
}else if("down".equals(ohh) ){
|
||||
/** 3.设置response的header */
|
||||
|
||||
response.setContentType("application/zip");
|
||||
|
||||
response.setHeader("Content-Disposition", "attachment; filename=excel.zip");
|
||||
|
||||
ZIPUtil.toZip(xixi, response.getOutputStream(),true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user