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

          碰到文件亂碼,google了一下,發(fā)現(xiàn)這篇文章還不賴


          摘錄如下:
          ??? 之前,寫過(guò)一個(gè)Download.jsp文件,可以解決下載文件亂碼問(wèn)題(諸如:DOC,XSL文件等等).
          后來(lái)發(fā)現(xiàn),遇到中文名的文件的時(shí)候,文件下載將會(huì)報(bào)錯(cuò)~~~~
          今天,通過(guò)改寫原Download.jsp文件已經(jīng)徹底解決了這個(gè)問(wèn)題~
          現(xiàn)在,把一整套的文件上傳下載的方法給貼出來(lái)~~~以便大家借鑒!~!~!~!~!?
          作者:古埃及法老

          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了

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

          list.jsp: 顯示附件名稱的頁(yè)面

          ?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:下載頁(yè)面

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

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

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

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

            回復(fù)  更多評(píng)論
            
          # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問(wèn)題解決(1) 2008-05-16 01:01 | 小非
          這篇文章在網(wǎng)路中廣泛流傳,看了那么多人搜藏這片文章,居然很少人提出異議,真是讓人失望。最上面的代碼我實(shí)驗(yàn)過(guò),一:點(diǎn)擊下載后彈出的保存對(duì)話框中,名稱一項(xiàng)遇中文是一橫杠;二,就是如一樓的那位說(shuō)的抱錯(cuò)情況。本人正在做這個(gè)東西,找半天也沒(méi)解決,繼續(xù)關(guān)注...  回復(fù)  更多評(píng)論
            
          # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問(wèn)題解決(1) 2008-05-16 08:32 | 青誠(chéng)
          等有空,我上傳一個(gè)完整的代碼。  回復(fù)  更多評(píng)論
            
          # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問(wèn)題解決(1) 2009-03-10 16:14 | zhmm
          CourseDetailBusiness 在哪里?  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 峨山| 富锦市| 南宫市| 丹巴县| 宾川县| 莆田市| 小金县| 阿勒泰市| 磐石市| 泰和县| 铜鼓县| 湛江市| 天柱县| 安化县| 沅陵县| 枣庄市| 新昌县| 托里县| 钟山县| 得荣县| 林甸县| 黄龙县| 黄冈市| 务川| 公安县| 井冈山市| 张家口市| 晋宁县| 贵溪市| 白水县| 扎兰屯市| 万源市| 新河县| 许昌县| 榆林市| 尼勒克县| 太原市| 宝清县| 绍兴市| 德清县| 调兵山市|