How To Download A File/Plugin

Discussion in 'Plugin Development' started by chenr1, Jul 13, 2013.

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

    chenr1

    So how would I go about downloading a file from a website? Like for instance a required plugin thats needed to work with my plugin. If thats not to confusing.
    TL;DR-Need to download file into plugin folder.
    Thanks Everyone!
     
  2. Offline

    xTrollxDudex

    chenr1
    Wrong forum go to bukkit help
     
  3. Offline

    ZeusAllMighty11



    I would set up an input stream for the web. I am not really good with that stuff, but you should google 'input streams' 'output streams' and 'grabbing files from internet, java'
     
  4. Offline

    xTrollxDudex

    TheGreenGamerHD
    That threw me off >:-(
     
  5. Offline

    Polaris29

    Try using Java NIO and FileChannels.

    Using transferFrom is potentially more efficient than a loop that reads from and writes to a channel. Many operating systems can transfer bytes directly from the source channel to the file system cache without copying them.

    Code:
    URL website = new URL("http://www.website.com/information.asp");
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream("information.html");
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    
    The third parameter in transferFrom() is the maximum amount of bytes to transfer. Integer.Max_VALUE will transfer at most 2^31 bytes and Long.MAX_VALUE will allow at most 2^63 bytes.


    And there's also this:
    Code:
    /**
      * Download.
      *
      * @param URL URL of the file to download
      * @param folder The folder to download it to (Relative or absolute path is acceptable)
      * @param name The new name of the file, leave null to use the filename in the URL.
      * @return true if succesful, false otherwise
      */
    public static boolean download(String URL, String folder, String name) {
        if (folder == null) return false;
     
        if (name == null) {
              String[] list = URL.replace("\\", "/").split("/");
              name = list[list.length-1];
        }
     
        if (folder.endsWith("/"))
              folder = folder + name;
        else
              folder = folder + "/" + name;
     
        try {
              java.net.URL url = new java.net.URL(URL);
              java.net.HttpURLConnection connection = (java.net.HttpURLConnection)                              url.openConnection();
     
              float totalDataRead=0;
     
              java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
              java.io.FileOutputStream fos = new java.io.FileOutputStream(new java.io.File(folder));
              java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(fos, 1024);
     
              byte[] data = new byte[1024];
              int i=0;
              while((i=in.read(data,0,1024))>=0) {
                  totalDataRead=totalDataRead+i;
                  bout.write(data,0,i);
              }
     
              bout.close();
              in.close();
              return true;
        } catch (Exception e) {
              return false;
        }
    }
    
     
  6. Offline

    chenr1

    Thanks so much. But i get errors in eclipse. Do i have to surround in try catch?
     
  7. Offline

    Polaris29

    chenr1
    Yeah.
    "URL website = new URL("http://www.website.com/information.asp");" throws a MalformedURLExcepion
    "website.openStream()" throws an IOException
    "FileOutputStream fos = new FileOutputStream("information.html");" throws a FileNotFoundException
    and the transferFrom throws an IOException.

    And the file in the FileOutputStream is the destination file, it can be a relative path, so just use something like "plugins/filename.jar".

    And the FileOutputStream needs to be closed after the transferFrom
     
Thread Status:
Not open for further replies.

Share This Page