如何控制客户端浏览器打开文件另存为对话框?
用户要定制下载数据库中的数据,用户提交定制条件,后台再数据库中进行查询,然后再服务器上生成文件,不把文件地址给用户显示出来,也不让用户再做操作,用户提交了定制条件后,后台操作完后就直接再客户端打开文件另存为对话框,怎么做?
eg:已打开excel为例
//访问数据库,获得需要下载的文件,eg: 0508.xls
String filenameForSave = "abc.xls";
//指定使用excel打开
response.setContentType("application/msexcel");
//filename为文件打开时,你希望它显示的title
response.setHeader("Content-disposition","attachment;filename=abc.xls");
BufferedInputStream bufferInput = null;
BufferedOutputStream bufferOutput = null;
try {
//upload为工程下的一个文件夹
bufferInput = new BufferedInputStream(new FileInputStream(servlet.getServletContext().getRealPath("upload\\" + filenameForSave)));
bufferOutput = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bufferInput.read(buff, 0, buff.length))) {
bufferOutput.write(buff,0,bytesRead);
}
} catch(final IOException e) {
e.printStackTrace();
forward = mapping.findForward("error");
} finally {
if (bufferInput != null)
bufferInput.close();
if (bufferOutput != null)
bufferOutput.close();
}