mirror of https://e.coding.net/circlecloud/Yum.git
68 lines
2.0 KiB
Java
68 lines
2.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package cn.citycraft.Yum.utils;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.net.URL;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
/**
|
|
* @author 蒋天蓓
|
|
* 2015年8月21日下午6:08:09
|
|
* TODO
|
|
*/
|
|
public class DownloadUtils {
|
|
public static boolean download(CommandSender sender, String pluginname) {
|
|
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
|
|
BufferedInputStream in = null;
|
|
FileOutputStream fout = null;
|
|
if (sender == null)
|
|
sender = Bukkit.getConsoleSender();
|
|
try {
|
|
sender.sendMessage("开始下载: " + pluginname);
|
|
URL fileUrl = new URL(String.format(url, pluginname));
|
|
sender.sendMessage("下载地址: http://********/" + fileUrl.getFile());
|
|
int fileLength = fileUrl.openConnection().getContentLength();
|
|
sender.sendMessage("文件长度: " + fileLength);
|
|
in = new BufferedInputStream(fileUrl.openStream());
|
|
File file = new File("/plugins/", fileUrl.getFile());
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
sender.sendMessage("创建新文件: " + fileUrl.getFile());
|
|
}
|
|
fout = new FileOutputStream(file);
|
|
byte[] data = new byte[1024];
|
|
long downloaded = 0L;
|
|
int count;
|
|
long time = System.currentTimeMillis();
|
|
while ((count = in.read(data)) != -1) {
|
|
downloaded += count;
|
|
fout.write(data, 0, count);
|
|
double percent = downloaded / fileLength * 10000;
|
|
if (System.currentTimeMillis() - time > 1000) {
|
|
sender.sendMessage(String.format("已下载: ====================> %.2f%%", percent));
|
|
time = System.currentTimeMillis();
|
|
}
|
|
}
|
|
return true;
|
|
} catch (Exception ex) {
|
|
sender.sendMessage("插件下载失败!");
|
|
return false;
|
|
} finally {
|
|
try {
|
|
if (in != null) {
|
|
in.close();
|
|
fout.close();
|
|
}
|
|
} catch (Exception ex) {
|
|
sender.sendMessage("关闭数据流时发生错误!");
|
|
}
|
|
}
|
|
}
|
|
}
|