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