<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    weizi2012

    weizi2012

    2012年9月27日 #

    java 文件下載

    /**
      * 本地下載
      *
      * @param response
      * @throws FileNotFoundException
      */
     public void downloadLocal(HttpServletResponse response)
       throws FileNotFoundException {
      // 下載本地文件
      String fileName = "Operator.doc".toString(); // 文件的默認保存名
      // 讀到流中
      InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
      // 設置輸出的格式
      response.reset();
      response.setContentType("bin");
      response.addHeader("Content-Disposition", "attachment; filename=\""
        + fileName + "\"");
      // 循環取出流中的數據
      byte[] b = new byte[100];
      int len;
      try {
       while ((len = inStream.read(b)) > 0)
        response.getOutputStream().write(b, 0, len);
       inStream.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }

     /**
      * 網絡文件下載
      *
      * @param response
      * @throws MalformedURLException
      */
     public void downloadNet(HttpServletResponse response)
       throws MalformedURLException {
      // 下載網絡文件
      int bytesum = 0;
      int byteread = 0;
      URL url = new URL("windine.blogdriver.com/logo.gif");
      try {
       URLConnection conn = url.openConnection();
       InputStream inStream = conn.getInputStream();
       FileOutputStream fs = new FileOutputStream("c:/abc.gif");
       byte[] buffer = new byte[1204];
       @SuppressWarnings("unused")
       int length;
       while ((byteread = inStream.read(buffer)) != -1) {
        bytesum += byteread;
        System.out.println(bytesum);
        fs.write(buffer, 0, byteread);
       }
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }

     /**
      * 支持在線打開下載
      *
      * @param filePath
      * @param response
      * @param isOnLine
      * @throws Exception
      */
     public void downLoad(String filePath, HttpServletResponse response,
       boolean isOnLine,String fname) throws Exception {
      System.out.println("filePath:" + filePath);
      File f = new File(filePath);
      if (!f.exists()) {
       response.sendError(404, "File not found!");
       return;
      }
      BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
      byte[] buf = new byte[1024];
      int len = 0;
      response.reset(); // 非常重要
      if (isOnLine) { // 在線打開方式
       URL u = new URL("file:///" + filePath);
       response.setContentType(u.openConnection().getContentType());
       response.setHeader("Content-Disposition", "inline; filename="
         + fname);
       // 文件名應該編碼成UTF-8
      } else { // 純下載方式
       response.setContentType("application/x-msdownload");
       response.setHeader("Content-Disposition", "attachment; filename="
         + fname);
      }
      OutputStream out = response.getOutputStream();
      while ((len = br.read(buf)) > 0)
       out.write(buf, 0, len);
      br.close();
      out.close();
     }

    posted @ 2012-09-27 10:57 jiangwei 閱讀(1304) | 評論 (2)編輯 收藏

    僅列出標題  

    My Links

    Blog Stats

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    主站蜘蛛池模板: 91在线亚洲综合在线| 久久亚洲精品成人无码网站| 亚洲Av无码国产一区二区| 毛片网站免费在线观看| 亚洲一级高清在线中文字幕| 亚洲综合免费视频| 亚洲videos| 日韩高清在线免费观看| 日韩亚洲翔田千里在线| 婷婷亚洲天堂影院| 一级做a爰片久久毛片免费看 | 亚洲国产高清在线| 国内精品久久久久影院免费| 亚洲国产精品久久久久久| 91免费福利精品国产| 国产成人精品日本亚洲专| 日韩在线免费播放| 一区在线免费观看| 亚洲国产精品无码久久一区二区| 99精品视频免费观看| 亚洲伦理中文字幕| yy6080久久亚洲精品| 最近免费中文字幕MV在线视频3 | 毛片在线全部免费观看| 亚洲综合在线成人一区| 无码人妻久久一区二区三区免费丨 | 久久久久久毛片免费播放| 亚洲午夜电影在线观看| 日本一道高清不卡免费| 人体大胆做受免费视频| 亚洲AV电影院在线观看| 久久WWW色情成人免费观看| 无遮挡呻吟娇喘视频免费播放| 国产亚洲精品精华液| 久久笫一福利免费导航| 边摸边吃奶边做爽免费视频网站 | 成人女人A级毛片免费软件| 国产亚洲精品精品精品| 亚洲电影中文字幕| 国产成人高清精品免费鸭子| 一个人看的www免费视频在线观看|