<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    每日一得

    不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發(fā)
    最近關(guān)心的內(nèi)容:SSH,seam,flex,敏捷,TDD
    本站的官方站點(diǎn)是:顛覆軟件

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      220 隨筆 :: 9 文章 :: 421 評(píng)論 :: 0 Trackbacks
    key words: jsp文件下載 中文亂碼 文件名亂碼

    文件上傳參看: 這里

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

    摘錄如下:


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

    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();
    }
    %>?


    注意,關(guān)鍵就是setHeader里的filename需要重新編碼,格式是ISO-8859-1就OK了

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

    list.jsp: 顯示附件名稱的頁面
    <tr>
    ????????????
    <td?height="25"?class="tdcor">&nbsp;&nbsp;&nbsp;</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;

    ????????
    //?寫緩沖區(qū):
    ????????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。文件名在數(shù)據(jù)庫中保存的編碼為URLEncode
    2.在list.jsp顯示的時(shí)候多了一次encode,不知為什么,不encode一次還不行,實(shí)際上是第二次編碼了


    posted on 2006-06-14 22:48 Alex 閱讀(35504) 評(píng)論(35)  編輯  收藏 所屬分類: web技術(shù)

    評(píng)論

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-06-15 01:10 黑蝙蝠
    感謝你啊 我遇到這個(gè)問題好久了啊 一直沒找到解決方法!
    只有用英文名字代替!  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-06-15 09:04 寒晴天
    DYG  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-06-16 13:33 google
    B CUO  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-06-19 21:31 Hi
    多謝,哥們,非常感謝  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-08-10 10:53 一路順風(fēng)
    讓人非常感激的文章 樓下多轉(zhuǎn)摘哦   回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-09-19 09:53 myloveroot
    兩個(gè)download.jsp文件,他們的代碼組合到一起嗎?  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-09-21 09:52 renyprrrre\
    兩個(gè)download.jsp文件怎么用  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-10-09 16:58 Java初心
    好文章啊  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-10-30 17:12 bobo[匿名]
    SRVE0026E: [Servlet 錯(cuò)誤]-[已獲取輸出流]:java.lang.IllegalStateException: 已獲取輸出流
    at com.ibm.ws.webcontainer.srt.SRTServletResponse.getWriter(SRTServletResponse.java:545)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java(Compiled Code))
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:193)
    at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:246)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:197)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:255)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:249)
    at org.apache.jsp._down_5F_document._jspService(_down_5F_document.java:219)
    at com.ibm.ws.webcontainer.jsp.runtime.HttpJspBase.service(HttpJspBase.java:89)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
    at com.ibm.ws.webcontainer.jsp.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:344)
    at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.serviceJspFile(JspServlet.java:683)
    at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.service(JspServlet.java:781)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java(Inlined Compiled Code))
    at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java(Compiled Code))
    at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java(Inlined Compiled Code))
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code))
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code))
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java(Compiled Code))
    at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java(Compiled Code))
    at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java(Compiled Code))
    at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java(Compiled Code))
    at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java(Compiled Code))
    at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java(Compiled Code))
    at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java(Compiled Code))
    at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java(Compiled Code))
    at com.ibm.ws.http.HttpConnection.run(HttpConnection.java(Compiled Code))
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))


    怎么解決  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-12-11 19:53 BeanSoft
    這個(gè)... 用 Weblogic 似乎也是同樣的錯(cuò)誤..

    貌似要先調(diào)用一下 response.reset();  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2006-12-11 19:58 BeanSoft
    http://www.tkk7.com/beansoft/archive/2006/12/11/87034.html

    看看我轉(zhuǎn)的這個(gè)文章...

    JSP文件下載及出現(xiàn)getOutputStream() has already been called for ...的解決方法  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2007-01-27 16:00 老大感謝!
    說什么好呢?只能說樓主我太愛你了。你的那句
    new String(filename.getBytes("gb2312"),"iso8859-1"));拯救了我,謝謝!  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2007-03-20 17:19 mathfych
    hao  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2007-11-29 15:19 rocker100
    @黑蝙蝠
    說什么很好,只能說樓主我太愛你了。  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-04-23 15:04 三水少
    好帖,贊!  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-04-29 10:36
    好東西  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-07-21 11:19 午夜晴天
    謝過。。。。。。。。。。。。。
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
    filename=new String(filename.getBytes("iso8859-1"),"gb2312");
      回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-11-20 16:11 游客
    為什么我的還是解決不了  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-11-20 16:13 游客
    我的環(huán)境是tomcat5.5、mysql4.0  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2008-11-20 16:30 游客
    當(dāng)我用 String fileName = "測(cè)試.doc";
    可以正常顯示
    但是我用 String fileName = request.getParameter("filename");
    還是顯示亂碼!!

    怎么回事啊?!?!  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2009-03-20 09:01 游客
    好文章  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2009-03-20 09:02 游客
    www.chinaitlab.com  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2009-03-20 09:02 游客
    http://www.tkk7.com/alex/archive/2006/06/14/52855.aspx  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2009-03-20 09:03 游客
    http://www.baidu.com  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2009-03-20 09:04 游客
    d:\\hao.txt  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決[未登錄] 2009-12-22 09:14 xma
    作者真是幫了我大忙,讓我知道這個(gè)問題的內(nèi)在原因了,謝謝作者的好帖,大家?guī)兔敯 ?nbsp; 回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2010-07-23 09:49
    謝謝了, String filename=new String(file.getBytes("iso8859-1"),"gb2312");
    真有用,要不我還不知道怎么做呢。  回復(fù)  更多評(píng)論
      

    # 已獲得輸出流 2011-05-03 21:34 唯一
    @bobo[匿名]
    在處理下載的jsp頁面加上
    out.clear();
    out = pageContext.pushBody();  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2011-06-02 09:25 小佳
    感謝樓主啊  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2011-06-02 09:25 小佳
    感謝樓主  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2012-04-05 22:17 撐一支長(zhǎng)篙
    感謝樓主,真多虧那一句new String(filename.getBytes("gb2312"),"iso8859-1")),也多謝前面特別提及這一句的兄弟  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2012-09-27 10:47 jiangwei
    把中文的文件名字進(jìn)行URLEncoder.encode(filenamedisplay, "UTF-8")(filenamedisplay:文件名稱)  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2013-04-19 15:34 abby
    非常好用 謝謝  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2013-07-09 14:37 txt小說下載
    亂碼一直令我感到非常頭疼。  回復(fù)  更多評(píng)論
      

    # re: jsp實(shí)現(xiàn)文件下載與中文文件名亂碼問題解決 2013-10-15 20:54 程瓊
    樓主愛你啊!!!  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 亚洲AV日韩AV永久无码久久| 亚洲一区无码精品色| 免费理论片51人人看电影| 亚洲国产av玩弄放荡人妇| 久久亚洲精品11p| 黄色视屏在线免费播放| 99久久免费精品视频| 亚洲欧洲免费无码| 亚洲av无码专区在线观看素人| 亚洲精品国产精品乱码不卞 | 无码一区二区三区免费| 国产成人精品免费视| 久久aⅴ免费观看| 性做久久久久久免费观看| 亚洲成年看片在线观看| 久久久久亚洲AV片无码| 亚洲精品无码aⅴ中文字幕蜜桃| 四虎一区二区成人免费影院网址| 很黄很污的网站免费| 啦啦啦www免费视频| 亚洲日韩中文字幕在线播放| 亚洲国产日韩在线成人蜜芽| 免费人成视频在线播放| 最近中文字幕完整免费视频ww| 国产美女做a免费视频软件| 亚洲av永久无码精品秋霞电影影院 | 亚洲国产午夜电影在线入口| 另类专区另类专区亚洲| 污视频在线免费观看| 免费国产一级特黄久久| 亚洲色欲www综合网| 一级免费黄色大片| 波多野结衣在线免费观看| 久久午夜免费视频| 成全视频免费高清 | 校园亚洲春色另类小说合集 | 国产无遮挡又黄又爽免费网站| AA免费观看的1000部电影| 亚洲午夜未满十八勿入网站2| 456亚洲人成在线播放网站| a级在线观看免费|