public boolean uploadFile(String remoteFile,File localFile) throws IOException {
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
FileInputStream fis = new FileInputStream(localFile);
boolean flag = ftpClient.storeFile(new String(remoteFile.getBytes("GBK"),"iso-8859-1"), fis);
fis.close();
return flag;
}
/**
* 上传文件到服务器,新上传和断点续传
*
* @param remoteFile
* 远程文件名,在上传之前已经将服务器工作目录做了改变
* @param localFile
* 本地文件 File句柄,尽对路径
* @param processStep
* 需要显示的处理进度步进值
* @param ftpClient
* FTPClient 引用
* @return
* @throws IOException
*/
public boolean uploadFile(String remoteFile, File localFile, long remoteSize)
throws IOException {
ftpClient.setSoTimeout(3000);
// UploadStatus status;
// 显示进度的上传
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile, "r");
OutputStream out = ftpClient.appendFileStream(remoteFile);
// 断点续传
if (remoteSize > 0) {
ftpClient.setRestartOffset(remoteSize);
if (step != 0) {
process = remoteSize / step;
}
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while ((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);
localreadbytes += c;
if (step != 0 && localreadbytes / step != process) {
process = localreadbytes / step;
// TODO 汇报上传状态
}
}
out.flush();
raf.close();
out.close();
boolean result = ftpClient.completePendingCommand();
return result;
}
FTPClient 上传
最新推荐文章于 2022-08-06 18:50:23 发布