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);
? ? ? ? }
? ? }