Download file from URL

Discussion in 'Plugin Development' started by Vilsol, Aug 20, 2012.

Thread Status:
Not open for further replies.
  1. I have thought of this idea for a while. Is it actually possible to download a file from somewhere. Example: I write in console "download www.example.com/example.random random/directory/here/" and the plugin will download a file from site "www.example.com/example.random" and save it to directory "random/directory/here/"

    Thanks in advance!
     
  2. Offline

    Kodfod

  3. Offline

    stelar7

    in other words, yes it is possible
     
  4. Im quite a newbie so dont shout on me...
    I made this script but for some reason when i run a command: "download example.jpg http://upload.wikimedia.org/wikipedia/en/8/87/Example.JPG plugins" the server just stops :( (It can be any file, that was just an example)

    Code:
    Code:
    package com.vilsol.downurl;
     
    import java.io.*;
    import java.net.*;
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class DownURL extends JavaPlugin {
     
        public static DownURL plugin;
     
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable(){
            this.logger.info("DownURL Enabled!");
        }
     
        public void onDisable(){
            this.logger.info("DownURL Disabled!");
        }
     
     
        public void saveUrl(String filename, String urlString, String directory) throws MalformedURLException, IOException
        {
            BufferedInputStream in = null;
            FileOutputStream fout = null;
            try
            {
                    in = new BufferedInputStream(new URL(urlString).openStream());
                    fout = new FileOutputStream(directory + "\\" + filename);
     
                    byte data[] = new byte[1024];
                    int count;
                    while ((count = in.read(data, 0, 1024)) != -1)
                    {
                            fout.write(data, 0, count);
                    }
            }
            finally
            {
                    if (in != null)
                            in.close();
                    if (fout != null)
                            fout.close();
            }
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("download")){
                try {
                    saveUrl(args[1], args[2], args[3]);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
                return true;
            }else{
                return false;
            }
        }
     
        public static String[] RearangeString(int startIndex, String[] string)
          {
            String TMPString = "";
            for (int i = startIndex; i < string.length; i++) {
              String Add = " ";
              if (i == startIndex) {
                Add = "";
              }
              TMPString = TMPString + Add + string[i];
            }
            return TMPString.split("\\ ");
          }
    }
     
  5. Offline

    stelar7

    one thing to note is that the file will be downloaded onto the server, not the client!

    Working code that dosent stop the server
    Code:
    package test;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Runnable {
     
        URL file;
        File dest = new File("plugins/stuff");
     
        public void onEnable() {
     
        }
     
        public void onDisable() {
     
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            try {
                file = new URL(args[0]);
                Thread th = new Thread(this);
                th.start();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        return false;
        }
     
        @Override
        public void run() {
            download(file, dest);
        }
     
        public void download(URL file, File dest) {
            try {
                InputStream is = file.openStream();
                File finaldest = new File(dest + "/" + file.getFile());
                finaldest.getParentFile().mkdirs();
                finaldest.createNewFile();
                OutputStream os = new FileOutputStream(finaldest);
                byte data[] = new byte[1024];
                int count;
                while ((count = is.read(data, 0, 1024)) != -1) {
                    os.write(data, 0, count);
                }
                os.flush();
                is.close();
                os.close();
            } catch (Exception ec) {
                ec.printStackTrace();
            }
        }
    }
    
    note: could most likely be improved
     
Thread Status:
Not open for further replies.

Share This Page