我想用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
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
解决方案 »
- CXF 2.4 启动报错
- jsp牛人请进!!!!!用jsp做好登录界面,想在个人主页上添加修改个人信息功能!可是不知道怎么获得用户名!!!!在线求高人指点!
- 请教关于SpringSide3中filter查询的问题
- java中frame窗体变大的问题?
- 求SQL 查询相同号码的最新时间
- PreparedStatement的addBatch()和addBatch(String sql)
- 在MyEclipse中运行工程时出现提示:create,manager,and run configuration,该如何解决?
- hibernate连接oracle数据库出错!请问如何解决?谢谢
- 我是一个菜鸟。想请教大家几个问题。请给点建议。谢谢
- JAVA 定时器 调用重复问题
- response.getOutputStream();这样抛出文件流
- 数据库中的数据如何实现多语
response里也有值
form.submit({
waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST',
success : function(form, action) {
alert(action.response.responseText);
// var obj = Ext.JSON.decode(action.response.responseText);
// alert(obj.path);
// window.open(obj.path, '_blank',
// 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
},
failure : function(form, action) {
//window.open('test.html','newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
alert(action.response.responseText);
alert("failure");
}
});
这里直接进入了failure,responseText里有很长一串乱码的东西
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();
感觉文件内容已经放到response里了,但是前台有问题
在Ext 4.2里,我设置一下form表单属性var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true}),这样表单提交会以正常方式提交,就会弹出下载框了,搞了我2天了,心都肿了,现在贴出我的代码
String fileName = result.getString("path");
File file = new File("E://"+ fileName);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment; filename="+ fileName);
OutputStream out = response.getOutputStream();
BufferedInputStream buff = new BufferedInputStream(new FileInputStream(file));
byte[] content = new byte[1024];
long k = 0;
while(k<file.length()){
int j=buff.read(content,0,1024);
k+=j;
out.write(content,0,j); }
out.flush();
out.close();
buff.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
js代码
var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true}
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来做的,response写完文件流后,无法再返回json格式的数据
form.submit({
// waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST',
success : function(form, action) {
alert(action.result.msg);
alert(action.response.responseText);
// var obj = Ext.JSON.decode(action.response.responseText);
// alert(obj.path);
// window.open(obj.path, '_blank',
// 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
},
failure : function(form, action) {
alert(action.failureType);
alert(action.result.msg);
alert(action.response.responseText);
alert("failure");
}
});
所以不能设置success的值,直接返回到failure里了,然后在failure里应该怎么写?