BlogJava 首頁 新隨筆 聯系 聚合 管理
            37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks
          /**
               * 實現文件另存功能
               * 
               * 
          @param text
               *            文件內容
               * 
          @param fileName
               *            文件名稱
               * 
          @return
               
          */

              
          protected String renderFile(String text, String fileName)
                      
          throws IOException
              
          {
                  response.addHeader(
          "Content-Disposition""attachment; filename="
                          
          + fileName);
                  response.setContentType(
          "application/octet-stream");
                  response.setCharacterEncoding(
          "GB2312");
                  response.getWriter().write(text);
                  response.flushBuffer();
                  response.getWriter().close();
                  
          return null;
              }

          下載的action:
          /**
               * 提供下載的方法
               * 
          @return
               
          */

              
          public String down()
              
          {
                  String dir 
          = getFullPath() + "/upload/file/";
                  
          try
                  
          {
                      
          if (!FileUtils.exists(dir))
                      
          {
                          
          new File(dir).mkdirs();
                      }

                      Random r 
          = new Random(System.currentTimeMillis());
                      Integer randomInt 
          = r.nextInt();
                      
          this.renderFile("test content:" + randomInt,randomInt + ".txt");
                  }

                  
          catch (IOException e)
                  
          {
                      e.printStackTrace();
                      
          this.renderText(e.getMessage());
                  }

                  
          return null;
              }


          posted on 2009-08-07 23:37 xiachang88 閱讀(174) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 科尔| 阿合奇县| 澄迈县| 收藏| 黄平县| 平武县| 霍林郭勒市| 鹤壁市| 福海县| 彩票| 榆社县| 扬中市| 深水埗区| 林口县| 南昌县| 无为县| 图们市| 新龙县| 珠海市| 崇阳县| 焦作市| 晋中市| 沙湾县| 宝丰县| 格尔木市| 观塘区| 新兴县| 乾安县| 绿春县| 宝清县| 揭阳市| 广河县| 武穴市| 麦盖提县| 十堰市| 曲松县| 台江县| 顺义区| 邢台市| 开化县| 高青县|