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

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


          網站導航:
           
          主站蜘蛛池模板: 宁南县| 藁城市| 来安县| 若尔盖县| 红原县| 鹿泉市| 福清市| 安仁县| 巧家县| 南皮县| 红原县| 湘潭县| 洛宁县| 新化县| 五河县| 旌德县| 定兴县| 阿荣旗| 九寨沟县| 江永县| 乌拉特后旗| 双鸭山市| 修水县| 张掖市| 枝江市| 平罗县| 新余市| 石阡县| 慈溪市| 惠州市| 都安| 宁安市| 涪陵区| 湟源县| 台东县| 彰化市| 历史| 二连浩特市| 开阳县| 宾阳县| 洛阳市|