下载附件谷歌浏览器报:ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION

//修改前webAction代码
package com.web.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class FileDownload implements Action {

private String fileName;// 初始的通过param指定的文件名属性
private String inputPath;// 指定要被下载的文件路径

public InputStream getInputStream() throws Exception {
// 通过 ServletContext,也就是application 来读取数据


ServletActionContext.getResponse().addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(getDownloadFileName(),"utf-8"));
//return ServletActionContext.getServletContext().getResourceAsStream("/upload"+"/"+
//inputPath+"/"+getDownloadFileName());
//return ServletActionContext.getServletContext().getResourceAsStream("/upload"+"/"+
//inputPath+"/"+getDownloadFileName());



InputStream iStream=new FileInputStream("D:/ftoa/upload/"+inputPath+"/"+getDownloadFileName());
//InputStream iStream=new FileInputStream( ServletActionContext.getRequest().getRealPath("externalOper/files")+"/"+getDownloadFileName());

return iStream;
//String dstPath ="d:/upload/" + this.getSavePath()+ "/" + tempUrl;
}

public void setInputPath(String value) {
inputPath = value;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

/** 提供转换编码后的供下载用的文件名 */
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}

public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}

}
//--------------------------------------------
//修改前Strut2配置
<action name="fileDownload" class="fileDownload">
<param name="inputPath">/各类地址.txt</param>
<!-- 初始文件名 -->
<param name="fileName">各类地址.txt</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream;</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
//----------------------------------
//以上做法只有IE支持,360,谷歌都不支持附件下载。
//谷歌浏览报:
//收到了来自服务器的重复标头

//隐藏详细信息
//来自服务器的响应包含重复标头。此问题通常是由于网站或代理配置不正确导致的。
//只有网站或代理管理员才能解决此问题。
//错误代码:ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
//----------------------------------------
//修改后:javaAction代码
package com.web.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


import com.opensymphony.xwork2.Action;

public class FileDownload implements Action {

private String fileName;// 初始的通过param指定的文件名属性
private String inputPath;// 指定要被下载的文件路径

public InputStream getInputStream() throws Exception {
// 通过 ServletContext,也就是application 来读取数据


// ServletActionContext.getResponse().addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(getDownloadFileName(),"utf-8"));
//return ServletActionContext.getServletContext().getResourceAsStream("/upload"+"/"+
//inputPath+"/"+getDownloadFileName());
//return ServletActionContext.getServletContext().getResourceAsStream("/upload"+"/"+
//inputPath+"/"+getDownloadFileName());



InputStream iStream=new FileInputStream("D:/ftoa/upload/"+inputPath+"/"+getDownloadFileName());
//InputStream iStream=new FileInputStream( ServletActionContext.getRequest().getRealPath("externalOper/files")+"/"+getDownloadFileName());

return iStream;
//String dstPath ="d:/upload/" + this.getSavePath()+ "/" + tempUrl;
}

public void setInputPath(String value) {
inputPath = value;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

/** 提供转换编码后的供下载用的文件名 */
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}

public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}

public String getFileName() throws UnsupportedEncodingException {
return URLEncoder.encode(getDownloadFileName(),"utf-8");
}


}
//------------------------------------------------------------
//-------struts2配置----
<action name="fileDownload" class="fileDownload">
<param name="inputPath">/各类地址.txt</param>
<!-- 初始文件名 -->
<param name="fileName">各类地址.txt</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream;</param>
<param name="inputName">inputStream</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">4096</param>
</result>
</action>
-----------------------------------
/ //经测试谷歌,IE,360浏览器都支持
---------------jsp代码-------------------------
[url=javascript:download('<s:property value=]','<s:property value="#f[0]" />')"><s:property value="#f[0]" />[/url];
----------------------------
<form action="fileDownload" name="downloadfrom" >
<input type="hidden" name="inputPath" value="" />
<input type="hidden" name="fileName" value="" />
</form>
-------------js代码--------------------------
function download(fileurl,filename){
document.forms["downloadfrom"].inputPath.value=fileurl;
document.forms["downloadfrom"].fileName.value=filename;
document.forms["downloadfrom"].submit();
}
-----------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值