Java 远程文件对接

以下代码即贴即用,第一次发博客,不足之处请大家多多指教!

1、配置文件:copyRemoteFile.properties

?
#  src/dao.properties
#      这里保存的都是键值对信息
interface  name(no packgage) = implementation class
# 注意:
#  A:【路径符号】【必须】是【/】【如:D:/home/publish】
#  B:【键key=值value】对【后面】【绝不允许有空格】【如:REMOTE_HOST_IP= 172.77 . 9.77
# REMOTE_HOST_IP  远程机器IP
# LOGIN_ACCOUNT   远程机器登录名
# LOGIN_PASSWORD  远程机器登录密码
# SHARE_DOC_NAME  远程机器共享文件夹名(设置共享后必须授予读写权限)
# sourcePath      本地路径
# targetPath      目标路径(真实路径=共享文件夹路径+目标路径)
REMOTE_HOST_IP= 172.77 . 9.77
LOGIN_ACCOUNT= 77
LOGIN_PASSWORD= 77
SHARE_DOC_NAME=vfs_home
sourcePath=D:/home/publish
targetPath=publish

  

  

2、导入jar包:jcifs-1.3.16.jar

 

3、读取配置文件中key对应的value类:RemoteConfigUtil.java

 

?
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
package  com.remote;
 
import  java.io.IOException;
import  java.util.Properties;
 
/**
  * 读取配置文件中key对应的value
  * @author JunjieQin
  */
public  class  RemoteConfigUtil {
     private  String REMOTE_HOST_IP;
     private  String LOGIN_ACCOUNT;
     private  String LOGIN_PASSWORD;
     private  String SHARE_DOC_NAME;
     private  String sourcePath;
     private  String targetPath;
 
     //无参构造方法
     public  RemoteConfigUtil() {
         try  {
             // 读取配置文件
             Properties prop = new  Properties();
             prop.load( this .getClass().getClassLoader().getResourceAsStream( "copyRemoteFile.properties" ));
             // 根据 key 获取 value
             REMOTE_HOST_IP = prop.getProperty( "REMOTE_HOST_IP" );
             LOGIN_ACCOUNT = prop.getProperty( "LOGIN_ACCOUNT" );
             LOGIN_PASSWORD = prop.getProperty( "LOGIN_PASSWORD" );
             SHARE_DOC_NAME = prop.getProperty( "SHARE_DOC_NAME" );
             sourcePath = prop.getProperty( "sourcePath" );
             targetPath = prop.getProperty( "targetPath" );
         } catch  (IOException e) {
             e.printStackTrace();
         }
     }
     public  String getLOGIN_ACCOUNT() {
         return  LOGIN_ACCOUNT;
     }
 
     public  void  setLOGIN_ACCOUNT(String login_account) {
         LOGIN_ACCOUNT = login_account;
     }
 
     public  String getLOGIN_PASSWORD() {
         return  LOGIN_PASSWORD;
     }
 
     public  void  setLOGIN_PASSWORD(String login_password) {
         LOGIN_PASSWORD = login_password;
     }
 
     public  String getREMOTE_HOST_IP() {
         return  REMOTE_HOST_IP;
     }
 
     public  void  setREMOTE_HOST_IP(String remote_host_ip) {
         REMOTE_HOST_IP = remote_host_ip;
     }
 
     public  String getSHARE_DOC_NAME() {
         return  SHARE_DOC_NAME;
     }
 
     public  void  setSHARE_DOC_NAME(String share_doc_name) {
         SHARE_DOC_NAME = share_doc_name;
     }
 
     public  String getSourcePath() {
         return  sourcePath;
     }
 
     public  void  setSourcePath(String sourcePath) {
         this .sourcePath = sourcePath;
     }
 
     public  String getTargetPath() {
         return  targetPath;
     }
 
     public  void  setTargetPath(String targetPath) {
         this .targetPath = targetPath;
     }
}

  

4、操作远程共享文件夹类: RemoteFileUtil.java

     根据需求选择相应的 Method

 

