weizi2012

          weizi2012

          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();
            }
           }

           /**
            * 支持在線打開下載
            *
            * @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);
             // 文件名應(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 on 2012-09-27 10:57 jiangwei 閱讀(1315) 評論(2)  編輯  收藏

          Feedback

          # re: java 文件下載 2012-09-27 15:11 遠(yuǎn)程考勤

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

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

          這個(gè)是什么玩意兒其實(shí)也不懂  回復(fù)  更多評論   



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


          網(wǎng)站導(dǎo)航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 达拉特旗| 西峡县| 莱州市| 桐梓县| 建宁县| 茂名市| 宁夏| 江达县| 大名县| 湟源县| 博乐市| 灵川县| 新丰县| 治县。| 信阳市| 嘉兴市| 兴和县| 南宫市| 华蓥市| 梁山县| 含山县| 临猗县| 通海县| 汝州市| 承德市| 武冈市| 斗六市| 嵊州市| 海口市| 曲水县| 阜平县| 商河县| 甘洛县| 太原市| 太湖县| 平原县| 繁昌县| 逊克县| 普兰店市| 桓台县| 安阳市|