FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在FTP的使用当中,用户经常遇到两个概念:"下载"(Download)和"上传"(Upload)。"下载"文件就是从远程主机拷贝文件至自己的计算机上;"上传"文件就是将文件从自己的计算机中拷贝至远程主机上。用Internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。
首先下载了Serv-U将自己的电脑设置为了FTP文件服务器,方便操作。
1.FTP文件的下载(从FTP服务器下载到本机)
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 FtpApche {private static FTPClient ftpClient = new FTPClient();private static String encoding = System.getProperty("file.encoding");/*** Description: 从FTP服务器下载文件* * @Version1.0* @param url* FTP服务器hostname* @param port* FTP服务器端口* @param username* FTP登录账号* @param password* FTP登录密码* @param remotePath* FTP服务器上的相对路径* @param fileName* 要下载的文件名* @param localPath* 下载后保存到本地的路径* @return*/public static boolean downFile(String url, int port, String username,String password, String remotePath, String fileName,String localPath) {boolean result = false;try {int reply;ftpClient.setControlEncoding(encoding);/** 为了上传和下载中文文件,有些地方建议使用以下两句代替* new String(remotePath.getBytes(encoding),"iso-8859-1")转码。* 经过测试,通不过。*/// 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(new String(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;}/*** 将FTP服务器上文件下载到本地* */public void testDownFile() {try {boolean flag = downFile("10.0.0.102", 21, "admin","123456", "/", "ip.txt", "E:/");System.out.println(flag);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {FtpApche fa = new FtpApche();fa.testDownFile();}}
2.FTP文件的上传(从本机上传到FTP服务器)
import java.io.File;import java.io.FileInputStream;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;public class FTPTest_05 {private FTPClient ftp;/** * * @param path 上传到ftp服务器哪个路径下 * @param addr 地址 * @param port 端口号 * @param username 用户名 * @param password 密码 * @return * @throws Exception */ private boolean connect(String path,String addr,int port,String username,String password) throws Exception {boolean result = false;ftp = new FTPClient();int reply;ftp.connect(addr,port);ftp.login(username,password);ftp.setFileType(FTPClient.BINARY_FILE_TYPE);reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();return result;}ftp.changeWorkingDirectory(path);result = true;return result;}/** * * @param file 上传的文件或文件夹 * @throws Exception */ private void upload(File file) throws Exception{if(file.isDirectory()){ftp.makeDirectory(file.getName());ftp.changeWorkingDirectory(file.getName());String[] files = file.list();for (int i = 0;i < files.length;i++) {File file1 = new File(file.getPath()+"//"+files[i] );if(file1.isDirectory()){upload(file1);ftp.changeToParentDirectory();}else{File file2 = new File(file.getPath()+"//"+files[i]);FileInputStream input = new FileInputStream(file2);ftp.storeFile(file2.getName(), input);input.close();}}}else{File file2 = new File(file.getPath());FileInputStream input = new FileInputStream(file2);ftp.storeFile(file2.getName(), input);input.close();}}public static void main(String[] args) throws Exception{FTPTest_05 t = new FTPTest_05();boolean connFlag = t.connect("/", "10.0.0.105", 21, "ls", "123456");System.out.println("connFlag : " + connFlag);File file = new File("D://test02");//本机被传文件的地址System.out.println("file : " + file);t.upload(file);System.out.println("upload : " + "ok");}}
以上所述是小编给大家介绍的Java实现FTP文件的上传和下载功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
新闻热点
疑难解答