将代码DownloadSomeData.java放在教程的Java Project中,代码如下:

    1. package com.alibaba.alink;
    2. import org.apache.commons.io.FileUtils;
    3. import javax.net.ssl.HttpsURLConnection;
    4. import javax.net.ssl.SSLContext;
    5. import java.io.File;
    6. import java.net.URL;
    7. /**
    8. * 本代码只能下载部分章节的数据。
    9. * 全部数据链接地址:https://www.yuque.com/pinshu/alink_tutorial/book_java_reference
    10. */
    11. public class DownloadSomeData {
    12. public static void main(String[] args) throws Exception {
    13. downloadUrl(
    14. "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
    15. Chap03.LOCAL_DIR
    16. );
    17. downloadUrl(
    18. "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv",
    19. Chap03.LOCAL_DIR
    20. );
    21. downloadUrl(
    22. "http://files.grouplens.org/datasets/movielens/ml-100k/u.data",
    23. Chap03.LOCAL_DIR
    24. );
    25. downloadUrl(
    26. "http://archive.ics.uci.edu/ml/machine-learning-databases/00267/data_banknote_authentication.txt",
    27. Chap08.DATA_DIR
    28. );
    29. downloadUrl(
    30. "http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data",
    31. Chap09.DATA_DIR
    32. );
    33. downloadUrl(
    34. "http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data",
    35. Chap10.DATA_DIR
    36. );
    37. downloadUrl(
    38. "http://alink-release.oss-cn-beijing.aliyuncs.com/data-files/action_log.csv",
    39. Chap11.DATA_DIR
    40. );
    41. downloadUrl(
    42. "http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
    43. Chap12.DATA_DIR
    44. );
    45. downloadUrl(
    46. "http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz",
    47. Chap13.DATA_DIR
    48. );
    49. downloadUrl(
    50. "http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz",
    51. Chap13.DATA_DIR
    52. );
    53. downloadUrl(
    54. "http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz",
    55. Chap13.DATA_DIR
    56. );
    57. downloadUrl(
    58. "http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz",
    59. Chap13.DATA_DIR
    60. );
    61. downloadUrl(
    62. "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv",
    63. Chap16.DATA_DIR
    64. );
    65. for (String fileName : new String[]{Chap24.ITEM_FILE, Chap24.USER_FILE,
    66. Chap24.RATING_FILE, Chap24.RATING_TRAIN_FILE, Chap24.RATING_TEST_FILE
    67. }) {
    68. downloadUrl(
    69. "http://files.grouplens.org/datasets/movielens/ml-100k/" + fileName,
    70. Chap24.DATA_DIR
    71. );
    72. }
    73. downloadUrlHttps(
    74. "https://raw.githubusercontent.com/tennessine/corpus/master/%E4%B8%89%E5%9B%BD%E6%BC%94%E4%B9%89.txt",
    75. Chap22.DATA_DIR
    76. );
    77. downloadUrlHttps(
    78. "https://github.com/BenDerPan/toutiao-text-classfication-dataset/raw/master/toutiao_cat_data.txt.zip",
    79. Chap21.DATA_DIR
    80. );
    81. }
    82. static synchronized void downloadUrl(String url, String dirPath) {
    83. try {
    84. URL httpUrl = new URL(url);
    85. File dir = new File(dirPath);
    86. if (!dir.exists()) {
    87. dir.mkdirs();
    88. }
    89. String fileName = url.substring(url.lastIndexOf("/") + 1);
    90. FileUtils.copyURLToFile(httpUrl, new File(dir, fileName));
    91. // FileUtils.copyURLToFile(httpUrl, new File(dir, fileName), 30000, 10000);
    92. System.out.println("Success @ " + url);
    93. } catch (Exception e) {
    94. System.err.println("Failed @ " + url);
    95. System.err.println(e.toString());
    96. }
    97. }
    98. static synchronized void downloadUrlHttps(String url, String dirPath) {
    99. try {
    100. URL httpsUrl = new URL(url);
    101. File dir = new File(dirPath);
    102. if (!dir.exists()) {
    103. dir.mkdirs();
    104. }
    105. String str = java.net.URLDecoder.decode(url, "UTF-8");
    106. String fileName = str.substring(str.lastIndexOf("/") + 1);
    107. SSLContext context = SSLContext.getInstance("TLS");
    108. context.init(null, null, null);
    109. HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    110. HttpsURLConnection connection = (HttpsURLConnection) httpsUrl.openConnection();
    111. FileUtils.copyURLToFile(connection.getURL(), new File(dir, fileName), 60000, 50000);
    112. System.out.println("Success @ " + url);
    113. } catch (Exception e) {
    114. System.err.println("Failed @ " + url);
    115. System.err.println(e.toString());
    116. }
    117. }
    118. }


    在Java Project中的位置及运行结果,如下面截图所示:
    1636104055(1).png