Posted on 2012-02-06 10:49
H2O 閱讀(856)
評論(0) 編輯 收藏 所屬分類:
struts
引用:
http://blog.csdn.net/csh624366188/article/details/6695702 感謝原作者 本人僅作整理、分享。
方案一:

原來處理下載的代碼如下:
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
下載的程序里有了這句,一般在IE6的下載提示框上將正確顯示文件的名字,無論是簡體中文,還是日文。

一. 上面方式,也就是先用URLEncoder編碼,當中文文字超過17個時,IE6 無法下載文件。
這是IE的bug,參見微軟的知識庫文章 KB816868 。
原因可能是因為ie在處理 Response Header 的時候,對header的長度限制在150字節左右。
而一個漢字編碼成UTF-8是9個字節,那么17個字便是153個字節,所以便會報錯。
微軟提供了一個補丁。這個補丁需要先安裝ie6 sp1。

二. 我嘗試使用 javamail 的MimeUtility.encode()方法來編碼文件名,也就是編碼成 =?gb2312?B?xxxxxxxx?= 這樣的形式,
并從 RFC1522 中找到對應的標準支持。不過很遺憾,IE6并不支持這一個標準。
我試了一下,Firefox是支持的。

三. 按網上很多人提供的解決方案:將文件名編碼成ISO8859-1似乎是有效的解決方案,代碼如下:
response.setHeader( "Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO8859-1" ) );
在確保附件文件名都是簡體中文字的情況下,那么這個辦法確實是最有效的,不用讓客戶逐個的升級IE。
如果臺灣同胞用,把gb2312改成big5就行。但現在的系統通常都加入了國際化的支持,普遍使用UTF-8。
如果文件名中又有簡體中文字,又有繁體中文,還有日文。那么亂碼便產生了。
另外,在我的電腦上Firefox (v1.0-en)下載也是亂碼。

折中考慮,我結合了一、三的方式,代碼片斷如下:

String fileName = URLEncoder.encode(atta.getFileName(), "UTF-8");

/**//*
* see http://support.microsoft.com/default.aspx?kbid=816868
*/

if (fileName.length() > 150)
{

String guessCharset = xxxx /**//*根據request的locale 得出可能的編碼,中文操作系統通常是gb2312*/
fileName = new String(atta.getFileName().getBytes(guessCharset), "ISO8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
暫且不考慮 Firefox。
/////////////////////

下面是解決文件名空格問題

String fileName = StringUtils.trim(file.getName());

String formatFileName = encodingFileName(name);//在后面定義方法encodingFileName(String fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + formatFileName );

//處理文件名中出現的空格

//其中%20是空格在UTF-8下的編碼


public static String encodingFileName(String fileName)
{
String returnFileName = "";

try
{
returnFileName = URLEncoder.encode(fileName, "UTF-8");
returnFileName = StringUtils.replace(returnFileName, "+", "%20");

if (returnFileName.length() > 150)
{
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
returnFileName = StringUtils.replace(returnFileName, " ", "%20");
}

} catch (UnsupportedEncodingException e)
{
e.printStackTrace();

if (log.isWarnEnabled())
{
log.info("Don't support this encoding
");
}
}
return returnFileName;


方案二


Struts2下載文件實現的說明

contentType

內容類型,和互聯網MIME標準中的規定類型一致,例如text/plain代表純文本,text/xml表示XML,image/gif代表GIF圖片,image/jpeg代表JPG圖片

inputName

下載文件的來源流,對應著action類中某個類型為Inputstream的屬性名,例如取值為inputStream的屬性需要編寫getInputStream()方法

contentDisposition

文件下載的處理方式,包括內聯(inline)和附件(attachment)兩種方式,而附件方式會彈出文件保存對話框,否則瀏覽器會嘗試直接顯示文件。取值為:

attachment;filename="struts2.txt",表示文件下載的時候保存的名字應為struts2.txt。如果直接寫filename="struts2.txt",那么默認情況是代表inline,瀏覽器會嘗試自動打開它,等價于這樣的寫法:inline;filename="struts2.txt"

bufferSize

下載緩沖區的大小


contentType屬性和contentDisposition分別對應著HTTP響應中的頭Content-Type和Content-disposition頭。

如下例:

down.jsp

<a href="<s:url value='test/fileDown.do?fileName=struts2配置參數詳解.txt'> </s:url>">下載</a>

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="com" namespace="/test" extends="struts-default">
<action name="fileDown" class="it.com.down.FileDown">
<result 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>
</package>

</struts>

文件下載Action:


public class FileDown extends ActionSupport
{
private String fileName;


public String getFileName()
{
return fileName;
}


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

}


public InputStream getInputStream() throws UnsupportedEncodingException
{
return ServletActionContext.getServletContext().getResourceAsStream(
"/WEB-INF/" + fileName);
}


public String execute()
{
System.out.println(fileName+"----------");
return "success";
}

}


這個例子運行可,可能會出現:下載頁面的文件名為:fileDown.do或htm等情況。

這在實際保存時,要改成相應的文件名,在不知道原文件類型情況下就無法改了。

這種情況,可以這樣解決:

將struts.xml配置改為:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="com" namespace="/test" extends="struts-default">

<result type="stream">

<!-- 下載文件類型定義 -->
<param name="contentType">
application/octet-stream
</param>

<!-- 下載文件輸出流定義 -->
<param name="inputName">inputStream</param>
<!-- 下載文件的緩沖大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>

</struts>

文件下載action改為:


public class FileDown extends ActionSupport
{
private String fileName;


public String getFileName()
{
return fileName;
}


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

}


public InputStream getInputStream()
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment;fileName="+
fileName);

return ServletActionContext.getServletContext().getResourceAsStream(
"/WEB-INF/" + fileName);
}


public String execute()
{
System.out.println(fileName+"----------");
return "success";
}

}
這樣就行了。

如果下載文件名稱是中文,會出現亂碼現象,解決方案:

將上面action相應的改為如下兩步

(1)設置:

public void setFileName(String fileName) throws UnsupportedEncodingException
{
this.fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
}
(2)設置:
response.setHeader("Content-Disposition", "attachment;fileName="
+ java.net.URLEncoder.encode(fileName,"UTF-8"));

問題解決。


