posts - 54,  comments - 1,  trackbacks - 0
           /**
            * 實現(xiàn)下載
            *
            * 
          @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 閱讀(346) 評論(0)  編輯  收藏 所屬分類: 開發(fā)手記

          蜀中人氏,躬耕于珠海

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

          常用鏈接

          留言簿(2)

          隨筆分類(71)

          隨筆檔案(54)

          博客

          文檔

          站點

          論壇

          搜索

          •  

          積分與排名

          • 積分 - 50987
          • 排名 - 977

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 双鸭山市| 奉新县| 手游| 鄱阳县| 宁远县| 灵丘县| 信丰县| 游戏| 蓬莱市| 资源县| 武山县| 绥滨县| 平阳县| 桦甸市| 湘潭市| 清水县| 宣汉县| 兴仁县| 中超| 田林县| 丁青县| 松潘县| 濮阳县| 锦州市| 河东区| 达孜县| 井研县| 汝阳县| 额济纳旗| 正定县| 青州市| 白银市| 平南县| 天气| 铁力市| 醴陵市| 安塞县| 南宫市| 濉溪县| 潜山县| 汝阳县|