?
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package  com.remote;
import  java.io.BufferedOutputStream;
import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.net.MalformedURLException;
import  java.net.UnknownHostException;
import  java.util.ArrayList;
import  java.util.List;
 
import  jcifs.smb.SmbException;
import  jcifs.smb.SmbFile;
import  jcifs.smb.SmbFileInputStream;
import  jcifs.smb.SmbFileOutputStream;
 
/***********************************************
  *  File Name: RemoteFileUtil.java            
  *  Created by: JunjieQin     
  *  Checked in by:   
  *  Date: 2011-9-6          
  *  Revision: 1.7       
  *  Description:操作远程共享文件夹类
  *  Amendment History
  *  Modified Date:2011-9-16        
  *  Modified By:JunjieQin  
  *  Change Description:From local copy files to remote directory
  *
  ***********************************************/
public  class  RemoteFileUtil {  
     
     private  ArrayList filelist = new  ArrayList();
     RemoteConfigUtil rc = new  RemoteConfigUtil();
 
     private  String remoteHostIp;  //远程主机IP  
     private  String account;       //登陆账户  
     private  String password;      //登陆密码  
     private  String shareDocName;  //共享文件夹名称  
        
     /** 
      * 默认构造函数 
      */ 
     public  RemoteFileUtil(){ 
         this .remoteHostIp = rc.getREMOTE_HOST_IP();  
         this .account = rc.getLOGIN_ACCOUNT();  
         this .password = rc.getLOGIN_PASSWORD();  
         this .shareDocName = rc.getSHARE_DOC_NAME();  
     }  
        
     /** 
      * 构造函数 
      * @param remoteHostIp  远程主机Ip 
      * @param account       登陆账户 
      * @param password      登陆密码 
      * @param sharePath     共享文件夹路径 
      */ 
     public  RemoteFileUtil(String remoteHostIp, String account, String password,String shareDocName) {  
         this .remoteHostIp = remoteHostIp;  
         this .account = account;  
         this .password = password;  
         this .shareDocName = shareDocName;  
     }     
        
     /** 
      * 对远程共享文件进行读取所有行 
      * @param remoteFileName  文件名  说明:参数为共享目录下的相对路径 
      *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
      *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
      * @return  文件的所有行 
      */ 
     public  List<String> readFile(String remoteFileName){  
         SmbFile smbFile = null ;  
         BufferedReader reader = null ;  
         List<String> resultLines = null ;  
         //构建连接字符串,并取得文件连接  
         String conStr = null ;  
         conStr = "smb://" +account+ ":" +password+ "@" +remoteHostIp+ "/" +shareDocName+ "/" +remoteFileName;  
         try  {  
             smbFile = new  SmbFile(conStr);  
         } catch  (MalformedURLException e) {  
             e.printStackTrace();  
         }  
         //创建reader  
         try  {  
             reader = new  BufferedReader( new  InputStreamReader( new  SmbFileInputStream(smbFile)));  
         } catch  (SmbException e) {  
             e.printStackTrace();  
         } catch  (MalformedURLException e) {  
             e.printStackTrace();  
         } catch  (UnknownHostException e) {  
             e.printStackTrace();  
         }         
         //循环对文件进行读取  
         String line;  
         try  {  
             line = reader.readLine();  
             if (line != null  && line.length()> 0 ){  
                 resultLines = new  ArrayList<String>();  
             }  
             while  (line != null ) {  
                 resultLines.add(line);  
                 line = reader.readLine();  
             }  
         } catch  (IOException e) {  
             e.printStackTrace();  
         }  
         //返回  
         return  resultLines;  
     }  
        
