Apache common FTP(七)

2014-11-24 02:43:15 · 作者: · 浏览: 4
// 如果远程目录不存在,则递归创建远程服务器目录

int start = 0;

int end = 0;

if (directory.startsWith("/")) {

start = 1;

} else {

start = 0;

}

end = directory.indexOf("/", start);

while (true) {

String subDirectory = new String(remote.substring(start, end)

.getBytes("GBK"), "iso-8859-1");

if (!ftpClient.changeWorkingDirectory(subDirectory)) {

if (ftpClient.makeDirectory(subDirectory)) {

ftpClient.changeWorkingDirectory(subDirectory);

} else {

log.info("创建目录失败");

return UploadStatus.CreateDirectoryFail;

}

}

start = end + 1;

end = directory.indexOf("/", start);

// 检查所有目录是否创建完毕

if (end <= start) {

break;

}

}

}

return status;

}

/**

* 上传文件到服务器,新上传和断点续传

* @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变

* @param localFile 本地文件File句柄,绝对路径

* @param processStep 需要显示的处理进度步进值

* @param ftpClient FTPClient引用

* @return

* @throws IOException

*/

public UploadStatus uploadFile(String remoteFile, File localFile,

FTPClient ftpClient, long remoteSize) throws IOException {

UploadStatus status;

// 显示进度的上传

System.out.println("localFile.length():"+localFile.length());

long step = localFile.length() / 100;

step = step==0 1:step;//文件过小,step可能为0

long process = 0;

long localreadbytes = 0L;

RandomAccessFile raf = new RandomAccessFile(localFile, "r");

OutputStream out = ftpClient.appendFileStream(new String(remoteFile

.getBytes("GBK"), "iso-8859-1"));

// 断点续传

if (remoteSize > 0) {

ftpClient.setRestartOffset(remoteSize);

process = remoteSize / step;

raf.seek(remoteSize);

localreadbytes = remoteSize;

}

byte[] bytes = new byte[1024];

int c;