`

不导包,压缩出力csv文件

    博客分类:
  • java
阅读更多

最近研究压缩csv文件出力,根据网上资料,整理了一套用不导包的例子。

 

引包

 

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.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

 

 

调用

//临时csv作成
File csvFile1 = CsvCreator.out(entities);
String url2 = csvFile1.getPath();
url2 = url2.substring(0, url2.length() - 3);
fileChannelCopy(csvFile1.getPath(), url2 + ".csv");

// ZIP压缩包
zip(url + ".zip", new File(url + ".csv"));
StringBuilder uri = new StringBuilder();
uri.append(url);
uri.append(".zip");

// 下载
downloadFile(request, response, uri);

// 删除临时文件
deleteFile(url + ".csv");
deleteFile(url + ".zip");

 文件复制

 

	private void fileChannelCopy(String ChangeBeforeName, String ChangeAfterName) {
		File s = new File(ChangeBeforeName);
		File t = new File(ChangeAfterName);

		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fi = new FileInputStream(s);
			fo = new FileOutputStream(t);
			in = fi.getChannel();
			out = fo.getChannel();
			in.transferTo(0, in.size(), out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 压缩文件

 

	private void zip(String zipFileName, File inputFile) throws Exception {
		System.out.println("Start...");
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		BufferedOutputStream bo = new BufferedOutputStream(out);
		zip(out, inputFile, inputFile.getName(), bo);
		bo.close();
		out.close();
		System.out.println("End...");
	}

	private void zip(ZipOutputStream out, File f, String base,
			BufferedOutputStream bo) throws Exception {
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			if (fl.length == 0) {
				out.putNextEntry(new ZipEntry(base + "/"));
				System.out.println(base + "/");
			}
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + "/" + fl[i].getName(), bo);
			}
		} else {
			out.putNextEntry(new ZipEntry(base));
			System.out.println(base);
			FileInputStream in = new FileInputStream(f);
			BufferedInputStream bi = new BufferedInputStream(in);
			int b;
			while ((b = bi.read()) != -1) {
				bo.write(b);
			}
			bi.close();
			in.close();
		}
	}

 下载

 

 

	private void downloadFile(HttpServletRequest request,
			HttpServletResponse response, StringBuilder uri) throws IOException {
		StringBuffer filename = new StringBuffer();
		filename.append(uri);
		File file = new File(filename.toString());
		StringBuffer sb = new StringBuffer();
		String name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
				+ ".zip";
		sb.append("attachment;  filename=").append(name);
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control",
				"must-revalidate, post-check=0, pre-check=0");
		response.setHeader("Pragma", "public");
		response.setContentType("application/x-msdownload;charset=UTF-8");
		response.setHeader("Content-Disposition", new String(sb.toString()
				.getBytes(), "Windows-31J"));

		FileInputStream inputStream = new FileInputStream(file);
		OutputStream outputStream = response.getOutputStream();
		byte[] buffer = new byte[1024];
		int i = -1;
		while ((i = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, i);
		}
		outputStream.flush();
		outputStream.close();
		inputStream.close();
	}

 删除临时文件

 

 

	private void deleteFile(String sPath) {
		File file = new File(sPath);
		if (file.isFile() && file.exists()) {
			file.delete();
		}
		return;
	}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics