在action中进行文件下载,下载时运行不报错,可是也不下载

一:在写前端下载页面时,使用ajax方式调用action中的方法,然后就将下载内容返回js中了,所以没有下载,之后改为使用Windows.location进行下载,就没有问题了。

 

 

action中代码:

 

 if(decode = null ll decode. length = 0){
log.warn("****无下载内容**)
 throw new Exception();
}
 String filename =contractNo+".pdf";
/设置文件名的编码
 if(request.getHeader("user-agent "). toLowerCase().contains("msie")){
 filename URLEncoder. encode(filename,"utf-8");//将不安全的文件名改为UTF-8格式 
}else{
 filename new String(filename. getBytes("utf-8"),"iso-8859-1");/火狐浏览器
}
//告知浏览器要下载文件
 response. setHeader("content-disposition","attachment; filename="+filename); response.setHeader("content-type,"application/pdf");
 response setCharacterEncoding("utf-8)告知服务器使用什么编码
//创建一个文件输出流
 out response getOutputStream():
 out.write(decode);
 }catch(Exception e){
log.warn("****下载合同结束异******");
 throw e;
 finally {
 if (out != null){
 try {
 out.close();
 catch(Exception e){
 throw e;
}

  

 

js中代码:

 

 

/*下载*/
 function cfContractsignDownload(){
 if($('#cfContractsignList')find".selected").length!== 1){
 showMsg('请选择一行记录','warning');
 return;
}
 var rowData=$('#cfContractsignList').DataTable().rows(". selected").data()[0];//已经获取数据 
if(rowData. contractNo != undefined & rowData. contractNo !=="&& rowData. contractNo !== null){ var url= contextPath +"/cfContractsign/cfContractsignDownload",
 var data={
 contractNo:rowData.contractNo,
 sourceType: rowData. sourceType,
 consumerId: rowData. consumerId
}
 //window.location=ur1;//请求不带参数
 window.location=url+"?"+$.pram(data);//请求带参数
 Else{
);
 showMsgDuringTime(合同编号为空,不能下载!)
}

  

 

文件下载资料:https://blog.csdn.net/feng2147685/article/details/80515371

 

 

二:js中使用xhr.open("get",url,true)

action中:

 

BufferedInputStream output = null;
		try {
			BeanResult data = cf12000008.downloadContract(cf12000008In);
			BaseResponse bresponse = data.getResponse();
			CF12000008Out cf12000008Out =(CF12000008Out) bresponse;
			String fileInfo = cf12000008Out.getFileInfo();
			
			String fileUrl = cf12000008Out.getFileUrl();
			response.setHeader("content-type", "application/pdf");
			out = response.getOutputStream();
			//判断下载内容
			if(BusiUtil.isNotNull(fileInfo)){
			decode = Base64Utils.decode(fileInfo);
			//创建一个文件输出流
			out.write(decode);
			}else if(BusiUtil.isNotNull(fileUrl)){		
				URL url = new URL(fileUrl);		
				URLConnection conn = url.openConnection();		
				in = conn.getInputStream();			
				output = new BufferedInputStream(in);
				byte[] buff = new byte[1024*10];		
				int len = 0;		
				while ((len=output.read(buff))>-1) {			
					out.write(buff,0,len);		
					}
			}else{
				response.setHeader("content-type", "text/plain");
				out.write(0);
			}

  

 

 

js中代码:

 

 

if($("#contractNo").val() !== "" && $("#contractNo").val() !== null && $("#contractNo").val() !== undefined){
	var url = contextPath + "/cfContractSign/cfContractSignDownload";
	 var xhr = new XMLHttpRequest();    	    
	 xhr.open("get", url +"?" +$.param(data), true);	    
	 xhr.responseType = "blob";	    
	 xhr.onload = function() {	    		        
		 if (this.status == 200) {	        	        	
			 var blob = this.response;	        	
			 if(blob.type == "text/xml"){	        		
				 parent.showMsgDuringTime("下载失败,请检查合同编号"); 
				 return false	        	
				 }else if(blob.type == "text/plain"){
					 parent.showMsgDuringTime("下载失败,后台返回为空"); 
					 return false
				 }	    		
			 var fileName = $("#contractNo").val()+".pdf";	    		
			 if(window.navigator.msSaveOrOpenBlob){			
				 // IE浏览器下	    			
				 navigator.msSaveBlob(blob, fileName);	    		
				 } else {	    			
					 var  link = document.createElement("a");	    			
					 link.href = window.URL.createObjectURL(blob);	    			
					 link.download = fileName;	    			
					 link.click();	    			
					 window.URL.revokeObjectURL(link.href);
					 parent.showMsgDuringTime("下载成功");
					 contractAddCancel();
					 }	       
			 }else{	    	   
				 showMsg('请求错误', 'warning');       
				 }	   
		 }	   
	 xhr.onloadend = function(res){		   		   
		 
	 }	   
	 xhr.send();
	}

  

posted @ 2019-04-29 15:47  4小辣椒1  阅读(582)  评论(0编辑  收藏  举报