weizi2012

          weizi2012

          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 on 2012-09-27 10:57 jiangwei 閱讀(1314) 評論(2)  編輯  收藏

          Feedback

          # re: java 文件下載 2012-09-27 15:11 遠程考勤

          下載圖的就是速度和便捷  回復  更多評論   

          # re: java 文件下載 2012-09-28 13:04 天津公積金取

          這個是什么玩意兒其實也不懂  回復  更多評論   



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


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 江源县| 张北县| 桐庐县| 芦山县| 临湘市| 龙口市| 连南| 林甸县| 志丹县| 侯马市| 瓮安县| 三江| 鱼台县| 东乡族自治县| 古丈县| 黔江区| 洛隆县| 洱源县| 图片| 繁昌县| 盱眙县| 多伦县| 原平市| 革吉县| 兴安县| 邹平县| 青田县| 彭水| 紫金县| 上虞市| 开远市| 蚌埠市| 西畴县| 金湖县| 夹江县| 灵台县| 交口县| 石首市| 宁南县| 宽甸| 广宗县|