因为涉及到FTP上的附件迁移,需要将一个文件夹下的文件移动到另外一个文件夹,考虑到历史数据的备份,所以就没有使用FTPClient 的rename方法。
此处采用递归的方式备份的文件夹及文件。
/**
* 复制文件.
*
* @param sourceFileName
* @param targetFile
* @throws IOException
*/
public void copyFile(String sourceFileName, String sourceDir, String targetDir) throws IOException {
ByteArrayInputStream in = null;
ByteArrayOutputStream fos = new ByteArrayOutputStream();
try {
if (!existDirectory(targetDir)) {
createDirectory(targetDir);
}
ftpClient.setBufferSize(1024 * 2);
// 变更工作路径
ftpClient.changeWorkingDirectory(sourceDir);
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 将文件读到内存中
ftpClient.retrieveFile(new String(sourceFileName.getBytes("GBK"), "iso-8859-1"), fos);
in = new ByteArrayInputStream(fos.toByteArray());
if (in != null) {
ftpClient.changeWorkingDirectory(targetD