FTP 是 File Transfer Protocol(文件传输协议)的英文简称, 而中文简称为文传协议接下来通过本文给大家实例讲解 Java 实现 FTP 文件的上传和下载功能, 需要的的朋友一起看看吧
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");
- }
- }
来源: http://www.phperz.com/article/18/0223/359345.html