     /** 
      * 对远程共享文件进行写入 
      * @param is                本地文件的输入流 
      * @param remoteFileName    远程文件名    说明:参数为共享目录下的相对路径 
      *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
      *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
      * @return   
      */ 
     public  boolean  writeFile(InputStream is,String remoteFileName){  
         SmbFile smbFile = null ;  
         OutputStream os = null ;  
         byte [] buffer = new  byte [ 1024 * 8 ];  
         //构建连接字符串,并取得文件连接  
         String conStr = null ;  
         conStr = "smb://" +account+ ":" +password+ "@" +remoteHostIp+ "/" +shareDocName+ "/" +remoteFileName;  
         try  {  
             smbFile = new  SmbFile(conStr);  
         } catch  (MalformedURLException e) {  
             e.printStackTrace();  
             return  false ;  
         }  
            
         //获取远程文件输出流并写文件到远程共享文件夹  
         try  {  
             os = new  BufferedOutputStream( new  SmbFileOutputStream(smbFile));  
             while ((is.read(buffer))!=- 1 ){  
                 os.write(buffer);         
             }  
         } catch  (Exception e) {  
             e.printStackTrace();  
             return  false ;  
         }   
            
         return  true ;  
     }  
        
        
     /** 
      * 对远程共享文件进行写入重载 
      * @param localFileName   要写入的本地文件全名 
      * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径 
      *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
      *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
      * @return 
      */ 
     public  boolean  writeFile(String localFileFullName ,String remoteFileName){  
         try  {  
             return  writeFile( new  FileInputStream( new  File(localFileFullName)),remoteFileName);  
         } catch  (FileNotFoundException e) {  
             e.printStackTrace();  
             return  false ;  
         }  
     }  
        
     /** 
      * 对远程共享文件进行写入重载 
      * @param localFileName   要写入的本地文件 
      * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径 
      *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
      *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
      * @return 
      */ 
     public  boolean  writeFile(File localFile ,String remoteFileName){  
         try  {  
             return  writeFile( new  FileInputStream(localFile),remoteFileName);  
         } catch  (FileNotFoundException e) {  
             e.printStackTrace();  
             return  false ;  
         }  
     }  
 
        
     /** 
      * 对远程共享文件所有文件 
      * @return  所有文件
      */ 
     public  List<String> getFiles(){  
         SmbFile smbFile = null ;  
         BufferedReader reader = null ;  
         List<String> resultLines = new  ArrayList();  
         //构建连接字符串,并取得文件连接  
         String conStr = null ;  
         conStr = "smb://" +account+ ":" +password+ "@" +remoteHostIp+ "/" +shareDocName+ "/" ;  
         try  {  
             smbFile = new  SmbFile(conStr);  
         } catch  (MalformedURLException e) {  
             e.printStackTrace();  
         }  
         //创建reader  
         try  {  
           String[] a = smbFile.list();
           for ( int  i= 0 ;i<a.length;i++){
             resultLines.add(a[i]);
             System.out.println(a[i]);
           }
         } catch  (SmbException e) {  
             e.printStackTrace();  
         } catch  (Exception e) {  
             e.printStackTrace();  
         }         
         //返回  
         return  resultLines;  
     }  
     
     /** 在本地为共享计算机创建文件夹
      * @param remoteUrl 远程计算机路径
      */ 
     public  void  smbMkDir(String name) {
         // 注意使用jcifs-1.3.15.jar的时候 操作远程计算机的时候所有类前面须要增加Smb
         // 创建一个远程文件对象
         String conStr = "smb://"  + account + ":"  + password + "@"  + remoteHostIp + "/"  + shareDocName;
         SmbFile remoteFile;
         try  {
             remoteFile = new  SmbFile(conStr + "/"  + name);
             if  (!remoteFile.exists()) {
                 remoteFile.mkdir(); // 创建远程文件夹
             }
         } catch  (MalformedURLException e) {
             e.printStackTrace();
         } catch  (SmbException e) {
             e.printStackTrace();
         }
     }
     
