【发布时间】:2015-04-22 07:46:21
【问题描述】:
我有许多资源文件想作为方法调用复制到另一个位置。有各种方法和指南,以及关于如何从 JAR 读取/复制文件的问题。但是,如果我没有完全关闭,与在开发/调试时从 Eclipse 运行代码相比,当代码从已部署的 JAR 运行时,standard ways of getting a resource 将查看不同的位置。所以适用于一种情况的解决方案不适用于另一种情况..
我当前且极其冗长的解决方案(见下文)有效,但维护起来很丑陋、冗长且烦人(添加或删除附加文件)。 我有什么方法可以优化这个方法,(可能使用 NIO 包中的新巫术),例如只需循环 /res 文件夹中的文件并将它们全部复制到新位置。我'不确定如何避免为 getResource() 指定文件名和路径在这种情况下是否可行。
private void loadRes() throws IOException{
// get styling resources from the jar file
File htmlRes = new File(topfolder, "res");
if(!htmlRes.exists() || !htmlRes.isDirectory())
htmlRes.mkdir();
File cssfile = new File(htmlRes, "style.css"),
bullet_file = new File(htmlRes,"i.gif"),
banner_file = new File(htmlRes, "banner.png"),
bg_file = new File(htmlRes,"body_bg.png"),
img_bg = new File(htmlRes,"button-bg.png"),
hov_bg = new File(htmlRes,"button-hover-bg.png"),
visu_img = new File(htmlRes,"visu.png"),
pc_img = new File(htmlRes,"pc.png"),
asc_gif = new File(htmlRes,"asc.gif"),
desc_gif = new File(htmlRes,"desc.gif"),
bg_gif = new File(htmlRes,"bg.gif");
URL css_url = ExportUtils.class.getResource("/res/style.css");
URL bullet_url = ExportUtils.class.getResource("/res/i.gif");
URL banner_url = ExportUtils.class.getResource("/res/banner.png");
URL bg_url = ExportUtils.class.getResource("/res/body_bg.png");
URL image1_url = ExportUtils.class.getResource("/res/button-bg.png");
URL image2_url = ExportUtils.class.getResource("/res/button-hover-bg.png");
URL visu_url = ExportUtils.class.getResource("/res/visu.png");
URL pc_url = ExportUtils.class.getResource("/res/pc.png");
URL asc_url = ExportUtils.class.getResource("/res/asc.gif");
URL desc_url = ExportUtils.class.getResource("/res/desc.gif");
URL bggif_url = ExportUtils.class.getResource("/res/bg.gif");
FileOutputStream fos = new FileOutputStream(cssfile);
Resources.copy(css_url, fos);
fos = new FileOutputStream(bullet_file);
Resources.copy(bullet_url, fos);
fos = new FileOutputStream(banner_file);
Resources.copy(banner_url, fos);
fos = new FileOutputStream(bg_file);
Resources.copy(bg_url, fos);
fos = new FileOutputStream(img_bg);
Resources.copy(image1_url, fos);
fos = new FileOutputStream(hov_bg);
Resources.copy(image2_url, fos);
fos = new FileOutputStream(visu_img);
Resources.copy(visu_url, fos);
fos = new FileOutputStream(pc_img);
Resources.copy(pc_url, fos);
fos = new FileOutputStream(asc_gif);
Resources.copy(asc_url, fos);
fos = new FileOutputStream(desc_gif);
Resources.copy(desc_url, fos);
fos = new FileOutputStream(bg_gif);
Resources.copy(bggif_url, fos);
// scripts and finish head
URL sort_script = ExportUtils.class.getResource("/res/scripts.js");
URL func_script = ExportUtils.class.getResource("/res/jquery.tablesorter.min.js");
URL scinot_parser = ExportUtils.class.getResource("/res/jquery.tablesorter.scinot.js");
File div_js = new File(htmlRes,"scripts.js");
File jquery_sorttable = new File(htmlRes,"jquery.tablesorter.min.js");
File jquery_st_scinot = new File(htmlRes, "jquery.tablesorter.scinot.js");
fos = new FileOutputStream(div_js);
Resources.copy(sort_script, fos);
fos = new FileOutputStream(jquery_sorttable);
Resources.copy(func_script, fos);
fos = new FileOutputStream(jquery_st_scinot);
Resources.copy(scinot_parser, fos);
}
【问题讨论】:
标签: java deployment io nio