自封装JAVA检测文件在FTP是否存在

目前我所知,通过java中没有直接判断文件是否存在的方法,只能通过遍历目录即listFiles的方法来进行,但是这样读取目录特别耗内存,因此想看看有没有别的方法比如new File(filename).exists()之类的方法来减少内存占用。最终发现通过调用dos命令的方法是可以实现的,比遍历ftp目录速度快很多啊!

================dos命令begin========================

1
2
3
4
5
6
7
8
9
10
11
@ echo  off
set  ftpfilename=autoftp.cfg
echo  open  172.18.3.3 > "%ftpfilename%"
echo  mios >> "%ftpfilename%"
echo  m92102007 >> "%ftpfilename%"
echo  bin >> "%ftpfilename%"
echo  cd  bcbj\zyt\zds >> "%ftpfilename%"
echo  get jqyb06.txt c: /param/jqyb06 .txt >> "%ftpfilename%"
echo  bye >> "%ftpfilename%"
ftp  -s: "%ftpfilename%"
exit

================dos命令end========================

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
java调用程序贴一下:
package  com.wyebd.task.receive;
 
import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileWriter;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.util.Date;
 
import  com.wyebd.publicuse.ApplicationResource;
 
public  class  FtpDownload {
 
     /**
      * @description:以非遍历ftp目录的方式下载文件,通过dos命令的方式直接进行文件下载,加快了下载速度
      * @author RickyLee
      * @time:2013-06-05 10:00
      */
 
     // ftp地址
     private  String ftp1Ip =  null ;
 
     // ftp用户名
     private  String ftp2User =  null ;
 
     // ftp密码
     private  String ftp3Pass =  null ;
 
     // ftp上文件存放的路径
     private  String ftp4Dir =  null ;
 
     // ftp上的文件名
     private  String ftp5FileName =  null ;
 
     // 存放到本地的文件的全路径
     private  String localFullFilePath =  null ;
 
     public  String getFtp1Ip() {
         return  ftp1Ip;
     }
 
     public  void  setFtp1Ip(String ftp1Ip) {
         this .ftp1Ip = ftp1Ip;
     }
 
     public  String getFtp2User() {
         return  ftp2User;
     }
 
     public  void  setFtp2User(String ftp2User) {
         this .ftp2User = ftp2User;
     }
 
     public  String getFtp3Pass() {
         return  ftp3Pass;
     }
 
     public  void  setFtp3Pass(String ftp3Pass) {
         this .ftp3Pass = ftp3Pass;
     }
 
     public  String getFtp4Dir() {
         return  ftp4Dir;
     }
 
     public  void  setFtp4Dir(String ftp4Dir) {
         this .ftp4Dir = processDir(ftp4Dir);
     }
 
     public  String getFtp5FileName() {
         return  ftp5FileName;
     }
 
     public  void  setFtp5FileName(String ftp5FileName) {
         this .ftp5FileName = ftp5FileName;
     }
 
     public  String getLocalFullFilePath() {
         return  localFullFilePath;
     }
 
     public  void  setLocalFullFilePath(String localFullFilePath) {
         this .localFullFilePath = localFullFilePath;
     }
 
     // 开始执行下载任务,执行成功返回true,否则返回false
     public  boolean  run() {
         boolean  b =  false ;
         // 根据bean的信息生成bat文件,并返回bat文件的物理路径
         String batPath = creatBat();
         // 调用bat
         b = runBat(batPath);
         return  b;
     }
 
