Java -从dropbox获取YAML文件



我想知道如何在dropbox中获得YAML文件并将其存储为Java中的YamlConfiguration对象。这是针对Bukkit插件的,所以plugin对象是API的一部分。我现在拥有的代码只是本地的,它是:

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);
}

我该如何从dropbox获取这个文件,我该如何用更新的信息重新加载它?https://www.dropbox.com/s/hv8yhz0grci8xpl/punishments.yml

谢谢

我不确定我理解Bukkit API,但如果你想下载一个文件,我为你写了这个:

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;
}

你可以通过downloadFile("www.urlhere.com/data.dat", " filetosaveasa .dat")运行它,如果成功,它将返回true,如果失败,它将返回false。请注意,如果现有文件存在,它将覆盖它,如果连接中途中断,您将留下一个损坏的文件。

关于更新文件,您有3个选择:

  • 在运行Bukkit的服务器上安装Dropbox,并将您编辑的文件复制到Dropbox目录并让Dropbox处理上传
  • 咨询Dropbox API。这是一个比上面一个更好的解决方案,但需要更多的努力来学习API,并可能增加文件大小。
  • 使用Dropbox以外的解决方案。我最推荐这个。Dropbox主要用于通过网络与朋友和移动设备共享文件,不应该用作服务器的存储设备。你可以研究一下FTP。就我个人而言,我会编写一个小的PHP脚本来附加惩罚。在web服务器上(假设您有一个Bukkit服务器)的yaml文件,然后在需要时下载它,尽管这可能不是最好的解决方案,考虑到我甚至不知道什么惩罚。.

最新更新