【发布时间】:2017-11-15 07:46:38
【问题描述】:
当我尝试从 servlet 代码导出 csv 文件时,它显示了正确的记录,但最后它显示了额外的行。
示例:它需要 1000 行,但它显示 1041 行长,有 1000 条记录。
下面是我的代码和excel表格。
// for downloading csv
public void getDownloaded(String filepath, HttpServletResponse response) {
try {
File file = new File(filepath);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
FileInputStream in = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
// copy binary content to output stream
while (in.read(outputByte, 0, 4096) != -1) {
out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】:
-
您在本地服务器目录目录中看到
getCSVWrite方法生成的文件了吗?