Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Downloader.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.io.BufferedReader;
  24. import java.io.DataOutputStream;
  25. import java.io.File;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.InputStreamReader;
  30. import java.io.OutputStream;
  31. import java.net.MalformedURLException;
  32. import java.net.URL;
  33. import java.net.URLConnection;
  34. import java.net.URLEncoder;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * Allows easy downloading of files from HTTP sites.
  40. */
  41. public final class Downloader {
  42. /** Creates a new instance of Downloader. */
  43. private Downloader() {
  44. // Shouldn't be used
  45. }
  46. /**
  47. * Retrieves the specified page.
  48. *
  49. * @param url The URL to retrieve
  50. * @return A list of lines received from the server
  51. * @throws java.net.MalformedURLException If the URL is malformed
  52. * @throws java.io.IOException If there's an I/O error while downloading
  53. */
  54. public static List<String> getPage(final String url)
  55. throws MalformedURLException, IOException {
  56. return getPage(url, "");
  57. }
  58. /**
  59. * Retrieves the specified page, sending the specified post data.
  60. *
  61. * @param url The URL to retrieve
  62. * @param postData The raw POST data to send
  63. * @return A list of lines received from the server
  64. * @throws java.net.MalformedURLException If the URL is malformed
  65. * @throws java.io.IOException If there's an I/O error while downloading
  66. */
  67. public static List<String> getPage(final String url, final String postData)
  68. throws MalformedURLException, IOException {
  69. final List<String> res = new ArrayList<String>();
  70. final URLConnection urlConn = getConnection(url, postData);
  71. BufferedReader in = null;
  72. try {
  73. in = new BufferedReader(new InputStreamReader(
  74. urlConn.getInputStream()));
  75. String line;
  76. do {
  77. line = in.readLine();
  78. if (line != null) {
  79. res.add(line);
  80. }
  81. } while (line != null);
  82. } finally {
  83. StreamUtil.close(in);
  84. }
  85. return res;
  86. }
  87. /**
  88. * Retrieves the specified page, sending the specified post data.
  89. *
  90. * @param url The URL to retrieve
  91. * @param postData A map of post data that should be sent
  92. * @return A list of lines received from the server
  93. * @throws java.net.MalformedURLException If the URL is malformed
  94. * @throws java.io.IOException If there's an I/O error while downloading
  95. */
  96. public static List<String> getPage(final String url,
  97. final Map<String, String> postData)
  98. throws MalformedURLException, IOException {
  99. final StringBuilder data = new StringBuilder();
  100. for (Map.Entry<String, String> entry : postData.entrySet()) {
  101. data.append('&');
  102. data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  103. data.append('=');
  104. data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  105. }
  106. return getPage(url, data.length() == 0 ? "" : data.substring(1));
  107. }
  108. /**
  109. * Downloads the specified page to disk.
  110. *
  111. * @param url The URL to retrieve
  112. * @param file The file to save the page to
  113. * @throws java.io.IOException If there's an I/O error while downloading
  114. */
  115. public static void downloadPage(final String url, final String file)
  116. throws IOException {
  117. downloadPage(url, file, null);
  118. }
  119. /**
  120. * Downloads the specified page to disk.
  121. *
  122. * @param url The URL to retrieve
  123. * @param file The file to save the page to
  124. * @param listener The progress listener for this download
  125. * @throws java.io.IOException If there's an I/O error while downloading
  126. */
  127. public static void downloadPage(final String url, final String file,
  128. final DownloadListener listener) throws IOException {
  129. final URLConnection urlConn = getConnection(url, "");
  130. final File myFile = new File(file);
  131. OutputStream output = null;
  132. InputStream input = null;
  133. try {
  134. output = new FileOutputStream(myFile);
  135. input = urlConn.getInputStream();
  136. final int length = urlConn.getContentLength();
  137. int current = 0;
  138. if (listener != null) {
  139. listener.setIndeterminate(length == -1);
  140. }
  141. final byte[] buffer = new byte[512];
  142. int count;
  143. do {
  144. count = input.read(buffer);
  145. if (count > 0) {
  146. current += count;
  147. output.write(buffer, 0, count);
  148. if (listener != null && length != -1) {
  149. listener.downloadProgress(100 * (float) current
  150. / length);
  151. }
  152. }
  153. } while (count > 0);
  154. } finally {
  155. StreamUtil.close(input);
  156. StreamUtil.close(output);
  157. }
  158. }
  159. /**
  160. * Creates an URL connection for the specified URL and data.
  161. *
  162. * @param url The URL to connect to
  163. * @param postData The POST data to pass to the URL
  164. * @return An URLConnection for the specified URL/data
  165. * @throws java.net.MalformedURLException If the specified URL is malformed
  166. * @throws java.io.IOException If an I/O exception occurs while connecting
  167. */
  168. private static URLConnection getConnection(final String url,
  169. final String postData)
  170. throws MalformedURLException, IOException {
  171. final URL myUrl = new URL(url);
  172. final URLConnection urlConn = myUrl.openConnection();
  173. urlConn.setUseCaches(false);
  174. urlConn.setDoInput(true);
  175. urlConn.setDoOutput(postData.length() > 0);
  176. urlConn.setConnectTimeout(10000);
  177. if (postData.length() > 0) {
  178. urlConn.setRequestProperty("Content-Type",
  179. "application/x-www-form-urlencoded");
  180. DataOutputStream out = null;
  181. try {
  182. out = new DataOutputStream(urlConn.getOutputStream());
  183. out.writeBytes(postData);
  184. out.flush();
  185. } finally {
  186. StreamUtil.close(out);
  187. }
  188. }
  189. return urlConn;
  190. }
  191. }