     /**
      * 删除文件夹
      * @param folderPath 共享文件夹下一个文件夹名
      * @return
      */
     public  void  delFolder(String folderPath) {
         //String conStr = "smb://"+LOGIN_ACCOUNT+":"+LOGIN_PASSWORD+"@"+remoteHostIp+"/"+shareDocName;
         try  {
             delAllFile(folderPath); //删除完里面所有内容
             String filePath = folderPath;
             filePath = filePath.toString();
             
             SmbFile myFilePath = new  SmbFile(filePath);
             myFilePath.delete(); //删除空文件夹
         }
         catch  (Exception e) {
             String message = ( "删除文件夹操作出错" );
             System.out.println(message);
         }
     }
     
     
     /**
      * 删除共享文件夹下一个文件夹名
      * @param path 共享文件夹下一个文件夹名
      * @return
      * @return
      */
     public  boolean  delAllFile(String path) {
         boolean  bea = false ;
         try  {
             SmbFile file = new  SmbFile(path);
             if  (!file.exists()) {
                 return  bea;
             }
             if  (!file.isDirectory()) {
                 return  bea;
             }
             String[] tempList = file.list();
             SmbFile temp = null ;
             for  ( int  i = 0 ; i < tempList.length; i++) {
                 if  (path.endsWith( "/" )) {
                     temp = new  SmbFile(path + tempList[i]);
                 } else  {
                     temp = new  SmbFile(path + "/"  + tempList[i]);
                 }
                 if  (temp.isFile()) {
                     temp.delete();
                 }
                 if  (temp.isDirectory()) {
                     delAllFile(path + "/"  + tempList[i] + "/" ); // 先删除文件夹里面的文件
                     delFolder(path + "/"  + tempList[i] + "/" ); // 再删除空文件夹
                     bea = true ;
                 }
             }
             return  bea;
         } catch  (Exception e) {
             return  bea;
         }
     }
 
     
     
     /**
      * 复制整个文件夹的内容
      * @param oldPath 准备拷贝的目录
      * @param newPath 指定绝对路径的新目录
      * @return
      */
     public  void  copyFolder(String oldPath, String newPath) {
         String conStr = "smb://"  + account + ":"  + password + "@"  + remoteHostIp + "/"  + shareDocName;
         System.err.println(conStr);
         try  {
             /**
              * 如果存在文件夹删除文件
              * SmbFile exittemp = new SmbFile(conStr + "/"+newPath);
              * if(exittemp.exists()){
              *      delFolder(conStr+"/"+newPath+"/");
              * }
              */
             SmbFile exittemps = new  SmbFile(conStr + "/"  + newPath);
             if  (!exittemps.exists()) {
                 exittemps.mkdirs(); // 如果文件夹不存在 则建立新文件夹
             }
             File a = new  File(oldPath);
             String[] file = a.list();
             File temp = null ;
             for  ( int  i = 0 ; i < file.length; i++) {
                 if  (oldPath.endsWith( "/" )) {
                     temp = new  File(oldPath + file[i]);
                 } else  {
                     temp = new  File(oldPath + "/"  + file[i]);
                 }
                 if  (temp.isFile()) {
                     if  (temp.exists()) {
                         writeFile(temp, newPath + "/"  + file[i]);
                     }
                 }
                 if  (temp.isDirectory()) { // 如果是子文件夹
                     copyFolder(oldPath + "/"  + file[i], newPath + "/"  + file[i]);
                 }
             }
 
         } catch  (Exception e) {
             String message = "复制整个文件夹内容操作出错" ;
             System.out.println(message);
         }
     }
     
