隨筆-200  評論-148  文章-15  trackbacks-0
          這篇文章也是我收集的,如下:

          碰到文件亂碼,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了

          以下是我自己項目中用到的代碼片斷,供參考:

          list.jsp: 顯示附件名稱的頁面

          ?1?<tr>
          ?2?????????????<td?height="25"?class="tdcor">&nbsp;&nbsp;件&nbsp;</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:下載頁面

          ?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?????????}

          說明:
          1。文件名在數(shù)據(jù)庫中保存的編碼為URLEncode
          2.在list.jsp顯示的時候多了一次encode,不知為什么,不encode一次還不行,實際上是第二次編碼了

          posted on 2006-06-15 13:14 無聲 閱讀(3410) 評論(5)  編輯  收藏 所屬分類: 職場生活

          評論:
          # re: jsp實現(xiàn)文件下載與中文文件名亂碼問題解決(1) 2006-07-20 15:26 | Richar
          output = new BufferedOutputStream(response.getOutputStream());
          這一句應(yīng)該會產(chǎn)生java.lang.IllegalStateException錯誤。在JSP里面是禁止這樣做的,可以寫一個Servlet  回復(fù)  更多評論
            
          # re: jsp實現(xiàn)文件下載與中文文件名亂碼問題解決(1) 2007-02-05 11:01 | jactive
          to Richar: out.close()再用output寫就可以了

            回復(fù)  更多評論
            
          # re: jsp實現(xiàn)文件下載與中文文件名亂碼問題解決(1) 2008-05-16 01:01 | 小非
          這篇文章在網(wǎng)路中廣泛流傳,看了那么多人搜藏這片文章,居然很少人提出異議,真是讓人失望。最上面的代碼我實驗過,一:點擊下載后彈出的保存對話框中,名稱一項遇中文是一橫杠;二,就是如一樓的那位說的抱錯情況。本人正在做這個東西,找半天也沒解決,繼續(xù)關(guān)注...  回復(fù)  更多評論
            
          # re: jsp實現(xiàn)文件下載與中文文件名亂碼問題解決(1) 2008-05-16 08:32 | 青誠
          等有空,我上傳一個完整的代碼。  回復(fù)  更多評論
            
          # re: jsp實現(xiàn)文件下載與中文文件名亂碼問題解決(1) 2009-03-10 16:14 | zhmm
          CourseDetailBusiness 在哪里?  回復(fù)  更多評論
            
          主站蜘蛛池模板: 许昌县| 乾安县| 惠水县| 襄汾县| 磴口县| 浏阳市| 肥西县| 绿春县| 鲁山县| 阿坝县| 安溪县| 昌吉市| 阿巴嘎旗| 文山县| 伊金霍洛旗| 上蔡县| 望城县| 商丘市| 建始县| 鹿邑县| 乐安县| 鄂尔多斯市| 长子县| 永仁县| 济源市| 杭锦后旗| 乌鲁木齐县| 开平市| 河东区| 巴东县| 土默特左旗| 宜州市| 墨江| 郑州市| 易门县| 金坛市| 盱眙县| 庐江县| 义马市| 赣榆县| 长春市|