posts - 54,  comments - 1,  trackbacks - 0
           /**
            * 實現下載
            *
            * 
          @param response
            * 
          @param headName: 默認文件名
            * 
          @param fileUrl :目標文件URL
            * 
          @throws Exception
            *
          */

           
          public void downLoad(HttpServletResponse response, String headName,
             String fileUrl) 
          throws Exception {

            
          if (headName.endsWith(".jpg"|| (headName.endsWith(".gif"))) {
             
          //no need setting content type and header
            }
           else if (headName.endsWith(".txt")) {
             response.setContentType(
          "text;charset=GB2312");
             response.setHeader(
          "Content-disposition""attachment; filename="
               
          + headName);
            }
           else {
             response.setContentType(
          "application/doc;charset=GB2312");
             response.setHeader(
          "Content-disposition""attachment; filename="
               
          + headName);
            }


            String fileURL 
          = fileUrl;
            
          if (fileURL == null)
             
          return;

            
          try {
             File file 
          = new File(fileUrl);
             FileInputStream bis 
          = new FileInputStream(file);
             OutputStream bos 
          = response.getOutputStream();

             
          byte[] buff = new byte[1024];
             
          int readCount = 0;
             
          int i = 0;
             readCount 
          = bis.read(buff);
             
          while (readCount != -1{
              bos.write(buff, 
          0, readCount);
              readCount 
          = bis.read(buff);
             }

             
          if (bis != null)
              bis.close();
             
          if (bos != null)
              bos.close();

            }
           catch (Exception e) {
             e.printStackTrace();
             
          throw e;
            }

           }



          posted on 2005-11-24 17:25 ZhuJun 閱讀(350) 評論(0)  編輯  收藏 所屬分類: 開發手記

          蜀中人氏,躬耕于珠海

          <2005年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(2)

          隨筆分類(71)

          隨筆檔案(54)

          博客

          文檔

          站點

          論壇

          搜索

          •  

          積分與排名

          • 積分 - 51266
          • 排名 - 974

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 北京市| 新绛县| 浦东新区| 昭平县| 五指山市| 三台县| 辽阳市| 平塘县| 泗水县| 边坝县| 天祝| 武鸣县| 郁南县| 安康市| 筠连县| 庆阳市| 额敏县| 宜兰县| 莎车县| 旬邑县| 通江县| 乡宁县| 沽源县| 渭南市| 平阳县| 中西区| 科技| 南雄市| 交口县| 博罗县| 江孜县| 邵东县| 合阳县| 河津市| 海宁市| 金山区| 朝阳县| 正镶白旗| 田林县| 五莲县| 吉安县|