[Util] Download Utility

Discussion in 'Resources' started by sebasju1234, Jan 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    sebasju1234

    Hello guys,

    I made a simple download utility that you can use in your plugin, but I shall only use it for private plugins because I don't think that your plugin will get accepted if you can download a file with it.

    You can use my utility like this:
    Code:java
    1. DownloadUtil util = new DownloadUtil(this, getDataFolder(), "[url]http://dev.bukkit.org/media/files/739/931/worldedit-5.5.8.jar[/url]");
    2.  
    3. try {
    4. util.downloadFile(new File("test.jar"));
    5. } catch (IOException e) {
    6. e.printStackTrace();
    7. }


    This is the utility class:
    Code:java
    1. import java.io.File;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.net.URL;
    5. import java.nio.channels.Channels;
    6. import java.nio.channels.ReadableByteChannel;
    7.  
    8. import com.github.sebasju1234.resources.ResourcePlugin;
    9.  
    10. public class DownloadUtil {
    11.  
    12. private ResourcePlugin plugin;
    13. private File directory;
    14. private String url;
    15.  
    16. /**
    17.   * @param instance
    18.   * @param directory
    19.   * @param url
    20.   */
    21. public DownloadUtil(ResourcePlugin instance, File directory, String url) {
    22. setPlugin(instance);
    23. setDirectory(directory);
    24. setUrl(url);
    25.  
    26. checkFiles();
    27. }
    28.  
    29. /**
    30.   * @return
    31.   */
    32. private ResourcePlugin getPlugin() {
    33. return plugin;
    34. }
    35.  
    36. /**
    37.   * @param plugin
    38.   */
    39. private void setPlugin(ResourcePlugin plugin) {
    40. this.plugin = plugin;
    41. }
    42.  
    43. /**
    44.   * @return
    45.   */
    46. private File getDirectory() {
    47. return directory;
    48. }
    49.  
    50. /**
    51.   * @param directory
    52.   */
    53. private void setDirectory(File directory) {
    54. this.directory = directory;
    55. }
    56.  
    57. /**
    58.   * @return
    59.   */
    60. private String getUrl() {
    61. return url;
    62. }
    63.  
    64. /**
    65.   * @param url
    66.   */
    67. private void setUrl(String url) {
    68. this.url = url;
    69. }
    70.  
    71. private void checkFiles() {
    72. if (getDirectory().exists()) {
    73. getDirectory().mkdirs();
    74. }
    75. }
    76.  
    77. /**
    78.   * @param path
    79.   * @throws IOException
    80.   */
    81. public void downloadFile(File path) throws IOException {
    82. if(!(path.exists())) {
    83. URL downloadUrl = new URL(getUrl());
    84. ReadableByteChannel readableByteChannel = Channels.newChannel(downloadUrl.openStream());
    85. FileOutputStream fileOutputStream = new FileOutputStream(path);
    86. fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
    87.  
    88. plugin.getLogger().info("[DOWNLOAD] File has been download. (" + path.toString() + ")");
    89. }
    90. }
    91.  
    92. }


    You have to change ResourcePlugin to your plugin instance. Tips and suggestions are welcome.
    - Sebastiaan
     
    Wizehh and MrInspector like this.
  2. Offline

    MrInspector

    Will be useful to a lot of people. :)
     
    sebasju1234 likes this.
  3. Offline

    sebasju1234

    That's why I made it hehe :p
     
    MrInspector likes this.
  4. Offline

    ZeusAllMighty11

  5. Offline

    breezeyboy

Thread Status:
Not open for further replies.

Share This Page