JAVA实现上传下载共享文件

1、上传下载共享文件需要用到jcifs,先下载相关JAR包(开源项目的源码,demo,文挡、API应有尽有)

https://jcifs.samba.org/src/

 

2、创建共享目录,确定访问用户及密码(用户需要写入权限)

String url_share_key = "192.16.20.15"; //共享IP
String url_share_user = "administrator"; //共享用户 需要有写入权限
String url_share_pwd = "123456"; //密码
String dir_share_key = "192.16.20.15/test/"; //共享根路径

 

3、用户凭证,用户凭证还可以通过远程路径传递(smb://用户名:密码@192.168.0.11/test )

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(url_share_key, url_share_user, url_share_pwd); 

 

4、上传

 1    /**
 2      * 从本地上传文件到共享目录
 3      * @param remoteUrl 远程路径
 4      * @param localFilePath 本地路径
 5      * @param auth 用户凭证
 6      */
 7     public static void smbPut(String remoteUrl, String localFilePath,NtlmPasswordAuthentication auth) {
 8         InputStream in = null;
 9         OutputStream out = null;
10         try {
11             // 需要上传的文件 取出文件名称
12             File localFile = new File(localFilePath);
13             String fileName = localFile.getName();
14 
15             // 共享目录 不存在则创建
16             SmbFile remoteFileDir = new SmbFile(remoteUrl, auth);
17             if (!remoteFileDir.isDirectory()) {
18                 remoteFileDir.mkdirs();
19             }
20 
21             // 上传
22             SmbFile remoteFile = new SmbFile(remoteUrl + File.separator
23                     + fileName, auth);
24             in = new BufferedInputStream(new FileInputStream(localFile));
25             out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
26 
27             // 缓冲数组
28             byte[] b = new byte[1024 * 5];
29             int len;
30             while ((len = in.read(b)) != -1) {
31                 out.write(b, 0, len);
32             }
33             // 刷新此缓冲的输出流
34             out.flush();
35         } catch (Exception e) {
36             e.printStackTrace();
37         } finally {
38             try {
39                 out.close();
40                 in.close();
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44         }
45     }

 

5、下载

 1   /**
 2      * 从共享目录拷贝文件到本地
 3      * @param remoteUrl 远程路径
 4      * @param localDir 本地路经
 5      * @param auth
 6      */
 7     public static void smbGet(String remoteUrl, String localDir,NtlmPasswordAuthentication auth) {
 8         InputStream in = null;
 9         OutputStream out = null;
10         try {
11             //远程文件
12             SmbFile remoteFile = new SmbFile(remoteUrl,auth);
13             if (remoteFile == null) {
14                 System.out.println("共享文件不存在");
15                 return;
16             }
17             
18             //创建本地文件并写入
19             String fileName = remoteFile.getName();
20             File localFile = new File(localDir + File.separator + fileName);
21             in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
22             out = new BufferedOutputStream(new FileOutputStream(localFile));
23             byte[] buffer = new byte[1024];
24             while (in.read(buffer) != -1) {
25                 out.write(buffer);
26                 buffer = new byte[1024];
27             }
28         } catch (Exception e) {
29             e.printStackTrace();
30         } finally {
31             try {
32                 out.close();
33                 in.close();
34             } catch (IOException e) {
35                 e.printStackTrace();
36             }
37         }
38     }

 

posted @ 2015-09-11 14:34  Nadim  阅读(1241)  评论(0编辑  收藏  举报