下载附件谷歌浏览器报: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(); 
}
-----------------------------------------------

你可能感兴趣的:(java下载文件实例,浏览器下载不兼容)