weizi2012

          weizi2012

          2012年9月27日 #

          java 文件下載

          /**
            * 本地下載
            *
            * @param response
            * @throws FileNotFoundException
            */
           public void downloadLocal(HttpServletResponse response)
             throws FileNotFoundException {
            // 下載本地文件
            String fileName = "Operator.doc".toString(); // 文件的默認(rèn)保存名
            // 讀到流中
            InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
            // 設(shè)置輸出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition", "attachment; filename=\""
              + fileName + "\"");
            // 循環(huán)取出流中的數(shù)據(jù)
            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();
            }
           }

           /**
            * 網(wǎng)絡(luò)文件下載
            *
            * @param response
            * @throws MalformedURLException
            */
           public void downloadNet(HttpServletResponse response)
             throws MalformedURLException {
            // 下載網(wǎng)絡(luò)文件
            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();
            }
           }

           /**
            * 支持在線打開(kāi)下載
            *
            * @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) { // 在線打開(kāi)方式
             URL u = new URL("file:///" + filePath);
             response.setContentType(u.openConnection().getContentType());
             response.setHeader("Content-Disposition", "inline; filename="
               + fname);
             // 文件名應(yīng)該編碼成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) | 評(píng)論 (2)編輯 收藏

          僅列出標(biāo)題  

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 南涧| 遂昌县| 汾阳市| 哈尔滨市| 淮南市| 龙口市| 万盛区| 通河县| 丰县| 五莲县| 谷城县| 仪陇县| 三台县| 汕尾市| 从化市| 蓬莱市| 江山市| 河北区| 兴仁县| 泸州市| 铜陵市| 阳曲县| 乐山市| 莱阳市| 教育| 武鸣县| 平陆县| 略阳县| 永新县| 边坝县| 九台市| 随州市| 丘北县| 依兰县| 福清市| 分宜县| 黑水县| 溆浦县| 高州市| 长葛市| 元阳县|