【问题标题】:Java - Getting YAML file from dropbox?Java - 从 Dropbox 获取 YAML 文件?
【发布时间】:2014-07-11 21:51:36
【问题描述】:

我想知道如何在 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

谢谢

【问题讨论】:

    标签: java yaml dropbox


    【解决方案1】:

    不确定我是否了解 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", "FileToSaveAs.dat") 运行它,如果成功则返回true,如果失败则返回false。请注意,如果现有文件存在,它将覆盖现有文件,如果连接中断,您将留下一个损坏的文件。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      相关资源
      最近更新 更多