<div> <table style="color:#333333;font-family:Verdana, Helvetica, Arial, sans-serif;font-size:medium;line-height:normal;"><tbody><tr><td><pre style="margin-top:0px;margin-bottom:0px;line-height:16.25px;"> 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</pre></td> <td><pre style="margin-top:0px;margin-bottom:0px;line-height:16.25px;"><span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">com.sun.istack.internal.NotNull</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">com.sun.istack.internal.Nullable</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.io.*</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.net.HttpURLConnection</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.net.URL</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.util.Arrays</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.util.List</span>; <span style="color:#008800;font-weight:bold;">import</span> <span style="color:#0e84b5;font-weight:bold;">java.util.Scanner</span>; <span style="color:#008800;font-weight:bold;">public</span> <span style="color:#008800;font-weight:bold;">class</span> <span style="color:#bb0066;font-weight:bold;">Run</span> { <span style="color:#008800;font-weight:bold;">public</span> <span style="color:#008800;font-weight:bold;">static</span> <span style="color:#008800;font-weight:bold;">final</span> String USER_AGENT = <span style="background-color:#fff0f0;">"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"</span>; <span style="color:#008800;font-weight:bold;">public</span> <span style="color:#008800;font-weight:bold;">static</span> <span style="color:#008800;font-weight:bold;">final</span> String WEB_CONTENT = <span style="background-color:#fff0f0;">"<a target="_blank" href="http://www.todayhumor.co.kr/board/view_tail.php?">http://www.todayhumor.co.kr/board/view_tail.php?</a>"</span>; <span style="color:#008800;font-weight:bold;">public</span> <span style="color:#008800;font-weight:bold;">static</span> <span style="color:#333399;font-weight:bold;">void</span> <span style="color:#0066bb;font-weight:bold;">main</span>(String[] args) { <span style="color:#008800;font-weight:bold;">new</span> <span style="color:#0066bb;font-weight:bold;">Run</span>(args); } <span style="color:#008800;font-weight:bold;">private</span> <span style="color:#0066bb;font-weight:bold;">Run</span>(String... args) { <span style="color:#008800;font-weight:bold;">if</span> (args == <span style="color:#008800;font-weight:bold;">null</span> || args.<span style="color:#0000cc;">length</span> == <span style="color:#0000dd;font-weight:bold;">0</span>) { System.<span style="color:#0000cc;">out</span>.<span style="color:#0000cc;">println</span>(<span style="background-color:#fff0f0;">"파일명을 입력하셈"</span>); <span style="color:#008800;font-weight:bold;">return</span>; } <span style="color:#008800;font-weight:bold;">for</span> (String arg : args) { File file = <span style="color:#008800;font-weight:bold;">new</span> File(arg); <span style="color:#008800;font-weight:bold;">if</span> (!file.<span style="color:#0000cc;">exists</span>()) { System.<span style="color:#0000cc;">out</span>.<span style="color:#0000cc;">println</span>(arg + <span style="background-color:#fff0f0;">" 파일을 찾을 수 없음"</span>); <span style="color:#008800;font-weight:bold;">continue</span>; } <span style="color:#008800;font-weight:bold;">try</span> (BufferedReader br = <span style="color:#008800;font-weight:bold;">new</span> BufferedReader(<span style="color:#008800;font-weight:bold;">new</span> InputStreamReader(<span style="color:#008800;font-weight:bold;">new</span> FileInputStream(file)))) { downloadFiles(br); } <span style="color:#008800;font-weight:bold;">catch</span> (IOException e) { e.<span style="color:#0000cc;">printStackTrace</span>(); } } } <span style="color:#008800;font-weight:bold;">private</span> <span style="color:#333399;font-weight:bold;">void</span> <span style="color:#0066bb;font-weight:bold;">downloadFiles</span>(<span style="color:#555555;font-weight:bold;">@NotNull</span> BufferedReader br) <span style="color:#008800;font-weight:bold;">throws</span> IOException { String str; <span style="color:#008800;font-weight:bold;">while</span> ((str = br.<span style="color:#0000cc;">readLine</span>()) != <span style="color:#008800;font-weight:bold;">null</span>) { <span style="color:#008800;font-weight:bold;">if</span> (str.<span style="color:#0000cc;">startsWith</span>(WEB_CONTENT)) { InputStream is = createConnection(str); <span style="color:#008800;font-weight:bold;">if</span> (is == <span style="color:#008800;font-weight:bold;">null</span>) <span style="color:#008800;font-weight:bold;">continue</span>; downloadFiles(<span style="color:#008800;font-weight:bold;">new</span> BufferedReader(<span style="color:#008800;font-weight:bold;">new</span> InputStreamReader(is))); } <span style="color:#008800;font-weight:bold;">else</span> { <span style="color:#333399;font-weight:bold;">int</span> idx; <span style="color:#008800;font-weight:bold;">while</span> ((idx = getUrlIndex(str)) > -<span style="color:#0000dd;font-weight:bold;">1</span>) { str = str.<span style="color:#0000cc;">substring</span>(idx); <span style="color:#333399;font-weight:bold;">int</span> jIdx = str.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">".jpg"</span>); <span style="color:#333399;font-weight:bold;">int</span> gIdx = str.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">".gif"</span>); <span style="color:#333399;font-weight:bold;">int</span> pIdx = str.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">".png"</span>); <span style="color:#333399;font-weight:bold;">int</span>[] indexes = { jIdx, gIdx, pIdx }; <span style="color:#333399;font-weight:bold;">int</span> index = Integer.<span style="color:#0000cc;">MAX_VALUE</span>; <span style="color:#008800;font-weight:bold;">for</span> (<span style="color:#333399;font-weight:bold;">int</span> i : indexes) { <span style="color:#008800;font-weight:bold;">if</span> (i < index && i > -<span style="color:#0000dd;font-weight:bold;">1</span>) index = i; } <span style="color:#008800;font-weight:bold;">if</span> (index != Integer.<span style="color:#0000cc;">MAX_VALUE</span> && index > -<span style="color:#0000dd;font-weight:bold;">1</span>) { String url = str.<span style="color:#0000cc;">substring</span>(<span style="color:#0000dd;font-weight:bold;">0</span>, idx = (index + <span style="color:#0000dd;font-weight:bold;">4</span>)); download(url, <span style="background-color:#fff0f0;">"imgs/"</span>, url.<span style="color:#0000cc;">substring</span>(url.<span style="color:#0000cc;">lastIndexOf</span>(<span style="background-color:#fff0f0;">"/"</span>) + <span style="color:#0000dd;font-weight:bold;">1</span>)); str = str.<span style="color:#0000cc;">substring</span>(idx); } <span style="color:#008800;font-weight:bold;">else</span> { str = str.<span style="color:#0000cc;">substring</span>(<span style="color:#0000dd;font-weight:bold;">7</span>); } } } } } <span style="color:#008800;font-weight:bold;">private</span> <span style="color:#333399;font-weight:bold;">int</span> <span style="color:#0066bb;font-weight:bold;">getUrlIndex</span>(<span style="color:#555555;font-weight:bold;">@NotNull</span> String line) { <span style="color:#333399;font-weight:bold;">int</span> httpIdx = line.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">"http://"</span>); <span style="color:#333399;font-weight:bold;">int</span> httpsIdx = line.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">"https://"</span>); <span style="color:#008800;font-weight:bold;">if</span> (httpIdx > -<span style="color:#0000dd;font-weight:bold;">1</span> && httpsIdx > -<span style="color:#0000dd;font-weight:bold;">1</span>) { <span style="color:#008800;font-weight:bold;">if</span> (httpIdx < httpsIdx) <span style="color:#008800;font-weight:bold;">return</span> httpIdx; <span style="color:#008800;font-weight:bold;">else</span> <span style="color:#008800;font-weight:bold;">return</span> httpsIdx; } <span style="color:#008800;font-weight:bold;">else</span> { <span style="color:#008800;font-weight:bold;">if</span> (httpIdx > -<span style="color:#0000dd;font-weight:bold;">1</span>) <span style="color:#008800;font-weight:bold;">return</span> httpIdx; <span style="color:#008800;font-weight:bold;">else</span> <span style="color:#008800;font-weight:bold;">return</span> httpsIdx; } } <span style="color:#555555;font-weight:bold;">@Nullable</span> <span style="color:#008800;font-weight:bold;">private</span> InputStream <span style="color:#0066bb;font-weight:bold;">createConnection</span>(<span style="color:#555555;font-weight:bold;">@NotNull</span> String uri) <span style="color:#008800;font-weight:bold;">throws</span> IOException { URL u = <span style="color:#008800;font-weight:bold;">new</span> URL(uri); HttpURLConnection conn = (HttpURLConnection) u.<span style="color:#0000cc;">openConnection</span>(); conn.<span style="color:#0000cc;">setRequestProperty</span>(<span style="background-color:#fff0f0;">"User-Agent"</span>, USER_AGENT); conn.<span style="color:#0000cc;">getResponseCode</span>(); InputStream err = conn.<span style="color:#0000cc;">getErrorStream</span>(); <span style="color:#008800;font-weight:bold;">if</span> (err == <span style="color:#008800;font-weight:bold;">null</span>) { conn.<span style="color:#0000cc;">connect</span>(); <span style="color:#008800;font-weight:bold;">return</span> conn.<span style="color:#0000cc;">getInputStream</span>(); } <span style="color:#008800;font-weight:bold;">try</span> (Scanner scan = <span style="color:#008800;font-weight:bold;">new</span> Scanner(err)) { scan.<span style="color:#0000cc;">useDelimiter</span>(<span style="background-color:#fff0f0;">"\\z"</span>); System.<span style="color:#0000cc;">out</span>.<span style="color:#0000cc;">println</span>(scan.<span style="color:#0000cc;">next</span>()); <span style="color:#008800;font-weight:bold;">return</span> <span style="color:#008800;font-weight:bold;">null</span>; } } <span style="color:#555555;font-weight:bold;">@Nullable</span> <span style="color:#008800;font-weight:bold;">private</span> File <span style="color:#0066bb;font-weight:bold;">download</span>(<span style="color:#555555;font-weight:bold;">@NotNull</span> String uri, <span style="color:#555555;font-weight:bold;">@NotNull</span> String dir, <span style="color:#555555;font-weight:bold;">@NotNull</span> String name) <span style="color:#008800;font-weight:bold;">throws</span> IOException { File dirFile = <span style="color:#008800;font-weight:bold;">new</span> File(dir); <span style="color:#008800;font-weight:bold;">if</span> (!dirFile.<span style="color:#0000cc;">exists</span>() && !dirFile.<span style="color:#0000cc;">mkdirs</span>()) <span style="color:#008800;font-weight:bold;">return</span> <span style="color:#008800;font-weight:bold;">null</span>; List<String> files = Arrays.<span style="color:#0000cc;">asList</span>(dirFile.<span style="color:#0000cc;">list</span>()); <span style="color:#333399;font-weight:bold;">int</span> i = <span style="color:#0000dd;font-weight:bold;">0</span>; <span style="color:#333399;font-weight:bold;">boolean</span> exists; <span style="color:#008800;font-weight:bold;">do</span> { <span style="color:#333399;font-weight:bold;">boolean</span> e = <span style="color:#008800;font-weight:bold;">false</span>; <span style="color:#008800;font-weight:bold;">for</span> (String file : files) { <span style="color:#008800;font-weight:bold;">if</span> (i > <span style="color:#0000dd;font-weight:bold;">0</span>) { StringBuilder sb = <span style="color:#008800;font-weight:bold;">new</span> StringBuilder(name); <span style="color:#008800;font-weight:bold;">if</span> ((sb.<span style="color:#0000cc;">insert</span>(sb.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">"."</span>), <span style="background-color:#fff0f0;">"_"</span>).<span style="color:#0000cc;">insert</span>(sb.<span style="color:#0000cc;">lastIndexOf</span>(<span style="background-color:#fff0f0;">"_"</span>) + <span style="color:#0000dd;font-weight:bold;">1</span>, i).<span style="color:#0000cc;">toString</span>()).<span style="color:#0000cc;">equals</span>(file)) { i++; e = <span style="color:#008800;font-weight:bold;">true</span>; <span style="color:#008800;font-weight:bold;">break</span>; } } <span style="color:#008800;font-weight:bold;">else</span> { <span style="color:#008800;font-weight:bold;">if</span> (name.<span style="color:#0000cc;">equals</span>(file)) { i++; e = <span style="color:#008800;font-weight:bold;">true</span>; <span style="color:#008800;font-weight:bold;">break</span>; } } } exists = e; } <span style="color:#008800;font-weight:bold;">while</span> (exists); <span style="color:#008800;font-weight:bold;">if</span> (i > <span style="color:#0000dd;font-weight:bold;">0</span>) { StringBuilder sb = <span style="color:#008800;font-weight:bold;">new</span> StringBuilder(name); name = sb.<span style="color:#0000cc;">insert</span>(sb.<span style="color:#0000cc;">indexOf</span>(<span style="background-color:#fff0f0;">"."</span>), <span style="background-color:#fff0f0;">"_"</span>).<span style="color:#0000cc;">insert</span>(sb.<span style="color:#0000cc;">lastIndexOf</span>(<span style="background-color:#fff0f0;">"_"</span>) + <span style="color:#0000dd;font-weight:bold;">1</span>, i).<span style="color:#0000cc;">toString</span>(); } <span style="color:#008800;font-weight:bold;">return</span> <span style="color:#0066bb;font-weight:bold;">download</span>(uri, dir + name); } <span style="color:#555555;font-weight:bold;">@NotNull</span> <span style="color:#008800;font-weight:bold;">private</span> File <span style="color:#0066bb;font-weight:bold;">download</span>(<span style="color:#555555;font-weight:bold;">@NotNull</span> String uri, <span style="color:#555555;font-weight:bold;">@NotNull</span> String path) <span style="color:#008800;font-weight:bold;">throws</span> IOException { System.<span style="color:#0000cc;">out</span>.<span style="color:#0000cc;">println</span>(<span style="background-color:#fff0f0;">"SEND >>>> "</span> + uri); URL u = <span style="color:#008800;font-weight:bold;">new</span> URL(uri); HttpURLConnection conn = (HttpURLConnection) u.<span style="color:#0000cc;">openConnection</span>(); conn.<span style="color:#0000cc;">setRequestProperty</span>(<span style="background-color:#fff0f0;">"Content-Type"</span>, <span style="background-color:#fff0f0;">"application/octet-stream"</span>); conn.<span style="color:#0000cc;">setRequestProperty</span>(<span style="background-color:#fff0f0;">"User-Agent"</span>, USER_AGENT); conn.<span style="color:#0000cc;">setDoInput</span>(<span style="color:#008800;font-weight:bold;">true</span>); conn.<span style="color:#0000cc;">connect</span>(); InputStream is = conn.<span style="color:#0000cc;">getInputStream</span>(); File file = <span style="color:#008800;font-weight:bold;">new</span> File(path); OutputStream os = <span style="color:#008800;font-weight:bold;">new</span> FileOutputStream(file); <span style="color:#333399;font-weight:bold;">byte</span>[] buff = <span style="color:#008800;font-weight:bold;">new</span> <span style="color:#333399;font-weight:bold;">byte</span>[<span style="color:#005588;font-weight:bold;">0x400</span>]; <span style="color:#333399;font-weight:bold;">int</span> length; <span style="color:#008800;font-weight:bold;">while</span> ((length = is.<span style="color:#0000cc;">read</span>(buff)) > <span style="color:#0000dd;font-weight:bold;">0</span>) os.<span style="color:#0000cc;">write</span>(buff, <span style="color:#0000dd;font-weight:bold;">0</span>, length); os.<span style="color:#0000cc;">close</span>(); is.<span style="color:#0000cc;">close</span>(); conn.<span style="color:#0000cc;">disconnect</span>(); System.<span style="color:#0000cc;">out</span>.<span style="color:#0000cc;">println</span>(<span style="background-color:#fff0f0;">"FILE <<<< "</span> + path + <span style="background-color:#fff0f0;">"("</span> + file.<span style="color:#0000cc;">length</span>() + <span style="background-color:#fff0f0;">" byte)"</span>); <span style="color:#008800;font-weight:bold;">return</span> file; } }</pre></td></tr></tbody></table></div> <div><br></div> <div> <div style="text-align:left;"><img src="http://thimg.todayhumor.co.kr/upfile/201511/1446945175axnfQWmovZHqKp2.png" width="677" height="443" alt="제목 없음.png" style="border:none;"></div></div> <div><br></div> <div><br></div> <div>오늘 아침에 하나하나 저장하기가 귀찮아서 시작했는데 재밌게 짰네여..</div> <div><br></div> <div>사용법은 텍스트 파일 안에 꼬릿말 url 또는 이미지파일 url을 입력해서 저장하고 커멘드에 해당 txt파일 입력..</div> <div><br></div> <div>커멘드에 txt파일 여러개 가능, txt파일 내에 url 여러줄 가능..</div> <div><br></div> <div>가져다 쓰시는거, 자기가 짠거인척 하는거(..)까지 완전히 자유입니다</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.