Ext实现文件下载

一条大红龙 2013-11-04 05:37:51
我想用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());


我预想是表单提交后,生成文件,传到前台,前台自动会弹出下载框,但是现在根本没反应,求各位大神帮帮忙,在线等啊
...全文
264 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
引用 12 楼 rui888 的回复:
ajax 是可以的。success 后。重新打开的方式实现
重新打开方式的话,是用window.open方式么?这种方式的话新 弹出一个网页,这样应该怎么做呢? 可以贴出来么? 主要我是用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里应该怎么写?
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
搞定了 ,是因为ext表单提交默认为ajax提交,所以不会弹出下载框 在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'
	                        });
	        			}
	        		})
	              }
贴出来后希望对别人有帮助,我真是被搞死了
tony4geek 2013-11-05
  • 打赏
  • 举报
回复
你试试直接链接url下载看看可以不?
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
刚才我查了一下,ext的form.submit是以Ajax方式提交的,是不是这种方式不会弹出下载框?
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
引用 8 楼 rui888 的回复:
//你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();  
我现在改成aaa.xls,前台还是不会弹出下载框 感觉文件内容已经放到response里了,但是前台有问题
tony4geek 2013-11-05
  • 打赏
  • 举报
回复
//你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();  
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
引用 6 楼 rui888 的回复:
你后台进去了没。
进去了 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里有很长一串乱码的东西
tony4geek 2013-11-05
  • 打赏
  • 举报
回复
你后台进去了没。
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
引用 4 楼 sunbo624 的回复:
location.href = 你的URL 就行
这个location.href 在js具体怎么写?
sunbo624 2013-11-05
  • 打赏
  • 举报
回复
location.href = 你的URL 就行
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
求救……各位大神
一条大红龙 2013-11-05
  • 打赏
  • 举报
回复
不知道楼主解决没有,我的是因为我生成excel时,没有记录时生成文件有问题导致的,楼主加油咯
一条大红龙 2013-11-04
  • 打赏
  • 举报
回复
真心没人么??
一条大红龙 2013-11-04
  • 打赏
  • 举报
回复
没人么??求救

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