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


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

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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 临朐县| 牙克石市| 大宁县| 遂溪县| 吉林市| 象山县| 金乡县| 浦县| 潞城市| 镇安县| 乌兰察布市| 吉安市| 西安市| 遂昌县| 炉霍县| 晋城| 佛坪县| 山西省| 全椒县| 仁怀市| 安顺市| 东源县| 孝昌县| 年辖:市辖区| 桦川县| 塔河县| 都江堰市| 贵溪市| 东至县| 红河县| 中方县| 林西县| 察隅县| 湘阴县| 融水| 江西省| 乌鲁木齐县| 化州市| 昌都县| 新巴尔虎左旗| 罗定市|