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

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 麻阳| 勃利县| 和硕县| 亳州市| 宁南县| 镶黄旗| 慈利县| 平湖市| 上杭县| 那坡县| 丘北县| 印江| 赤壁市| 阳新县| 鲁山县| 金门县| 沅陵县| 逊克县| 抚顺市| 平湖市| 乌什县| 额敏县| 沅陵县| 湖州市| 福海县| 大竹县| 来安县| 八宿县| 修水县| 德令哈市| 阜新| 湖南省| 清水县| 上思县| 陇川县| 白朗县| 都兰县| 林芝县| 平安县| 洱源县| 那曲县|