[Tutorial] - Download files with your plugin.

Discussion in 'Resources' started by Retherz_, Sep 5, 2013.

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

    Retherz_

    it is very simple, first you need this:
    Code:java
    1.  
    2. public static void downloadFile(String link, String directory) throws IOException {
    3. URL url = new URL(link);
    4. InputStream is = url.openStream();
    5. OutputStream os = new FileOutputStream(directory);
    6.  
    7. byte[] b = new byte[2048];
    8. int length;
    9.  
    10. while ((length = is.read(b)) != -1) {
    11. os.write(b, 0, length);
    12. }
    13.  
    14. is.close();
    15. os.close();
    16. }
    17.  

    Then you can use it in like your onEnable:

    try {
    downloadFile("http://dev.bukkit.org/media/files/729/547/SpamBlocker.jar", "plugins/plugin.jar");
    } catch (IOException e) {
    e.printStackTrace();
    }

    Example:
    Code:java
    1.  
    2.  
    3. public void onEnable() {
    4. Bukkit.getPluginManager().registerEvents(this, this);
    5.  
    6. try {
    7. downloadFile("[url]http://dev.bukkit.org/media/files/729/547/SpamBlocker.jar[/url]", "plugins/plugin.jar");
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. }
    11. }
     
  2. I'm not quite sure how that has anything to do with anything, guessing it's just a shameless self-bump, but anyways, any plugin using this in a BukkitDev project will be denied.. I guess this is a good thing for private (and "trusted") plugins, but not so much for public ones.
     
  3. Offline

    Retherz_

    Yeah, well it could be used to troll someone or basicly keep downloading files with different names which does the same thing
     
  4. Offline

    Deleted user


    or... it could be used in a positive manner! I personally plan on using this for downloading absent dependencies. Also if someone should use this in a malicious form, the project wouldn't be accepted on BukkitDev. (If you use a plugin and it's from from a trusted developer or BukkitDev, then you're at your own risk)
     
    bobacadodl likes this.
  5. Offline

    Retherz_

    Yes, i know i made it for positive stuff, this could be very useful for a lot of people ;)
     
  6. Offline

    Garris0n

    I'll probably be using this for something at some point :)
     
    MercilessPvP likes this.
  7. Offline

    Retherz_

    If you want to use this to download a plugin (If your plugin requires it);
    try this:
    Code:java
    1.  
    2. File pluginDirectory = new File("plugins/example.jar");
    3. if(!pluginDirectory.exists()){
    4. try {
    5. downloadFile("[url]http://dev.bukkit.org/media/files/729/547/SpamBlocker.jar[/url]", "plugins/example.jar");
    6. } catch (IOException e) {
    7. System.out.println("Download failed! :(");
    8. }



    Haven't tried it, should work though.
     
  8. Offline

    waremanu

    How can I replace the downloaded file if the file already exist?
     
  9. Offline

    Garris0n

    Something like
    Code:java
    1. if(file.exists())
    2. file.delete();
    3. download(url, file.getPath());

    Play around with it.


    Well, for one, tag me or I'm not going to see what you said. Anyway, what doesn't work? Errors? Post what you used?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    waremanu likes this.
Thread Status:
Not open for further replies.

Share This Page