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 閱讀(1316) | 評論 (2)編輯 收藏

          僅列出標題  

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 顺平县| 巩义市| 敦煌市| 青岛市| 息烽县| 涪陵区| 桐梓县| 扬中市| 麟游县| 青田县| 师宗县| 策勒县| 涡阳县| 蓬安县| 噶尔县| 察隅县| 肥乡县| 汉阴县| 吉隆县| 龙门县| 曲靖市| 遵义市| 德庆县| 登封市| 海伦市| 响水县| 阿克| 定西市| 田林县| 德州市| 莫力| 南溪县| 博爱县| 环江| 临泉县| 尼勒克县| 惠来县| 石泉县| 南平市| 全州县| 霍城县|