key words: jsp文件下載 中文亂碼 文件名亂碼
文件上傳參看:
這里碰到文件亂碼,google了一下,發現
這篇文章還不賴
摘錄如下:
之前,寫過一個Download.jsp文件,可以解決下載文件亂碼問題(諸如:DOC,XSL文件等等).
后來發現,遇到中文名的文件的時候,文件下載將會報錯~~~~
今天,通過改寫原Download.jsp文件已經徹底解決了這個問題~
現在,把一整套的文件上傳下載的方法給貼出來~~~以便大家借鑒!~!~!~!~!?
作者:古埃及法老
download.jsp文件---------------------------------------------------------
<%
??java.io.BufferedInputStream?bis=null;
??java.io.BufferedOutputStream??bos=null;
try{
?String?filename=request.getParameter("filename");
?????????????filename=new?String(filename.getBytes("iso8859-1"),"gb2312");
?response.setContentType("application/x-msdownload");
?response.setHeader("Content-disposition","attachment;?filename="+new?String(filename.getBytes("gb2312"),"iso8859-1"));
?bis?=new?java.io.BufferedInputStream(new?java.io.FileInputStream(config.getServletContext().getRealPath("files/"?+?filename)));
?bos=new?java.io.BufferedOutputStream(response.getOutputStream());?
?byte[]?buff?=?new?byte[2048];
?int?bytesRead;
?while(-1?!=?(bytesRead?=?bis.read(buff,?0,?buff.length)))?{
??bos.write(buff,0,bytesRead);
?}
}
catch(Exception?e){
?e.printStackTrace();
}
finally?{
?if?(bis?!=?null)bis.close();
?if?(bos?!=?null)bos.close();
}
%>?
注意,關鍵就是
setHeader里的filename需要重新編碼,格式是ISO-8859-1就OK了
以下是我自己項目中用到的代碼片斷,供參考:
list.jsp: 顯示附件名稱的頁面
<tr>
????????????<td?height="25"?class="tdcor">附 件 </td>
????????????<td?colspan="3"?height=50>
????????????????<%
????????????????????if?(null?!=?publish.getAttatchFilename()?&&
publish.getAttatchFilename().length()?>?0)?{
????????????????%>
????????????????<a?href="publish_do.jsp?method=download&fileName=
<%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>">
<%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%></a>
????????????????<%
????????????????????}
????????????????%>
????????????</td>
</tr>
download.jsp:下載頁面else?if?(null?!=?method?&&?method.equals("download"))?{//下載附件
????????String?fileName?=?request.getParameter("fileName");
????????File?file?=?new?File(Constants.PUBLISH_FILE_PATH?+?"/"?+?URLDecoder.decode(fileName,"GBK"));
????????response.reset();
????????response.setContentType("application/octet-stream;?charset=GBK");
????????response.addHeader("Content-Disposition",?"attachment;?filename="?+?CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1"));
????????response.setContentLength((int)?file.length());
????????byte[]?buffer?=?new?byte[4096];
????????BufferedOutputStream?output?=?null;
????????BufferedInputStream?input?=?null;
????????//?寫緩沖區:
????????try?{
????????????output?=?new?BufferedOutputStream(response.getOutputStream());
????????????input?=?new?BufferedInputStream(new?FileInputStream(file));
????????????int?n?=?(-1);
????????????while?((n?=?input.read(buffer,?0,?4096))?>?-1)?{
????????????????output.write(buffer,?0,?n);
????????????}
????????????response.flushBuffer();
????????}
????????catch?(Exception?e)?{
????????}?//?maybe?user?cancelled?download
????????finally?{
????????????if?(input?!=?null)?input.close();
????????????if?(output?!=?null)?output.close();
????????}
說明:
1。文件名在數據庫中保存的編碼為URLEncode
2.在list.jsp顯示的時候多了一次encode,不知為什么,不encode一次還不行,實際上是第二次編碼了