ftp上传和下载文件的java实现

本文介绍了一种使用Java和Apache Commons Net库实现的匿名FTP登录方法,并提供了从FTP服务器下载文件和上传文件的具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一个项目需要去ftp上下载文件,遇到一个问题是没有用户名和密码,后来才发现ftp原来可以匿名登录,而且还要声明一下自己是匿名的,空字符串都登陆不上去的,一下是我的代码,是匿名登录的。
001 package ftp2;
002  
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.FileOutputStream;
006 import java.io.IOException;
007 import java.io.OutputStream;
008 import java.net.SocketException;
009  
010 import org.apache.commons.io.FileUtils;
011 import org.apache.commons.io.IOUtils;
012 import org.apache.commons.net.ftp.FTPClient;
013 import org.apache.commons.net.ftp.FTPClientConfig;
014 import org.apache.commons.net.ftp.FTPFile;
015 import org.apache.commons.net.ftp.FTPReply;
016  
017 /**
018  * 使用commons的net包进行ftp链接. 相关包:commons-net-1.4.1.jar ;
019  * commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件
020  * 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名
021  *
022  */
023 public class ListFtpFile {
024  
025     private FTPClient ftpClient = new FTPClient();
026  
027     private OutputStream outSteam = null;
028  
029     /**
030      * ftp服务器地址
031      */
032     private String hostName = "192.168.0.2";
033     private int port = 212;
034  
035     /**
036      * 登录名
037      */
038     private String userName = "anonymous";//匿名登录,空字符串不行
039  
040     /**
041      * 登录密码
042      */
043     private String password = "121@hotmail.com";//随便一个地址,我胡乱写一个也可以运行的
044  
045     /**
046      * 需要访问的远程目录
047      */
048     private String remoteDir = "/software/dreamweaver/";
049  
050     /**
051      * 下载
052      */
053     private void download() {
054         try {
055             // 链接到ftp服务器
056             ftpClient.connect(hostName,port);
057             System.out.println("连接到ftp服务器:" + hostName + " 成功..开始登录");
058             // 登录.用户名 密码
059             boolean b = ftpClient.login(userName, password);
060             System.out.println("登录成功." + b);
061              
062 //          检测连接是否成功
063             int reply = ftpClient.getReplyCode();
064             if(!FTPReply.isPositiveCompletion(reply)) {
065                 ftpClient.disconnect();
066                 System.err.println("FTP server refused connection.");
067                 System.exit(1);
068             }
069              
070             ftpClient.setControlEncoding("GBK");
071             FTPClientConfig conf = newFTPClientConfig(FTPClientConfig.SYST_NT); 
072             conf.setServerLanguageCode("zh");
073             FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
074             if (remoteFiles != null) {
075                 for (int i = 0; i < remoteFiles.length; i++) {
076                     String name = remoteFiles[i].getName();
077                      
078                     //下载
079                     File localFile = new File("c:/001/ftp/" + name);
080                     OutputStream is = new FileOutputStream(localFile);
081                     //retrieveFile的第一个参数需要是 ISO-8859-1 编码,并且必须是完整路径!
082                     String fileName = remoteDir + name;
083                     ftpClient.retrieveFile(newString(fileName.getBytes("GBK"),"ISO-8859-1"), is);
084                     is.close();
085                      
086                     //打印
087                     long length = remoteFiles[i].getSize();
088                     String readableLength = FileUtils.byteCountToDisplaySize(length);
089                     System.out.println(name + ":\t"+remoteFiles[i].isFile()+"\t"+ readableLength);
090                      
091                 }
092             }
093              
094  
095             ftpClient.logout();
096         catch (Exception e) {
097             e.printStackTrace();
098         finally {
099             IOUtils.closeQuietly(outSteam);
100             try {
101                 ftpClient.disconnect();
102             catch (IOException ioe) {
103                 ioe.printStackTrace();
104             }
105         }
106     }
107      
108     /**
109      * 上传
110      * */
111     public void upload(){
112         String srcUrl = "C:/001/菜单权限设计.doc";
113         String targetFileName = "菜单权限设计.doc";
114         try {
115             ftpClient.connect(hostName,port);
116             boolean b = ftpClient.login(userName, password);
117             // 检测连接是否成功
118             int reply = ftpClient.getReplyCode();
119             if (!FTPReply.isPositiveCompletion(reply)) {
120                 ftpClient.disconnect();
121                 System.err.println("FTP server refused connection.");
122                 System.exit(1);
123             }
124              
125             ftpClient.setControlEncoding("GBK");
126             FTPClientConfig conf = newFTPClientConfig(FTPClientConfig.SYST_NT); 
127             conf.setServerLanguageCode("zh");
128              
129             File srcFile = new File(srcUrl);
130             FileInputStream fis = null;
131             fis = new FileInputStream(srcFile);
132  
133             // 设置上传目录
134             ftpClient.changeWorkingDirectory(remoteDir);
135             ftpClient.setBufferSize(1024);
136             ftpClient.setControlEncoding("GBK");
137  
138             // 设置文件类型(二进制)
139             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
140             // 上传
141             b = ftpClient.storeFile(targetFileName, fis);
142             IOUtils.closeQuietly(fis);
143              
144             /*boolean bool = ftpClient.changeWorkingDirectory("/NC");
145             System.out.println("changeWorkingDirectory="+bool);
146             bool = ftpClient.makeDirectory("/NC");
147             System.out.println("makeDirectory="+bool);*/
148              
149             ftpClient.logout();
150         } catch (SocketException e) {
151             e.printStackTrace();
152         } catch (IOException e) {
153             e.printStackTrace();
154         }finally{
155             try {
156                 ftpClient.disconnect();
157             } catch (IOException e) {
158                 e.printStackTrace();
159             }
160         }
161          
162     }
163  
164     /**
165      * 测试
166      * */
167     public static void main(String[] args) {
168         ListFtpFile listFtpfiles = new ListFtpFile();
169         listFtpfiles.download();
170         listFtpfiles.upload();
171     }
172 }
需要common-io 和common-net两个jar包的,同学们自己下载吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值