- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- 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;
- public class JFtpApche
- {
- privatestatic FTPClient ftpClient = new FTPClient();
- privatestatic String encoding = System.getProperty("file.encoding");
- publicstatic boolean uploadFile(String url, int port, String username,String password, String path, String filename, InputStream input)
- {
- boolean result = false;
- try
- {
- int reply;
- // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
- ftpClient.connect(url);
- // ftp.connect(url, port);// 连接FTP服务器
- // 登录
- ftpClient.login(username, password);
- ftpClient.setControlEncoding(encoding);
- // 检验是否连接成功
- reply = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply))
- {
- System.out.println("连接失败");
- ftpClient.disconnect();
- return result;
- }
- // 转移工作目录至指定目录下
- boolean change = ftpClient.changeWorkingDirectory(path);
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- if (change)
- {
- result = ftpClient.storeFile(newString(filename.getBytes(encoding), "iso-8859-1"), input);
- if (result)
- {
- System.out.println("上传成功!");
- }
- }
- input.close();
- ftpClient.logout();
- } catch (IOException e)
- {
- e.printStackTrace();
- } finally
- {
- if (ftpClient.isConnected())
- {
- try
- {
- ftpClient.disconnect();
- } catch (IOException ioe)
- {
- }
- }
- }
- return result;
- }
- public voidtestUpLoadFromDisk()
- {
- try
- {
- FileInputStream in = new FileInputStream(newFile("./data/se/1-2015-02-03-14.list"));
- //boolean flag = uploadFile("127.0.0.1", 21, "zlb", "123", "/","哈哈.txt", in);
- boolean flag = uploadFile("10.10.2.55", 21, "anonymous", "", "/","1-2015-02-03-14.list", in);
- System.out.println(flag);
- } catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- publicstatic boolean downFile(String url, int port, String username,String password, String remotePath, String fileName, StringlocalPath)
- {
- boolean result = false;
- try
- {
- int reply;
- ftpClient.setControlEncoding(encoding);
- // FTPClientConfig conf = new
- // FTPClientConfig(FTPClientConfig.SYST_NT);
- // conf.setServerLanguageCode("zh");
- ftpClient.connect(url, port);
- // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
- ftpClient.login(username, password);// 登录
- // 设置文件传输类型为二进制
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- // 获取ftp登录应答代码
- reply = ftpClient.getReplyCode();
- // 验证是否登陆成功
- if (!FTPReply.isPositiveCompletion(reply))
- {
- ftpClient.disconnect();
- System.err.println("FTP server refused connection.");
- return result;
- }
- // 转移到FTP服务器目录至指定的目录下
- ftpClient.changeWorkingDirectory(newString(remotePath.getBytes(encoding), "iso-8859-1"));
- // 获取文件列表
- FTPFile[] fs = ftpClient.listFiles();
- for (FTPFile ff : fs)
- {
- if (ff.getName().equals(fileName))
- {
- File localFile = new File(localPath + "/" + ff.getName());
- OutputStream is = new FileOutputStream(localFile);
- ftpClient.retrieveFile(ff.getName(), is);
- is.close();
- }
- }
- ftpClient.logout();
- result = true;
- } catch (IOException e)
- {
- e.printStackTrace();
- } finally
- {
- if (ftpClient.isConnected())
- {
- try
- {
- ftpClient.disconnect();
- } catch (IOException ioe)
- {
- }
- }
- }
- return result;
- }
- public voidtestDownFile()
- {
- try
- {
- boolean flag = downFile("127.0.0.1", 21, "zlb", "123", "/","contact.txt", "D:/");
- System.out.println(flag);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- publicstatic void main(String[] args)
- {
- JFtpApche fa = new JFtpApche();
- //fa.testDownFile();
- fa.testUpLoadFromDisk();
- }
- }
来源: http://www.phpxs.com/code/1002661/