Thinking

          快樂編程,開心生活
          posts - 21, comments - 27, trackbacks - 0, articles - -5
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          文件下載中的漢字編碼問題

          Posted on 2007-02-07 14:32 lixw 閱讀(250) 評論(0)  編輯  收藏
          ?1 public ? void ?doDownLoad(HttpServletRequest?request,?HttpServletResponse?response,?
          ?2 ????????????String?absolutePath)? {
          ?3 ????????
          ?4 ???????? // 設置響應頭信息
          ?5 ????????response.setContentType( " application/octet-stream;charset=UTF-8 " );?
          ?6 ????????log.debug( " GET:? " ? + ?absolutePath);
          ?7 ????????
          ?8 ????????String?str? = ?FilePathParseUtil.getFileNameByPath(absolutePath);
          ?9 ???????? // 調用自定義的編碼函數,解決不同瀏覽器上對漢字編碼的處理
          10 ????????str? = ? this .encodeFileName(request,?str) == null ? str: this .encodeFileName(request,?str);
          11 ???????? // 設置response頭信息,從而顯示正確的文件名,并彈出另存對話框
          12 ????????response.setHeader( " Content-Disposition " ,? " attachment;?filename= " ?
          13 ???????????????? + ?str);
          14 ????????OutputStream?out? = ? null ;
          15 ???????? try {
          16 ???????????? // 從response得到輸出流,從而向客戶端寫出文件
          17 ????????????out? = ?response.getOutputStream();
          18 ????????}
          catch (IOException?e) {
          19 ????????????log.error( " output?stream?is?null " );
          20 ????????????e.printStackTrace();
          21 ????????}

          22 ???????? this .doDownLoad(out,?absolutePath);
          23 ????}

          24 ????
          25 ???? /**
          26 ?????*?根據不同瀏覽器對文件名進行編碼
          27 ?????*? @param ?request?客戶端請求
          28 ?????*? @param ?fileName?文件名
          29 ?????*? @return ?編碼后的文件名
          30 ????? */

          31 ???? public ?String?encodeFileName(HttpServletRequest?request,?String?fileName) {???
          32 ????????String?agent? = ?request.getHeader( " USER-AGENT " );
          33 ???????? try {
          34 ???????????? if ?( null ? != ?agent? && ? - 1 ? != ?agent.indexOf( " MSIE " ))? {???
          35 ???????????????? return ?URLEncoder.encode(fileName,? " UTF-8 " );???
          36 ????????????}
          else ? if ?( null ? != ?agent? && ? - 1 ? != ?agent.indexOf( " Mozilla " ))? {???
          37 ???????????????? return ? " =?UTF-8?B? " +
          38 ???????????????????????? new ?String(
          39 ????????????????????????????????Base64.encodeBase64(
          40 ????????????????????????????????????fileName.getBytes( " UTF-8 " )
          41 ????????????????????????????????)
          42 ????????????????????????)? + ? " ?= " ;???
          43 ????????????}
          ? else ? {???
          44 ???????????????? return ?fileName;???
          45 ????????????}

          46 ????????}
          catch (UnsupportedEncodingException?e) {
          47 ???????????? return ? null ;
          48 ????????}

          49 ????}
          ??

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 怀远县| 临泉县| 临湘市| 保亭| 上思县| 玉门市| 武宁县| 凯里市| 博湖县| 浙江省| 民县| 玉山县| 郁南县| 隆林| 迭部县| 达尔| 思南县| 周口市| 青岛市| 正蓝旗| 嘉荫县| 红桥区| 兴文县| 泸溪县| 新安县| 太仆寺旗| 伊宁县| 昭觉县| 军事| 津市市| 德令哈市| 济宁市| 手游| 贺兰县| 滦平县| 河南省| 沙坪坝区| 仁布县| 霍州市| 雷州市| 罗甸县|