当前位置: 代码迷 >> J2SE >> FTP自动上传,该如何处理
  详细解决方案

FTP自动上传,该如何处理

热度:3   发布时间:2016-04-23 21:06:59.0
FTP自动上传
在指定目录下如果有一个新的文件就上传到服务器中  怎么实现
------解决方案--------------------
用apache包:

package com.test.project.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * 支持断点续传的FTP实用类 实现基本断点上传下载,支持上传下载进度
 * 
 * @author guishuanglin 2009-04-2
 */
public class MyFTPClient {
private Log logger = LogFactory.getLog(this.getClass());
private String fileSplit = "/";
private FTPClient ftpClient = new FTPClient();

public MyFTPClient() {
// 设置将过程中使用到的命令输出到控制台
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
}

/**
 * 连接到FTP服务器
 * 
 * @param hostname
 *            主机名
 * @param port
 *            端口
 * @param username
 *            用户名
 * @param password
 *            密码
 * @return 是否连接成功
 * @throws IOException
 */
public boolean connect(String hostname, int port, String username,
String password, String charEncoding) throws IOException {
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding(charEncoding);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
return true;
}
}
disconnect();
return false;
}


/**
 * 从FTP服务器上下载文件,支持断点续传.
 * @param fileName 文件名,如:file.txt
 * @param localPath 本地文件路径
 *   使用/home/der/dir或是
 *       http://www.baidu.com/dir
 * @param remotePath 远程文件路径,不包括文件名
 * @return 
 * @throws IOException
 */
public boolean download(String fileName, String localDir, String remoteDir)
throws IOException {
String localFile = localDir + this.fileSplit + fileName;
String remoteFile = remoteDir + this.fileSplit + fileName;
// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(remoteFile);
if (files.length < 1) {
logger.error("远程文件不存在");
return false;
}
long remoteSize = files[0].getSize();
File f = new File(localFile);
// 本地存在文件,进行断点下载
if (f.exists()) {
long localSize = f.length();
// 判断本地文件大小是否大于远程文件大小
if (localSize >= remoteSize) {
logger.error("本地文件大于远程文件,下载中止");
return true;
}
// 进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(remoteFile);

logger.info("下载文件开始");
boolean result = this.downloadFile(in, remoteSize, out, localSize,
ftpClient);
if (result) {
logger.info("下载文件完成");
return true;
} else {
logger.error("下载文件失败");
return false;
}
} else {
OutputStream out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(remoteFile);

logger.info("下载文件开始");
boolean result = this.downloadFile(in, remoteSize, out, 0,
ftpClient);
if (result) {
logger.info("下载文件完成");
return true;
} else {
logger.error("下载文件失败");
return false;
}
}
}

private boolean downloadFile(InputStream in, long inSize, OutputStream out,
long outSize, FTPClient ftpClient) throws IOException {
long step = inSize / 100;
long process = 0;
long readbytes = 0L;
//防止除0错误
if(step==0)
step = 1;
// 断点续传
if (outSize > 0) {
System.out.println("文件存在,可进行断点续传.");
ftpClient.setRestartOffset(outSize);
  相关解决方案