具体的不说啦,只是一个函数,很 easy的,调用方法如下:
try {
copySingleFile(list.get(i).getAttname(), filename,
pathBuilder.toString(), attkey);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
函数如下:
/**
* 文件复制业务逻辑实现
* @param srcFileName 源文件名
* @param destFileName 目标文件名
* @param rootPath 文件所操作路径
* @param attkey 文复制文件的数据KEY
*
* @return
* @throws BLException
* @throws IOException
* @throws FileNotFoundException
*/
public void copySingleFile(String srcFileName, String destFileName,String rootPath,String attkey) throws FileNotFoundException,
IOException, BLException {
// 目标文件
File destFile = new File(rootPath + attkey + "_" + destFileName);
// 源文件
File srcFile = new File(rootPath + srcFileName);
/** 源文件文件流取得 */
InputStream is = new FileInputStream(srcFile);
/** 输入文件流缓存取得 */
BufferedInputStream inBuffer = new BufferedInputStream(is);
/** 目标文件文件保存流取得 */
FileOutputStream out = new FileOutputStream(destFile);
/** 目标文件文件保存流缓存取得 */
BufferedOutputStream outBuffer = new BufferedOutputStream(out);
/** 复制文件保存 */
int contents = 0;
while ((contents = inBuffer.read()) != -1) {
outBuffer.write(contents);
}
outBuffer.flush();
// 文件流关闭
inBuffer.close();
outBuffer.close();
}