简体   繁体   English

Java-从Dropbox获取YAML文件?

[英]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. 我想知道如何在保管箱中获取YAML文件并将其存储为Java中的YamlConfiguration对象。 This is for a Bukkit plugin, so the Plugin object is part of the API. 这是针对Bukkit插件的,因此Plugin对象是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? 我如何从Dropbox中获取此文件,以及如何使用更新的信息重新上传该文件? https://www.dropbox.com/s/hv8yhz0grci8xpl/punishments.yml 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: 不确定我是否了解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;
}

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. 您可以通过downloadFile(“ www.urlhere.com/data.dat”,“ FileToSaveAs.dat”)运行它,如果成功,它将返回true;如果失败,则返回false。 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: 至于更新文件,您有3种选择:

  • Install Dropbox on the server running Bukkit, and copy your edited file to the Dropbox directory and let Dropbox handle uploading it 在运行Bukkit的服务器上安装Dropbox,然后将编辑后的文件复制到Dropbox目录中,并让Dropbox处理上传
  • Consult the Dropbox API. 咨询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. 这是比上述解决方案更好的解决方案,但是需要花费更多的精力来学习API,并且可能会增加文件大小。
  • Use a solution other than Dropbox. 使用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. Dropbox主要用于通过Web与朋友和移动设备共享文件,不应将其用作服务器的存储设备。 You can do some research on FTP. 您可以对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. 就个人而言,我会编写一个小的PHP脚本,以附加到Web服务器上的惩罚文件(yaml)上(假设您有一个Bukkit服务器),然后在需要时下载它,尽管考虑到我可能这不是最佳解决方案,甚至不知道惩罚是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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