     // 执行bat文件
     private  boolean  runBat(String batPath) {
         boolean  b =  false ;
         String command =  "cmd /c start " +batPath; // exe,bat文件名OR DOS命令
         String text= null ;
         try  {
             Process proc = Runtime.getRuntime().exec(command);
             BufferedReader in =  new  BufferedReader( new  InputStreamReader(proc
                     .getInputStream()));
             while  ((text = in.readLine()) !=  null ) {
                 System.out.println(text);  // 输出测试
             }
         catch  (IOException ioError) {
             ioError.printStackTrace();
             System.exit( 0 );
         }
         if  ( new  File(localFullFilePath).exists())b =  true ;
         return  b;
     }
 
     // 根据用户提供的信息生成bat信息
     private  String creatBat() {
         String path = ApplicationResource.getValueByKey( "batPath" ) + "FtpDownload_" + new  Date().getTime()+ ".bat" ;
         File file =  new  File(path.substring( 0 , path.lastIndexOf( "/" )));
         if  (!file.exists())
             file.mkdirs();
         try  {
             FileWriter writer =  new  FileWriter(path,  false );
             writer.write( "@echo on\n" );
             writer.write( "set ftpfilename=autoftp.cfg\n" );
             writer.write( "echo open "  + ftp1Ip +  " >\"%ftpfilename%\"\n" );
             writer.write( "echo "  + ftp2User +  " >>\"%ftpfilename%\"\n" );
             writer.write( "echo "  + ftp3Pass +  " >>\"%ftpfilename%\"\n" );
             writer.write( "echo bin >>\"%ftpfilename%\"\n" );
             writer.write( "echo cd "  + ftp4Dir +  " >>\"%ftpfilename%\"\n" );
             writer.write( "echo get "  + ftp5FileName +  " "  + localFullFilePath
                     " >>\"%ftpfilename%\"\n" );
             writer.write( "echo bye >>\"%ftpfilename%\"\n" );
             writer.write( "ftp -s:\"%ftpfilename%\"\n" );
             writer.write( "exit" );
             writer.close();
         catch  (IOException e) {
             e.printStackTrace();
         }
         return  path;
     }
 
     // 对ftp的Dir进行格式化处理
     private  String processDir(String fdir) {
         fdir = fdir.replace( "/" "\\" ).replace( "\\\\" "\\" );
         if  (fdir.startsWith( "\\" ))
             fdir = fdir.substring(fdir.indexOf( "\\" ) +  1 );
         if  (fdir.endsWith( "\\" ))
             fdir = fdir.substring( 0 , fdir.lastIndexOf( "\\" ));
         return  fdir;
     }
 
     // 主函数,用于测试
     public  static  void  main(String[] args) {
         FtpDownload fd =  new  FtpDownload();
         fd.setFtp1Ip( "172.18.3.3" );
         fd.setFtp2User( "mios" );
         fd.setFtp3Pass( "m92102007" );
         fd.setFtp4Dir( "bcbj\\zyt\\zds" );
         fd.setFtp5FileName( "jqyb06.txt" );
         fd.setLocalFullFilePath( "c:/param/jqyb06.txt" );
         System.out.println(fd.run());
     }
}

 

 

欢迎各位提出更宝贵的意见。

有同学提出需要看ApplicationResource类,我在这里贴一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.wyebd.hsms.publicUse;
 
import java.util.Enumeration;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.TreeMap;
 
import com.wyebd.hsms.utils.StringUtil;
 
/**
  * 该类用于管理全局应用属性配置信息,以用于显示、提示、配置等信息的集中管理及国际化。
  * <p>Title: 全局应用属性配置信息管理类</p>
  * <p>Description: </p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: </p>
  * @author RickyLee
  * @version 1.0
  */
public class ApplicationResource
{
     //存放全局应用属性配置信息的名、值对
     private static TreeMap tmKeyValue = null ;
     //资源配置文件的文件名
     private static final String APP_RESOURCE_FILENAME = "ApplicationResources" ;
 
 
     /**
      * 读取Web应用下的WEB-INF\classes目录下的对应的资源文件中的应用属性配置信息。
      * @param filenameNoExt String 不带扩展名.properties的文件名
      * @return String
      */
     private static String getApplicationResources(String filenameNoExt)
     {
         String name = null ;
         String value = null ;
         tmKeyValue = new TreeMap();
 
         PropertyResourceBundle configBundle = (PropertyResourceBundle)
                                               ResourceBundle.getBundle(filenameNoExt);
 
         Enumeration keys = configBundle.getKeys();
         while (keys.hasMoreElements())
         {
             name = keys.nextElement().toString();
             value = StringUtil.getStringISO8859_1(configBundle.getString(name));
             //value = configBundle.getString(name);
             tmKeyValue.put(name,value);
         }
 
         //查看系统资源配置情况
         //System.out.println(tmKeyValue);
 
         return value;
     }
 
     /**
      * 从资源配置文件中根据键名取出其对应的值。
      * @param key String 要取的资源标识名称
      * @return String 根据资源标识名称从资源文件取得的值
      */
     public static String getValueByKey(String key)
     {
         if (tmKeyValue == null )
             getApplicationResources(APP_RESOURCE_FILENAME);
 
         Object objTmp = tmKeyValue.get(key);
 
         if (objTmp == null )
             return "" ;
         else
             return objTmp.toString();
     }
 
     public static void main(String[] args)
     {
         String strNew = getValueByKey( "driver" );
         System.out.println(strNew);
         strNew = getValueByKey( "url" );
         System.out.println(strNew);
         //System.out.println();
         //System.out.println(tmKeyValue);
     }
}

 

资源文件ApplicationResources.properties中的内容格式为:

1
2
3
4
5
DataSourceName= java\:comp /env/jdbc/generateDataSource
driver = net.sourceforge.jtds.jdbc.Driver
url = jdbc\:jtds\:sqlserver\: //localhost \:1433 /dbName
username = sa
password = root

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值