Struts Upload + Java I/O handle file.
解决方案 »
- struts2 sort标签
- (求助)tomcat部署更换一个新的war包后原来的项目文件被删除了怎样找回啊!
- 怎么设置java.io.tmpdir或则jfreechart图片不能显示问题
- 报错误拉啊。来看看是什么错误吧,再线等~~~~~~~~~~~~~~~~~~~~~java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLSer
- integer类型怎么计算
- 没分了 但是真的很郁闷 中文问题
- java.lang.NoClassDefFoundError: org/dom4j/DocumentException
- jsp怎样将图片上传到SQLServer2005数据库保存
- 郁闷,散分!
- 0基础入门java一个半月,求大神指点迷津
- 我想这样,如何做到
- 简单问题-------再线
* 拷贝路径下的文件到另外一个路径
*
* @param in
* @param out
*/
public static void copyPath(File srcDir, File outDir)
throws FileNotFoundException, IOException {final String src = srcDir.getPath();
final String dest = outDir.getPath();
if ((srcDir == null) || !srcDir.isDirectory()) {
return;
}
File[] entries = srcDir.listFiles();
int sz = entries.length;
for (int i = 0; i < sz; i++) {
if (entries[i].isDirectory()) {
// 路径相对于输入路径的路径
String relPath1 = entries[i].getPath().substring(src.length());
// 写入目标的路径
String writeDirPath = dest + relPath1;
// 建立文件夹
new File(writeDirPath).mkdirs();
// 递归
copyPath(entries[i], outDir);
} else {
// 源文件的路径
String srcFilePath = entries[i].getPath();
System.out.println("源文件路径:" + srcFilePath);
// 源文件相对与输入路径的路径
String relPath2 = entries[i].getPath().substring(src.length());
System.out.println("输入路径:" + src);
System.out.println("源文件相对与输入路径的路径:" + relPath2);
// 写入文件的路径
String writeFilePath = dest + relPath2;
System.out.println("写入路径:" + writeFilePath);
// 建立文件
new File(writeFilePath).createNewFile();
// 拷贝文件
copyFile(srcFilePath, writeFilePath);
}
}
}
* <h3>拷贝文件</h3>
* InputStream ----> OutputStream
*
* @param in
* @param out
* @param close
*/
public static void copyFile(InputStream in, OutputStream out, boolean close)
throws IOException { int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
if (close)
out.close();
}
* <h3>拷贝文件</h3>
* 输入路径 ----> 输出路径
*
* @param in
* @param out
* @throws FileNotFoundException
* @throws IOException
*/
public static void copyFile(String in, String out)
throws FileNotFoundException, IOException { BufferedInputStream is = new BufferedInputStream(new FileInputStream(in));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
out));
System.out.println("拷贝文件:" + in + "\n" + "CopTo>" + out);
System.out.println("\n");
copyFile(is, bos, true);
}