我想用servlet 和ext实现文件下载,
js代码为
items: ['->',{
text: '导出',
handler: function(){
Ext.MessageBox.confirm('提示','确定要导出网点吗?',function(btn){
var requestObj = gridFormPanel.form.getValues();
if (btn == "yes") {
var form = gridFormPanel.form;
var requestObj = gridFormPanel.form.getValues();
form.submit({
waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST'
}
})
}
}]
后台servlet代码是
String fileName = result.getString("path");
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
ServletOutputStream sos = response.getOutputStream();
// PrintWriter writer = response.getWriter();
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fileName));
byte[] content = new byte[1024];
int length;
while ((length = fin.read(content, 0, content.length)) != -1) {
sos.write(content, 0, length);
}
fin.close();
sos.flush();
sos.close();
// writeResponse(response, result.toString());
我预想是表单提交后,生成文件,传到前台,前台自动会弹出下载框,但是现在根本没反应,求各位大神帮帮忙,在线等啊
java?ext
------解决方案--------------------
location.href = 你的URL 就行
------解决方案--------------------
//你excel 名字先用个英文的试试看。
resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename=file.xls");
OutputStream os = resp.getOutputStream();
FileInputStream in = new FileInputStream(new File("xxxxx"));
int n = 0;// 每次读取的字节长度
byte[] bb = new byte[1024];// 存储每次读取的内容
while ((n = in.read(bb)) != -1) {
os.write(bb, 0, n);// 将读取的内容,写入到输出流当中
}
os.close();// 关闭输入输出流
in.close();