当我在我的应用程序中添加数据时,它会保存在CSV文件中。我还有一个选项可以下载相同的CSV文件。但它给出了错误。 我的代码是:
String csvPath = Utility.getTestCSVFilePath();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=\"" +test.getTestName()+".csv"+ "\"");
PrintWriter responseOut = null;
try {
responseOut = response.getWriter();
InputStream inputStream = new FileInputStream(csvPath);
OutputStream out = response.getOutputStream();
IOUtils.copy(inputStream, out);
out.flush();
out.close();
inputStream.close();
} catch (IOException e) {
responseOut.write("Some error has occurred while preparing the file for download.");
}
当我调试代码时,它会在行中出错:
OutputStream out = response.getOutputStream();
问题是什么,请帮忙。
答案 0 :(得分:0)
尝试移动此行
responseOut = response.getWriter();
后
inputStream.close();
并查看是否有效