突然想记录一下这段代码,自己便写了个简单的demo及压缩工具类(一般不会说工具类我是在网上拷贝后重新修改了一下,哈哈哈),使用起来比较方便,以后导出csv或zip格式文件都可用,先写如何导出csv , 后边则是csv+图片导出zip
package com.hutool.test;
import cn.hutool.core.io.FileUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
/**
* @author [email protected]
* @date 2019/1/25 17:25
* @description:
*/
@RestController
@RequestMapping("/test")
public class DataExportTest {
/**
* CSV导出表头信息
*/
private static final List HEADS = Arrays.asList("测试名称", "测试数", "测试内容");
@GetMapping("/exportcsv")
public String export(HttpServletResponse response) {
List> dataMaps = new ArrayList<>();
List
单独导出csv文件工具类,里面有注释就单独解释了
package com.hutool.test;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author xqli7
** @date 2019/1/25 17:25
*/
public class CsvUtils {
private CsvUtils() {
}
private static final Logger LOG = LoggerFactory.getLogger(CsvUtils.class);
/**
* 读取CSV文件内容
*
* @param file 传入的CSV文件
* @return 读取的数据集合
*/
public static List readCsv(File file, boolean isNoRepeatData) {
try (
DataInputStream in = new DataInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"))
) {
//将第一行表头先读出,后面line读入的全部为数据内容
String firstLine = reader.readLine();
LOG.info("head:{}", firstLine);
String line;
List data = new ArrayList<>();
Set lines = new HashSet<>();
while ((line = reader.readLine()) != null) {
//未设置重复过滤 或者 设置重复过滤且未重复
//limit -1 防止无数据读取报角标越界异常
String[] item = line.split(",", -1);
if (isNoRepeatData) {
if (!lines.contains(line)) {
data.add(item);
lines.add(line);
}
} else {
data.add(item);
}
}
return data;
} catch (Exception e) {
LOG.error("读取csv文件出错", e);
return null;
}
}
/**
* 将CSV格式数据写入指定CSV文件
*
* @param filePath CSV文件写入的全路径
* @param heads 要导入的数据表头
* @param dataList 要导入的数据内容
* @Param fileName 导出时浏览器中展示的文件名
* @Param response HttpServlet响应
*/
public static void exportCsv(String filePath, List heads, List> dataList) {
if (StringUtils.isEmpty(filePath) || heads.isEmpty()) {
return;
}
LOG.debug("开始生成本地csv,filePath:{}", filePath);
long start = System.currentTimeMillis();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "GBK"))) {
StringBuilder stringBuilder = new StringBuilder();
heads.forEach(value ->
stringBuilder.append(value).append(',')
);
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
writer.write(stringBuilder.toString());
writer.flush();
for (List
以下代码是导出zip格式的,我是csv+图片,如果有其他需要的可以将图片更改为其他文件即可;
package com.hutool.test;
import cn.hutool.core.io.FileUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
/**
* @author [email protected]
* @date 2019/1/23 16:14
* @description:
*/
@RestController
@RequestMapping("/test")
public class DataExportTest {
/**
* CSV导出表头信息
*/
private static final String HEAD = "测试名称" + "," + "测试数" + "," + "测试内容" + ",";
/**
* 导出zip包名称
*/
private static final String DESCRIBE = "导出测试";
@GetMapping("/exportZip")
public String writeFileToZip(HttpServletResponse response) {
//获取数据(数据库数据)
List
以下是压缩zip及解压zip的工具类,可直接使用,不过建议还是需要看看的,不然像我一样第一次写找错还得找半天。。哈哈哈
package com.hutool.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* @author xqli7
* @date 2019/1/23 16:14
*/
public final class ZipUtils {
private static final Logger LOG = LoggerFactory.getLogger(ZipUtils.class);
private static final int BUFFER = 2048;
private static final int TRANS_BUFFER = 10240;
private ZipUtils() {
throw new IllegalStateException("Utility class");
}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件并存放到zipFilePath路径下
*
* @param sourceFilePath 待压缩的文件路径
* @param zipFilePath 压缩后存放路径
* @param fileName 压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
boolean flag = false;
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
LOG.info("待压缩的文件目录:{}不存在.", sourceFilePath);
sourceFile.mkdir();
}
File zipFile = new File(zipFilePath + File.separator + fileName + ".zip");
if (zipFile.exists()) {
LOG.info("{}目录下存在名字为:{}.zip打包文件", zipFilePath, fileName);
} else {
File[] sourceFiles = sourceFile.listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
LOG.info("待压缩的文件目录:{}里面不存在文件,无需压缩.", sourceFilePath);
} else {
try (
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))
) {
byte[] bytes = new byte[TRANS_BUFFER];
loopCreateZip(sourceFiles, zos, bytes);
flag = true;
} catch (Exception e) {
LOG.error("", e);
}
}
}
return flag;
}
private static void loopCreateZip(File[] sourceFiles, ZipOutputStream zos, byte[] bytes) throws IOException {
for (int i = 0; i < sourceFiles.length; i++) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
try (
FileInputStream fis = new FileInputStream(sourceFiles[i]);
BufferedInputStream bis = new BufferedInputStream(fis, TRANS_BUFFER)
) {
int read = 0;
while ((read = bis.read(bytes, 0, TRANS_BUFFER)) != -1) {
zos.write(bytes, 0, read);
}
} catch (IOException e) {
LOG.error("", e);
}
}
}
/**
* 读取zip包中的文本文件以及文件内容
*
* @param filePath
* @return
* @throws IOException
*/
public static boolean readZipFile(String filePath) {
File sourceFile = new File(filePath);
if (!sourceFile.exists()) {
LOG.info("待读取的文件:{}不存在.", filePath);
return false;
}
try (
FileInputStream fis = new FileInputStream(sourceFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
LOG.info("Extracting:{} ", entry);
// write the files to the disk
write(entry, zis);
}
} catch (Exception e) {
LOG.error("", e);
}
return true;
}
private static void write(ZipEntry entry, ZipInputStream zis) {
int count;
byte[] data = new byte[BUFFER];
try (
BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(entry.getName()), BUFFER)
) {
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
} catch (Exception e) {
LOG.error("", e);
}
}
/**
* 对zip文件进行解压
*
* @param sourcePath 解压文件路径
* @param targetDir 解压目标地址
* @return
*/
@SuppressWarnings("unchecked")
public static List unzip(String sourcePath, String targetDir) {
List files = new ArrayList<>();
File targetDirFile = new File(targetDir);
if (!Files.exists(targetDirFile.toPath())) {
targetDirFile.mkdir();
}
File file = new File(sourcePath);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file, Charset.forName("GBK"));
ZipEntry entry;
File entryFile;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
Enumeration entries = (Enumeration) zipFile.entries();
while (entries.hasMoreElements()) {
entry = entries.nextElement();
if (entry.isDirectory()) {
return null;
}
entryFile = new File(targetDir + File.separator + entry.getName());
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryFile));
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry))
) {
int length;
while ((length = bis.read(buffer, 0, bufferSize)) != -1) {
bos.write(buffer, 0, length);
}
bos.flush();
files.add(entryFile);
} catch (Exception e) {
LOG.error("文件读取出错", e);
return null;
}
}
return files;
} catch (IOException e) {
LOG.error("zip文件读取错误", e);
return null;
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException e) {
LOG.error("流关闭异常", e);
}
}
}
}
启动后直接访问浏览器访问:
http://localhost:8080/test/exportcsv
http://localhost:8080/test/exportZip
ok 导出成功