java遠程下載圖片

來源:互聯網
上載者:User

標籤:https   tps   lis   正則   修改   index   auth   ror   summary   

從別的網站複製文章的時候,要把圖片下載到我們網站,再把圖片地址換成我們網站的地址

<img id="mbkenHUwhWeOj9U8K6c8LlAXaes3oXit-M4SnmRvB4wXXEue2ol7MviqfDlnDFgp.jpg" alt="圖片" onerror="NextPic(‘/attached/image/mbkenHUwhWeOj9U8K6c8LlAXaes3oXit-M4SnmRvB4wXXEue2ol7MviqfDlnDFgp.jpg‘,‘mbkenHUwhWeOj9U8K6c8LlAXaes3oXit-M4SnmRvB4wXXEue2ol7MviqfDlnDFgp.jpg‘);" src="http://localhost:8080/attached/image/mbkenHUwhWeOj9U8K6c8LlAXaes3oXit-M4SnmRvB4wXXEue2ol7MviqfDlnDFgp.jpg">

  

<script type="text/javascript">window.NextPic=function(url,name){    var cb = "http://localhost:8080"+url    console.log("haha");    var src = document.getElementById(name).src    if(src.indexOf("rcwimg")<=0) return;    document.getElementById(name).src = cb;}</script>

 

public class WaNewsInfoVo extends Page<WaNewsInfo>{        private Integer id;    private String title;    private Integer typeId;    private String typeName;    private Integer typeId2;    private String typeName2;    private String logo;    private String content;    private String createTime;    private String createUser;    private String createUserName;    private String tag1;    private String tag2;    private Integer isDisplay;    private Integer browser;    private String summary;}

 

  // 編碼
  private static final String ECODING = "UTF-8";
  // 擷取img標籤正則
  private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
  // 擷取src路徑的正則
  private static final String IMGSRC_REG = "(http|https):\"?(.*?)(\"|>|\\s+)";


  //添加或者修改資訊public int insertOrUpdate(HttpServletRequest request, WaNewsInfoVo vo){String aaa = "/attached/image/";String path = request.getSession().getServletContext().getRealPath("../")+ "/attached/image/";//擷取圖片標籤List<String> imgUrl = getImageUrl(vo.getContent());//擷取圖片src地址 List<String> imgSrc = getImageSrc(imgUrl); //下載圖片 Download(imgSrc,path); String test = replaceHtmlTag(vo.getContent(),"img","src","src=\""+aaa,"\""); vo.setContent(test); if(CommUtil.isEmpty(vo.getId())){vo.setBrowser(0);return waNewsDao.insert(vo);}return waNewsDao.update(vo);}

   

   /***      * 擷取ImageUrl地址      *       * @param HTML      * @return      */      private List<String> getImageUrl(String HTML) {          Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);          List<String> listImgUrl = new ArrayList<String>();          while (matcher.find()) {              listImgUrl.add(matcher.group());          }          return listImgUrl;      }     /***      * 擷取HTML內容      *       * @param url      * @return      * @throws Exception      */      private String getHTML(String url) throws Exception {          URL uri = new URL(url);          URLConnection connection = uri.openConnection();          InputStream in = connection.getInputStream();          byte[] buf = new byte[1024];          int length = 0;          StringBuffer sb = new StringBuffer();          while ((length = in.read(buf, 0, buf.length)) > 0) {              sb.append(new String(buf, ECODING));          }          in.close();          return sb.toString();      }      /***      * 擷取ImageSrc地址      *       * @param listImageUrl      * @return      */      private List<String> getImageSrc(List<String> listImageUrl) {          List<String> listImgSrc = new ArrayList<String>();          for (String image : listImageUrl) {              Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(image);              while (matcher.find()) {                  listImgSrc.add(matcher.group().substring(0, matcher.group().length() - 1));              }          }          return listImgSrc;      }      /***      * 下載圖片      *       * @param listImgSrc      */      private void Download(List<String> listImgSrc, String path) {          try {              for (String url : listImgSrc) {                  if(url.indexOf("http")>=0){                String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());                  URL uri = new URL(url);                  InputStream in = uri.openStream();                FileOutputStream fo = new FileOutputStream(new File(path,imageName));                  byte[] buf = new byte[1024];                  int length = 0;                  System.out.println("開始下載:" + url);                  while ((length = in.read(buf, 0, buf.length)) != -1) {                      fo.write(buf, 0, length);                  }                  in.close();                  fo.close();                  System.out.println(path+imageName + "下載完成");                  }            }          } catch (Exception e) {              System.out.println("下載失敗");          }      }     

替換img標籤

    /**      * 替換指定標籤的屬性和值      * @param str 需要處理的字串      * @param tag 標籤名稱      * @param tagAttrib 要替換的標籤屬性值      * @param startTag 新標籤開始標記      * @param endTag  新標籤結束標記      * @return      * @author huweijun      * @date 2016年7月13日 下午7:15:32      */      public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {        //匹配以<img開頭>結尾        String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;          //匹配src="開頭,"結尾        String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;          //編譯後的Regex        Pattern patternForTag = Pattern.compile (regxpForTag,Pattern. CASE_INSENSITIVE );          Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern. CASE_INSENSITIVE );        //先查<img標籤        Matcher matcherForTag = patternForTag.matcher(str);          StringBuffer sb = new StringBuffer();          //如果有結果        boolean result = matcherForTag.find();          while (result) {              StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");              //查到的第一個<img,再匹配src            Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));              if (matcherForAttrib.find()) {                  String attributeStr = matcherForAttrib.group(1);                String imageName = attributeStr.substring(attributeStr.lastIndexOf("/") + 1, attributeStr.length());                sbreplace.append("id=‘"+imageName+"‘ alt=‘圖片‘ onerror=\"NextPic(‘/attached/image/"+imageName+"‘,‘"+imageName+"‘);\"");                matcherForAttrib.appendReplacement(sbreplace, startTag + "rcwimg" + endTag);            }              matcherForAttrib.appendTail(sbreplace);              matcherForTag.appendReplacement(sb, sbreplace.toString());              result = matcherForTag.find();          }          matcherForTag.appendTail(sb);                   return sb.toString();      }      

 

java遠程下載圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.