在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();
}
在action中进行文件下载,下载时运行不报错,可是也不下载的更多相关文章
- 在eclipse动态网页项目中,编写web.xml时,servlet标签报错.
cvc-complex-type.2.4.b: The content of element 'servlet' is not complete. One of '{"http:// jav ...
- homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题
homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题 1. 在虚拟器运行 npm 下载依赖组件时报错: npm ERR! EPROTO: protocol err ...
- pytest——pycharm中右击运行(run)没有问题,在terminal中运行pytest报错:E ModuleNotFoundError: No module named
参考了这个解决办法:https://blog.csdn.net/qq_36829091/article/details/82180866 我的是Windows,linux的和Windows的解决办法有 ...
- vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
- php中读取中文文件夹及文件报错
php读取时出现中文乱码 一般php输出中出现中文乱码我们可用 header ('content:text/html;charset="utf-8"'); php中读取中文文件夹及 ...
- soamanager发布的Webservice服务,调用时出现http500报错
最近再给薪酬那边发布ws服务时出现了报错,调用方反馈了errorCode:BEA-380002.在使用XMLspy工具去调用这个WSDL时候,则反馈http500的错误消息.如下图: 遇到这种问题我通 ...
- 关于使用stanfordcorenlp一直运行不报错的解决方法
一.问题描述: 最近在使用stanfordcorenlp时,遇到了我在运行时代码不报错但同时也没有结果的问题,等了很久也没有出结果.其实是很简单的一个步骤,但却花了好几天的时间都没有成功!网上更多的是 ...
- python3写入文件时编码问题报错
在字符串写入文件时,有时会因编码问题导致无法写入,可在open方法中指定encoding参数 chfile = open(filename, 'w', encoding='utf-8') 这样可解决大 ...
- javascript的倒计时功能中newData().getTime()在iOS下会报错问题解决
javascript的倒计时功能中newData().getTime()在iOS下会报错问题解决 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间,而在i ...
随机推荐
- Google搜索中的突变XSS-JavaScript Library Introduced XSS Flaw in Google Search
前言2018年9月26日,开源Closure库(最初由谷歌创建并用于谷歌搜索)的一名开发人员创建了一个提交,删除了部分输入过滤.据推测,这是因为开发人员在用户界面设计方面出现了问题.但此次提交的开发人 ...
- LeetCode-7-反转整数-c# 版本
c# 版本 // 给定一个 32 位有符号整数,将整数中的数字进行反转. public class Solution { public int Reverse(int x) { / // 边界判断 / ...
- spring boot集成aop实现日志记录
1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- flask 虚拟换将安装
1.1. 1.4虚拟环境的安装 1.1.1. 安装虚拟环境 1.1.1.1. 安装 注意:这里必须使用pip3 安装. sudo pip3 install virtualenv sudo pip3 ...
- 命名空间"xx"已经包含了"xx"的定义
例: namespace A.B { public class C { } } 注:重名的不仅仅是类,还可以结构,枚举,命名空间本身也有可能重复. 这个类C若与命名 ...
- 福州大学软件工程1916 | W班 作业成绩排名汇总
评分链接: 第一次作业-准备篇 https://www.cnblogs.com/deerCode/p/10527237.html 第二次作业-结对第一次-文献摘要热词统计原型设计 https://ww ...
- Hadoop HDFS 用java API 进行读写
public class HdfsApp { public static FileSystem getFileSystem() throws Exception { Configuration con ...
- MQ知识点汇总
1. MQ是什么 2. MQ能做什么 3. 消息模式 4. 使用MQ的时候需要注意什么 5. 常用MQ 6. MQ的不足 7. 什么时候不适用MQ 8. MQ的组成 9. MQ的关注点 1. MQ是什 ...
- select样式重置
div{ //用div的样式代替select的样式 width: 200px; ...
- Qt信号槽第5个参数
1.Qt::AutoConnection: 默认值,使用这个值则连接类型会在信号发送时决定.如果接收者和发送者在同一个线程,则自动使用Qt::DirectConnection类型.如果接收者和发送者不 ...