Java导出加密的CSV文件

将页面查询(分页查询)的数据导出为csv文件,主要问题还是文件的写入。(这里是生成zip压缩文件,输入的密码才能打开或解压文件)

工具类生成文件

public class CSVFileDownloadView extends AbstractFileDownloadView{
	@Override
	protected InputStream getInputStream(Map<String, Object> model, HttpServletRequest request) throws IOException{
		InputStream in;
		//文件名
		String fileName = (String) model.get("fileName ");
		//文件内容
		String fileContent= (String) model.get("fileContent");
		ByteArrayOutputStream inMemoryOutputStream = new ByteArrayOutputStream();
		//Zip压缩文件(加密)生成
		ZipOutputStream zos = new ZipOutputStream(inMemoryOutputStream );
		// 密码
		String zipPassword = (String) model.get("zipPassword ");
		try {
			ZipParameters parameters = new ZipParameters();
			parameters .setCompressionMethod(Zip4jConstans.COMP_DEFLATE);
			parameters .setCompressionLevel(Zip4jConstans.DEFLATE_LEVEL_NORMAL);
			parameters .setFileNameINZip(fileName + ".zip");
			parameters .setSourceExternalStream(true);
			parameters .setEncryptFiles(true);
			parameters .setEncryptionMethod(Zip4jConstans.ENC_METHOD_STANDARD);
			parameters .setPassword(zipPassword);
			zos.putNextEntry(null, parameters);
			//数据写入
			zos.write(fileContent.getBytes("MS932"));
			zos.closeEntry();
			zos.finish();
			zos.close();
			in = new ByteArrayOutputStream(inMemoryOutputStream.toByteArray);
		} catch (ZipException e) {
			throw new IOException(StringUtils.EMPTY, e);
		} finally {
			IOUtils.closeQuietly(zos);
			IOUtils.closeQuietly(inMemoryOutputStream);
		}
		return in;
	}
	
	@Override
	protected void addResponseHeader(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response){
		String fileName = (String) model.get("fileName ");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-Dispostion", "attachment;" + "filename="+ fileName + ".zip");
		response.setContentType("multipart/form-data");
	}
}

工具类写好,在spring-mvc.xml中配置一下

<bean id="csvFileDownloadView" class="com.jiexlcommon.app.view.CSVFileDownloadView">

OK,接下来是控制层Controller

@RequestMapping(value = "csvOutput", method = "RequestMethod.GET")
public String csvOutput(Form form, @pageableDefault(size = 10, page = 0) Pageable pageable, Model model){
	// 检索条件
	xxInputBean input = new xxInputBean();
	mapper.map(form, input);
	input.setPageable(pageable);
	// 通过调用service返回文件名,文件数据内容
	xxOutputBean output = xxService.csvFileOutput(input);
	// 设置文件名(自定义)
	model.addAttribute("fileName", output.getFileName);
	// (Stiring)fileContent为查询出的数据(在service编辑)
	model.addAttribute("fileContent", output.getFileContent);
	// 设置密码(页面输入)
	model.addAttribute("zipPassword", form.getEnCodePassword);
	return "csvFileDownloadView";
}

业务层主要是将查询数据按照csv文件要求的格式做编辑,然后返回给controller,这里就不多说了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.猫的树

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值