java做导出功能,后台已经写好文件放置tomcat上,文件目录是:file/*.csv
请问前台的按导出按钮的时候怎么写?网上好了些例子都不行。求助。。万分感谢
------解决方案--------------------
这点需求,很简单呀
先设置 HttpServletResponse:
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=downloadfilename.csv");
然后下载之:
File file = new File("C:\\temp\\downloadfilename.csv");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();