HTML to Img파일 추출 후 개별저장

김민규·2021년 3월 11일
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlParsingTest {

    public static void main(String[] args) throws Exception {
        StringBuilder contentBuilder = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader("htmlForParsing.html"));
            String str;
            while ((str = in.readLine()) != null) {
                contentBuilder.append(str);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String content = contentBuilder.toString();

        // content부분만 HTML형태의 String으로 넣어주면 됨.
        Document doc = Jsoup.parse(content);
        Elements imgs = doc.getElementsByTag("img");

        if (imgs.size() > 0) {
            for (Element img : imgs) {
                String src = img.attr("src");

                if (src.contains("data:image/")) {
                    String extension = src.substring(src.indexOf("/") + 1, src.indexOf(";"));
                    String base64Image = src.split(",")[1];

                    // 실제 이미지파일 byte[]
                    byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);

                    String filePath = "/upload/20210311/";
                    Calendar calendar = Calendar.getInstance();
                    Date date = calendar.getTime();
                    String fileName = (new SimpleDateFormat("yyyyMMddHHmmss").format(date)) + "." + extension;

                    // byte 데이터 ObjectStorage로 inputStream열어서 filePath, fileName, byte[] 넣고 업로드

                    // 업로드 성공하면 가져온 image태그의 src부분 주소로 변경
                    img.attr("src", filePath + fileName);
                }
            }
        }

        // System.out.println(imgs.toString());
        System.out.println(doc.getElementsByTag("img").toString());
        // System.out.println(doc.outerHtml());

        File f = new File("test111.html");
        FileUtils.writeStringToFile(f, doc.outerHtml());
    }
}
profile
개발자

0개의 댓글