tinguo002

           

          java下載網(wǎng)頁內(nèi)容和網(wǎng)絡(luò)圖片

          import java.io.BufferedReader;
          import java.io.ByteArrayOutputStream;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.net.HttpURLConnection;
          import java.net.URL;

          public class UrlResource {

              
          public static void main(String[] args){
                  
          try {
                      System.out.println(UrlResource.getUrlDetail(
          "http://www.baidu.com",true));
                      saveUrlFile(
          "http://www.baidu.com/img/baidu_jgylogo3.gif""D:\\1.gif");
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }

              
              
          //獲取網(wǎng)絡(luò)文件,轉(zhuǎn)存到fileDes中,fileDes需要帶文件后綴名
              public static void saveUrlFile(String fileUrl,String fileDes) throws Exception
              
          {
                  File toFile 
          = new File(fileDes);
                  
          if (toFile.exists())
                  
          {
          //            throw new Exception("file exist");
                      return;
                  }

                  toFile.createNewFile();
                  FileOutputStream outImgStream 
          = new FileOutputStream(toFile);
                  outImgStream.write(getUrlFileData(fileUrl));
                  outImgStream.close();
              }

              
              
          //獲取鏈接地址文件的byte數(shù)據(jù)
              public static byte[] getUrlFileData(String fileUrl) throws Exception
              
          {
                  URL url 
          = new URL(fileUrl);
                  HttpURLConnection httpConn 
          = (HttpURLConnection) url.openConnection();
                  httpConn.connect();
                  InputStream cin 
          = httpConn.getInputStream();
                  ByteArrayOutputStream outStream 
          = new ByteArrayOutputStream();
                  
          byte[] buffer = new byte[1024];
                  
          int len = 0;
                  
          while ((len = cin.read(buffer)) != -1{
                      outStream.write(buffer, 
          0, len);
                  }

                  cin.close();
                  
          byte[] fileData = outStream.toByteArray();
                  outStream.close();
                  
          return fileData;
              }

              
              
          //獲取鏈接地址的字符數(shù)據(jù),wichSep是否換行標(biāo)記
              public static String getUrlDetail(String urlStr,boolean withSep) throws Exception
              
          {
                  URL url 
          = new URL(urlStr);
                  HttpURLConnection httpConn 
          = (HttpURLConnection)url.openConnection();
                  httpConn.connect();
                  InputStream cin 
          = httpConn.getInputStream();
                  BufferedReader reader 
          = new BufferedReader(new InputStreamReader(cin,"UTF-8"));
                  StringBuffer sb 
          = new StringBuffer();
                  String rl 
          = null;
                  
          while((rl = reader.readLine()) != null)
                  
          {
                      
          if (withSep)
                      
          {
                          sb.append(rl).append(System.getProperty(
          "line.separator"));
                      }

                      
          else
                      
          {
                          sb.append(rl);
                      }

                  }

                  
          return sb.toString();
              }

              
          }

          // 禁止圖像緩存
                  response.setHeader("Pragma", "no-cache");
                  response.setHeader("Cache-Control", "no-cache");
                  response.setDateHeader("Expires", 0);


          文章詳細(xì)參考:http://blog.csdn.net/pandakong/article/details/7430844


          歡迎大家訪問我的個(gè)人網(wǎng)站 萌萌的IT人

          posted on 2014-08-04 18:38 一堣而安 閱讀(1512) 評(píng)論(0)  編輯  收藏 所屬分類: java

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 嘉禾县| 金堂县| 东乡族自治县| 旬邑县| 宁安市| 怀来县| 宁远县| 石城县| 长沙县| 雷波县| 江达县| 广灵县| 城固县| 垫江县| 屯昌县| 明溪县| 阜城县| 平昌县| 拜泉县| 河曲县| 惠州市| 桐庐县| 隆子县| 瑞昌市| 陇南市| 徐州市| 香港| 佛山市| 北安市| 德格县| 五峰| 贺州市| 留坝县| 辽中县| 休宁县| 正阳县| 含山县| 嘉兴市| 灌云县| 盐山县| 泽州县|