人生如梦 荣华富贵 如木槿之花 朝荣夕逝
需求
- 导出: 能够将库表内的数据导出多个Excel表,并且生成一个压缩包,提供用户下载
- 导入: 能够将一个压缩包内的多个Excel表解压,并获取表内的所有数据
FileUtils 工具类
public class UnitFileUtils {/*** @Description: 导入文件(返回文件全部数据)*/public static Map<String, List<List<String>>> importCsv(File file) throws IOException {String tableName = file.getName().replace(".csv", "");Map<String, List<List<String>>> result = new HashMap<>();List<List<String>> dataList = new ArrayList<>();BufferedReader br = null;try {br = new BufferedReader(new FileReader(file));String line;while ((line = br.readLine()) != null) {String[] data = line.split(",");List<String> list = new ArrayList<>();for (int i = 0; i < data.length; i++) {list.add(data[i]);}dataList.add(list);}} catch (Exception e) {throw new RuntimeException(e);} finally {br.close();}result.put(tableName, dataList);return result;}/*** @Description: 导出文件*/public static void exportCsv(List<List<String>> listData, String tableName, String path) {path = path + File.separator + tableName + ".csv";try (FileWriter writer = new FileWriter(path)) {for (List<String> list : listData) {StringBuilder rowData = new StringBuilder();for (int i = 0; i < list.size(); i++) {if (i == list.size()) {rowData.append(list.get(i));} else {rowData.append(list.get(i)).append(",");}}writer.write(rowData.toString());writer.write(System.lineSeparator());}} catch (IOException ex) {throw new RuntimeException(ex);}}
}
Impl 实现类
/*** @Description: 文件导入* multipartFile 文件*/@Overridepublic void importCsv(MultipartFile multipartFile) throws IOException, SQLException {// 获取导入文件数据List<Map<String, List<List<String>>>> fileDataList = transformImportCsv(multipartFile);.../*** @Description: 文件数据解析*/public List<Map<String, List<List<String>>>> transformImportCsv(MultipartFile multipartFile) throws IOException, SQLException {// 获取临时文件夹对象File tempDir = new File(tempPath);// 如果文件夹不存在,则抛出异常if (!tempDir.exists()) {tempDir.mkdirs();}//生成临时文件名称String fileName = UUID.randomUUID().toString().replace("-", "");//通过multipartFile将压缩文件保存到临时路径String temp = FileUtils.saveZipFile(multipartFile, tempPath);//解压zipFileUtils.unzip(temp, tempPath + fileName);//读取解压文件File folder = new File(tempPath + fileName);File[] files = folder.listFiles();List<Map<String, List<List<String>>>> fileList = new ArrayList<>();Map<String, List<List<String>>> fileData = null;for (File file : files) {//遍历zip中的csv文件fileData = UnitFileUtils.importCsv(file);fileList.add(fileData);}//删除临时文件夹以及压缩文件FileUtils.deleteFileOrDir(temp);FileUtils.deleteFileOrDir(tempPath + fileName);return fileList;}/*** @Description: 导出文件*/public void exportCsv(HttpServletResponse response, List<String> tableNames) {String dirName = tempPath + File.separator + UUID.randomUUID().toString().replace("-", "");// 获取临时文件夹对象File tempDir = new File(dirName);// 如果文件夹不存在,则创建if (!tempDir.exists()) {tempDir.mkdirs();}//导出csv文件到指定文件夹for (String tableName : tableNames) {try {List<List<String>> listData = new ArrayList<>();...UnitFileUtils.exportCsv(listData, "unit_sharding_version", path);} catch (Exception e) {throw new RuntimeException("导出表【" + tableName + "】失败!",e);}}//将文件夹压缩为压缩包FileUtils.zip(dirName, dirName + ".zip");//通过流返回FileUtils.doGet(dirName + ".zip", response);//删除临时文件夹以及压缩文件FileUtils.deleteFileOrDir(dirName);FileUtils.deleteFileOrDir(dirName + ".zip");}