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;
}
* 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;
}
相关文章推荐
- Tomcat JNDI 数据库用户名和密码加密(数据库连接池用户和密码加密)
- Apache与Tomcat的区别
- Apache与Tomcat的区别
- ubuntu 12.04 安装 tomcat
- 如何修改tomcat7的端口和用户名密码?
- tomcat Connector NIO的配置
- tomcat Connector NIO的配置
- 关于服务器(自己写的tomcat小服务器程序)应答浏览器请求信息
- 关于浏览器向服务端(自己写的个tomcat小服务器程序,仅供测试)发送请求的具体内容
- centos安装tomcat7
- tomcat的内部原理
- How to Install Tomcat
- How to Install Tomcat
- 【原创】Apache和Tomcat实现动静分离
- tomcat环境变量的配置
- 将MyEclipse上的项目部署到Tomcat上,并运行
- Tomcat环境搭建
- eclipse部署web项目至本地的tomcat但在webapps中找不到
- eclipse remote debug tomcat
- Linux下搭建web测试环境(安装JDK和Tomcat)