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 + "\"");
            // 循環(huán)取出流中的數據
            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 閱讀(1314) | 評論 (2)編輯 收藏

          僅列出標題  

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 成安县| 玉门市| 沈丘县| 潜山县| 宾阳县| 保德县| 武清区| 克什克腾旗| 临安市| 利川市| 聂拉木县| 乌鲁木齐县| 丹阳市| 龙岩市| 丰县| 盖州市| 平湖市| 梨树县| 景东| 德令哈市| 古浪县| 错那县| 阳新县| 景德镇市| 镇赉县| 红原县| 宣城市| 林芝县| 泉州市| 保亭| 平邑县| 徐水县| 淳安县| 丰顺县| 收藏| 乌拉特后旗| 城固县| 彩票| 呼玛县| 新泰市| 嘉禾县|