[英]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种选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.