qingkuangs 发表于 2015-5-27 07:53:12

JAVA 访问FTP服务器示例(2)

  当然还缺少两个枚举,一个是下载状态枚举,一个是上传状态枚举。
  代码如下:
  



1 /**
2* 下载状态枚举
3*
4 * @author HQQW510_64
5*
6 */
7 public enum DownloadStatus {
8   Remote_File_Noexist, // 远程文件不存在
9   Download_New_Success, // 下载文件成功
10   Download_New_Failed, // 下载文件失败
11   Local_Bigger_Remote, // 本地文件大于远程文件
12   Download_From_Break_Success, // 断点续传成功
13   Download_From_Break_Failed; // 断点续传失败
14 }
15
16
17
18 /**
19* 上传状态枚举
20*
21 * @author HQQW510_64
22*
23 */
24 public enum UploadStatus {
25   Create_Directory_Fail, // 远程服务器相应目录创建失败
26   Create_Directory_Success, // 远程服务器闯将目录成功
27   Upload_New_File_Success, // 上传新文件成功
28   Upload_New_File_Failed, // 上传新文件失败
29   File_Exits, // 文件已经存在
30   Remote_Bigger_Local, // 远程文件大于本地文件
31   Upload_From_Break_Success, // 断点续传成功
32   Upload_From_Break_Failed, // 断点续传失败
33   Delete_Remote_Faild; // 删除远程文件失败
34 }

  OK,到此为止基本代码都完成了,来编写个代码来测试一下。
  



1 public class FTPDemo {
2
3   public static void main(String[] args) {
4         FtpHelper myFtp = new FtpHelper();
5         try {
6             myFtp.connect("192.168.1.253", 21, "jx", "jx");
7            
8 //            System.out.println("===================列举目录及文件=====================");
9 //            FTPFile[] ftpFiles = myFtp.GetDirAndFilesInfo(null);
10 //            for (FTPFile ftpFile : ftpFiles) {
11 //                System.out.println(ftpFile.getName());
12 //            }
13 //            
14 //            System.out.println("===================列举文件=====================");
15 //            String[] dirs = myFtp.GetFileNames(null);
16 //            for (String dir : dirs) {
17 //                System.out.println(dir);
18 //            }
19 //            
20 //            System.out.println("===================下载文件=====================");
21 //            DownloadStatus ds = myFtp.download("wamp2.rar", "d:\\abc.rar");
22 //            System.out.println(ds);
23            
24             System.out.println("===================上传文件=====================");            
25             System.out.println(myFtp.upload("d:\\abc.rar", "/abc.txt"));
26
27             myFtp.disconnect();
28         } catch (Exception e) {
29
30             System.out.println("连接FTP出错:" + e.getMessage());
31             e.printStackTrace();
32         }
33   }
34 }

  嘿嘿嘿,效果不错。
页: [1]
查看完整版本: JAVA 访问FTP服务器示例(2)