- 정규식(Regular expression)을 이용하는 방법
- HTML Parser 를 이용하는 방법
여기에서는오픈소스 기반의 HTML Parser jsoup 을 이용하여 간단하게 게시물의 본문에서 첫번째 이미지 URL 을 추출하고자 한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package architecture.ee.web.community.announce.impl; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import architecture.common.model.factory.ModelTypeFactory; import architecture.common.model.support.NoNamedEntityModelObjectSupport; import architecture.common.user.User; import architecture.ee.web.attachment.Attachment; import architecture.ee.web.community.announce.Announce; public class DefaultAnnounce extends NoNamedEntityModelObjectSupport implements Announce { private Long announceId; private int objectType ; private Long objectId; private Long userId ; private String subject ; private String body; private Date startDate; private Date endDate; private User user; private List<attachment> attachments; private String firstImageSrc ; private int imageCount = 0 ; /** 중략 **/ /** * @return body */ public String getBody() { return body; } /** * @param body 설정할 body */ public void setBody(String body) { this .body = body; Document doc = Jsoup.parse( this .body); Elements links = doc.select( "img" ); this .imageCount = links.size(); if ( imageCount > 0 ) firstImageSrc = links.first().attr( "src" ); } public String getFirstImageSrc(){ return this .firstImageSrc; } /** * @return imageCount */ public int getImageCount() { return imageCount; } } </attachment> |
자세한 jsoup api 사용법은 문서를 참고
댓글 없음:
댓글 쓰기