public static <T> void readFromTempCsvFile(String filename) throws IOException {
// 这里需要设置不关闭流
String filePath=tempFilePath+ filename +".csv";
File file=new File(filePath);
//进行文件读取配置
CsvReadConfig csvReadConfig = new CsvReadConfig();
csvReadConfig.setSkipEmptyRows(true);
csvReadConfig.setContainsHeader(true);
//构建 CsvReader 对象
CsvReader csvReader = CsvUtil.getReader(csvReadConfig);
// ArrayList<ExtPatientFile> patientList = (ArrayList<ExtPatientFile>) csvReader.read(new FileReader(file), ExtPatientFile.class);
// file.delete();
}
/**
* 下载csv文件
* @param list 需要导出的数据列表
* @param filename 文件名称
* @param <T> 数据元素类型
* @throws IOException
*/
public static <T> File saveToTempCsvFile(List<T> list, String filename, boolean isAppend) throws IOException {
//1. 通过传入的参数,和相关的业务代码逻辑处理,获取相应的数据.
//2. 将数据进行转换,转换成 List<User> 的形式.
//将数据进行下载.
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
File file = null;
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileNameStr = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
// 这里需要设置不关闭流
String filePath = tempFilePath + fileNameStr + ".csv";
file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
try {
//将数据,写入到 文件里面。 主要是这一行代码逻辑
CsvWriter writer = CsvUtil.getWriter(file, Charset.forName("UTF-8"), isAppend);
writer.writeBeans(list);
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return file;
}
/**
* 导出csv压缩文件
* @param fileName 压缩文件名称 例:aa.zip
* @param response
* @throws IOException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void exportCsvZip(List<String> fileNames, String fileName, HttpServletResponse response) throws IOException, IllegalArgumentException, IllegalAccessException{
OutputStream out = response.getOutputStream();
File zip = new File(tempFilePath + "export_treatment/" + fileName + ".zip"); // 压缩文件
File []srcfile = new File[fileNames.size()];
for (int i = 0, n = fileNames.size(); i < n; i++) {
srcfile[i] = new File(fileNames.get(i));
}
zipFiles(srcfile, zip);
FileInputStream inStream = new FileInputStream(zip);
byte[] buf = new byte[4096];
int readLength;
while ((readLength = inStream.read(buf)) != -1) {
out.write(buf, 0, readLength);
}
inStream.close();
deleteFile(fileNames, tempFilePath + "export_treatment/" + fileName + ".zip");
}
/**
* 文件删除
* @param fileNames
* @param zipPath
*/
public static void deleteFile(List<String> fileNames, String zipPath) {
String sPath = null;
File file = null;
boolean flag = false;
try {
// 判断目录或文件是否存在
for (int i = 0; i < fileNames.size(); i++) {
sPath = fileNames.get(i);
file = new File(sPath);
if (file.exists()) {
file.delete();
}
}
file = new File(zipPath);
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置响应头
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(fileName, "UTF-8")
+ ".zip");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
log.error("e", ex);
}
}
网友评论