import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.svw.mbv.api.mysteryguest.oqcmanagement.task.vo.TaskTripVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
@Service
@Slf4j
public class DownloadManager {
private static final String FilePath = System.getProperty("java.io.tmpdir") + File.separator;
public void execute(HttpServletResponse response, List<String> ids) {
List<String> filePaths = new ArrayList<>();
String tmpFileName = "批量下载.zip";
String strZipPath = FilePath + tmpFileName;
filePaths.add(strZipPath);
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
List<File> fileList = new ArrayList<File>();
for (int i = 0; i < 10; i++) {
String filename = FilePath + generateRandomFilename() + ".xlsx";
File file = creatFile(filename);
fileList.add(file);
filePaths.add(filename);
List<TaskTripVO> list = new ArrayList<>();
for (int j = 0; j < 10; j++) {
TaskTripVO taskTripVO = new TaskTripVO();
taskTripVO.setAreaName("测试" + i);
list.add(taskTripVO);
}
writeExcel(file, TaskTripVO.class, list, ExcelTypeEnum.XLSX);
}
byte[] buffer = new byte[1024];
for (int i = 0; i < fileList.size(); i++) {
File file = fileList.get(i);
FileInputStream fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
out.setEncoding("GBK");
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
this.downFile(response, tmpFileName, filePaths);
} catch (Exception e) {
deleteFile(filePaths);
log.error("文件下载出错", e);
}
}
private void downFile(HttpServletResponse response, String str, List<String> filePaths) {
try {
String path = FilePath + str;
File file = new File(path);
if (file.exists()) {
InputStream ins = new FileInputStream(path);
BufferedInputStream bins = new BufferedInputStream(ins);
OutputStream outs = response.getOutputStream();
BufferedOutputStream bouts = new BufferedOutputStream(outs);
response.setContentType("application/x-download");
response.setHeader(
"Content-disposition",
"attachment;filename="
+ URLEncoder.encode(str, "UTF-8"));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();
ins.close();
bins.close();
outs.close();
bouts.close();
deleteFile(filePaths);
}
} catch (IOException e) {
deleteFile(filePaths);
log.error("文件下载出错", e);
}
}
private File creatFile(String filePath) {
File file = new File(filePath);
return file;
}
public static boolean deleteFile(List<String> filePath) {
boolean result = false;
for (String pathname : filePath) {
File file = new File(pathname);
if (file.exists()) {
file.delete();
result = true;
}
}
return result;
}
public String generateRandomFilename() {
String RandomFilename = "";
Random rand = new Random();
int random = rand.nextInt();
Calendar calCurrent = Calendar.getInstance();
int intDay = calCurrent.get(Calendar.DATE);
int intMonth = calCurrent.get(Calendar.MONTH) + 1;
int intYear = calCurrent.get(Calendar.YEAR);
String now = intYear + "_" + intMonth + "_" +
intDay + "_";
RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random);
return RandomFilename;
}
private void writeExcel(OutputStream outputStream, Class<?> clazz, List<?> datalist,
ExcelTypeEnum excelType, String sheetName) throws IOException {
EasyExcel.write(outputStream, clazz).excelType(excelType).sheet(sheetName == null ?
"sheet1" : sheetName).doWrite(datalist);
outputStream.flush();
}
private void writeExcel(File newFile, Class<?> clazz, List<?> datalist,
ExcelTypeEnum excelType) {
EasyExcel.write(newFile, clazz).excelType(excelType).sheet("sheet1").doWrite(datalist);
}
private List<?> readExcel(InputStream inputStream, Class<?> clazz, ReadListener<?> listener) {
List<?> list = null;
list = EasyExcel.read(inputStream, clazz, listener).sheet().doReadSync();
return list;
}
}