简体   繁体   中英

Java - Getting YAML file from dropbox?

I would like to know how to get a YAML file in dropbox and store it as a YamlConfiguration Object in Java. This is for a Bukkit plugin, so the Plugin object is part of the API. The code I have right now is only local, here it is:

private File cfile;
private FileConfiguration config;
private Plugin p;
//setup
public void setup(Plugin p){
  this.p = p;
  cfile = new File(p.getDataFolder(), "punishments.yml");
  config = YamlConfiguration.loadConfiguration(cfile);
  config.save(cfile);
}

How would I get this file from dropbox, and how would I reupload it with updated information? https://www.dropbox.com/s/hv8yhz0grci8xpl/punishments.yml

Thanks

Not sure I understand the Bukkit API, but if you're looking to download a file, I wrote this for you:

public static boolean downloadFile(String urlStr, String fileStr)
{
    boolean success = true;

    InputStream urlStream = null;
    BufferedInputStream iStream = null;
    FileOutputStream fOutput = null;
    try
    {
        URL url = new URL(urlStr);
        urlStream = url.openStream();
        iStream = new BufferedInputStream(urlStream);
        fOutput = new FileOutputStream(new File(fileStr));
        byte[] buffer = new byte[512];
        while(iStream.read(buffer) != -1)
        {
            fOutput.write(buffer);
        }
    } catch (IOException ioe)
    {
        success = false;
        ioe.printStackTrace();
    }
    finally
    {
        if(fOutput != null)
            try
            {
                fOutput.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        if(iStream != null)
            try
            {
                iStream.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        if(urlStream != null)
            try
            {
                urlStream.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
    }

    return success;
}

You can run it via downloadFile("www.urlhere.com/data.dat", "FileToSaveAs.dat"), and it will return true if it succeded and false if it failed. Be aware that it will overwrite the existing file if it exists, and if the connection breaks halfway through, you'll be left with a broken file.

As for updating the file, you have 3 choices:

  • Install Dropbox on the server running Bukkit, and copy your edited file to the Dropbox directory and let Dropbox handle uploading it
  • Consult the Dropbox API. This is a better solution than the one above, but takes more effort to learn the API and will likely increase file size.
  • Use a solution other than Dropbox. I suggest this one the most. Dropbox is mainly for sharing files over the web to friends and mobile devices, and shouldn't be used as a server's storage device. You can do some research on FTP. Personally, I would write a small PHP script to append to a punishments.yaml file on a web server (assuming you have one for the Bukkit server) and then download it whenever needed, although this may not be the best solution considering I don't even know what punishments.yaml is used for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM