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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import com.sun.istack.internal.NotNull; import com.sun.istack.internal.Nullable; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Run { public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"; public static final String WEB_CONTENT = "http://www.todayhumor.co.kr/board/view_tail.php?"; public static void main(String[] args) { new Run(args); } private Run(String... args) { if (args == null || args.length == 0) { System.out.println("파일명을 입력하셈"); return; } for (String arg : args) { File file = new File(arg); if (!file.exists()) { System.out.println(arg + " 파일을 찾을 수 없음"); continue; } try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { downloadFiles(br); } catch (IOException e) { e.printStackTrace(); } } } private void downloadFiles(@NotNull BufferedReader br) throws IOException { String str; while ((str = br.readLine()) != null) { if (str.startsWith(WEB_CONTENT)) { InputStream is = createConnection(str); if (is == null) continue; downloadFiles(new BufferedReader(new InputStreamReader(is))); } else { int idx; while ((idx = getUrlIndex(str)) > -1) { str = str.substring(idx); int jIdx = str.indexOf(".jpg"); int gIdx = str.indexOf(".gif"); int pIdx = str.indexOf(".png"); int[] indexes = { jIdx, gIdx, pIdx }; int index = Integer.MAX_VALUE; for (int i : indexes) { if (i < index && i > -1) index = i; } if (index != Integer.MAX_VALUE && index > -1) { String url = str.substring(0, idx = (index + 4)); download(url, "imgs/", url.substring(url.lastIndexOf("/") + 1)); str = str.substring(idx); } else { str = str.substring(7); } } } } } private int getUrlIndex(@NotNull String line) { int httpIdx = line.indexOf("http://"); int httpsIdx = line.indexOf("https://"); if (httpIdx > -1 && httpsIdx > -1) { if (httpIdx < httpsIdx) return httpIdx; else return httpsIdx; } else { if (httpIdx > -1) return httpIdx; else return httpsIdx; } } @Nullable private InputStream createConnection(@NotNull String uri) throws IOException { URL u = new URL(uri); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestProperty("User-Agent", USER_AGENT); conn.getResponseCode(); InputStream err = conn.getErrorStream(); if (err == null) { conn.connect(); return conn.getInputStream(); } try (Scanner scan = new Scanner(err)) { scan.useDelimiter("\\z"); System.out.println(scan.next()); return null; } } @Nullable private File download(@NotNull String uri, @NotNull String dir, @NotNull String name) throws IOException { File dirFile = new File(dir); if (!dirFile.exists() && !dirFile.mkdirs()) return null; List<String> files = Arrays.asList(dirFile.list()); int i = 0; boolean exists; do { boolean e = false; for (String file : files) { if (i > 0) { StringBuilder sb = new StringBuilder(name); if ((sb.insert(sb.indexOf("."), "_").insert(sb.lastIndexOf("_") + 1, i).toString()).equals(file)) { i++; e = true; break; } } else { if (name.equals(file)) { i++; e = true; break; } } } exists = e; } while (exists); if (i > 0) { StringBuilder sb = new StringBuilder(name); name = sb.insert(sb.indexOf("."), "_").insert(sb.lastIndexOf("_") + 1, i).toString(); } return download(uri, dir + name); } @NotNull private File download(@NotNull String uri, @NotNull String path) throws IOException { System.out.println("SEND >>>> " + uri); URL u = new URL(uri); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); File file = new File(path); OutputStream os = new FileOutputStream(file); byte[] buff = new byte[0x400]; int length; while ((length = is.read(buff)) > 0) os.write(buff, 0, length); os.close(); is.close(); conn.disconnect(); System.out.println("FILE <<<< " + path + "(" + file.length() + " byte)"); return file; } } |
오늘 아침에 하나하나 저장하기가 귀찮아서 시작했는데 재밌게 짰네여..
사용법은 텍스트 파일 안에 꼬릿말 url 또는 이미지파일 url을 입력해서 저장하고 커멘드에 해당 txt파일 입력..
커멘드에 txt파일 여러개 가능, txt파일 내에 url 여러줄 가능..
가져다 쓰시는거, 자기가 짠거인척 하는거(..)까지 완전히 자유입니다