     /**
      * 复制文件到远程计算机,如果目标路径不存在则创建,反之不创建
      * @param localFileFullName 本地指定文件路径
      * @param targetDir 目标路径
      */
     public  void  copyFileToRemoteDir(String localFileFullName, String targetDir) {
         System.err.println(localFileFullName + "--"  + targetDir);
         RemoteFileUtil rf = new  RemoteFileUtil();
         InputStream is = null ;
         SmbFile smbFile = null ;
         OutputStream os = null ;
         byte [] buffer = new  byte [ 1024  * 8 ];
         // 构建连接字符串,并取得文件连接
         String conStr = null ;
         conStr = "smb://"  + account + ":"  + password + "@"  + remoteHostIp + "/"  + shareDocName + "/"  + targetDir;
         System.err.println(conStr);
         SmbFile sf;
         try  {
             sf = new  SmbFile( "smb://"  + account + ":"  + password + "@"  + remoteHostIp + "/"  + shareDocName + "/"  + targetDir);
             if  (!sf.exists()) {
                 // 新建目标目录
                 sf.mkdirs();
                 is = new  FileInputStream( new  File(localFileFullName));
                 // 获取远程文件输出流并写文件到远程共享文件夹
                 os = new  BufferedOutputStream( new  SmbFileOutputStream(smbFile));
                 while  ((is.read(buffer)) != - 1 ) {
                     os.write(buffer);
                 }
             }
         } catch  (Exception e) {
             System.err.println( "提示:复制整个文件夹内容操作出错。" );
         }
 
         File file = new  File(localFileFullName);
         if  (file.isFile()) {
             File sourceFile = file;     // 源文件
             File targetFile = new  File( new  File(targetDir).getAbsolutePath() + File.separator + file.getName()); // 目标文件
             String name = file.getName(); // 文件名
             if  (targetDir != null  && targetFile != null ) {
                 rf.writeFile(sourceFile, "/"  + targetDir + "/"  + name); // 复制文件
             } else  if  (targetFile != null ) {
                 rf.writeFile(sourceFile, name); // 复制文件
             }
         }
     }
     
     /**
      * 循环获取文件夹内所有匹配的文件
      * @param strPath 路径
      * @param subStr 匹配字符
      * @return
      */
     public  ArrayList refreshFileList(String strPath, String subStr) {
         File dir = new  File(strPath);
         File[] files = dir.listFiles();
         if  (files == null )
             return  null ;
         for  ( int  i = 0 ; i < files.length; i++) {
             if  (!files[i].isDirectory()) {
                 String strFileName = files[i].getAbsolutePath().toLowerCase();
                 if  (files[i].getName().indexOf(subStr) >= 0 ) {
                     filelist.add(files[i].getName());
                 }
             }
         }
         return  filelist;
     }
 
     // 测试从本地复制文件到远程目标目录,测试已通过
     public  static  void  main(String[] args) {
         RemoteConfigUtil rc = new  RemoteConfigUtil();
         RemoteFileUtil util = new  RemoteFileUtil();
         util.copyFileToRemoteDir(rc.getSourcePath(), rc.getTargetPath());
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java对接大华海康视频监控是指通过Java编程语言实现与大华和海康视频监控设备的交互和通讯。Java是一种跨平台的高级编程语言,其强大的网络编程能力和丰富的第三方库使得与视频监控设备的对接变得更加简单和灵活。 对接大华海康视频监控可以利用Java提供的网络编程功能,使用Socket或HTTP协议与视频监控设备进行通信。首先,需要通过设备的IP地址和端口号建立与设备的连接。然后,通过发送指令和接收设备的响应来实现对视频监控设备的控制和操作。 在Java中,可以使用第三方库来简化与大华海康视频监控设备的对接过程。例如,对于大华视频监控设备,可以使用Java SDK提供的相关接口,通过调用SDK中的方法实现设备的登录、预览、录像回放和控制等功能。 对于海康视频监控设备,可以使用Java SDK提供的海康芯片平台开发包(SDK)来实现对接。通过该SDK,可以获取设备的基本信息、实时视频流、录像文件等,并进行远程控制和操作。 在对接大华海康视频监控时,还可以利用Java提供的图形用户界面(GUI)开发工具包,如JavaFX或Swing,将视频监控画面显示在程序界面上,以便用户实时查看监控画面,同时结合图像处理和分析算法,实现实时监控、报警和数据分析等功能。 总之,Java对接大华海康视频监控是一种灵活、高效的方式,能够通过Java编程语言实现与视频监控设备的交互和通讯,满足不同应用场景下的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值