package com.business.testExcelPort;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/download")
@Slf4j
public class DownLoad {
private Logger Log = Logger.getLogger(DownLoad.class);
private static final String FilePath = System.getProperty("java.io.tmpdir") + File.separator;
@RequestMapping(value = "/execute", method=RequestMethod.GET)
public void execute(HttpServletResponse response) {
List<String> filePaths = new ArrayList<>();
String tmpFileName = "Demo.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";
fileList.add(creatFile(filename));
filePaths.add(filename);
List<DataOne> list = new ArrayList<>();
for (int j = 0; j < 10; j++) {
DataOne dataOne = new DataOne();
list.add(dataOne);
}
writeExcel(newFile, DataOne.class, list,ExcelTypeEnum.XLS);
}
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 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 = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" +
String.valueOf(intDay) + "_";
RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random);
return RandomFilename;
}
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;
}
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;
};
}