您的位置:首页 > 运维架构 > Tomcat

java 复制目录(来自tomcat6源代码)

2014-01-13 08:29 495 查看
/**

* Copy directory.

*/

private boolean copyDir(DirContext srcDir, File destDir) {

try {

NamingEnumeration enumeration = srcDir.list("");

while (enumeration.hasMoreElements()) {

NameClassPair ncPair =

(NameClassPair) enumeration.nextElement();

String name = ncPair.getName();

Object object = srcDir.lookup(name);

File currentFile = new File(destDir, name);

if (object instanceof Resource) {

InputStream is = ((Resource) object).streamContent();

OutputStream os = new FileOutputStream(currentFile);

if (!copy(is, os))

return false;

} else if (object instanceof InputStream) {

OutputStream os = new FileOutputStream(currentFile);

if (!copy((InputStream) object, os))

return false;

} else if (object instanceof DirContext) {

currentFile.mkdir();

copyDir((DirContext) object, currentFile);

}

}

} catch (NamingException e) {

return false;

} catch (IOException e) {

return false;

}

return true;

}

/**

* Copy a file to the specified temp directory. This is required only

* because Jasper depends on it.

*/

private boolean copy(InputStream is, OutputStream os) {

try {

byte[] buf = new byte[4096];

while (true) {

int len = is.read(buf);

if (len < 0)

break;

os.write(buf, 0, len);

}

is.close();

os.close();

} catch (IOException e) {

return false;

}

return true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: