Repository navigation
Expand file tree
/
Copy pathSamba.java
More file actions
86 lines (78 loc) · 3.04 KB
/
Copy pathSamba.java
File metadata and controls
86 lines (78 loc) · 3.04 KB
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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class Samba {
/**
* 从局域网中共享文件中得到文件并保存在本地磁盘上
* @param remoteUrl 共享电脑路径 如:smb//administrator:[email protected]/smb/1221.zip , smb为共享文件
* 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。
* @param localDir 本地路径 如:D:/
*/
public static void smbGet(String remoteUrl,String localDir){
InputStream in = null;
OutputStream out = null;
try {
SmbFile smbFile = new SmbFile(remoteUrl);
String fileName = smbFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(smbFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte []buffer = new byte[1024];
while((in.read(buffer)) != -1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 把本地磁盘中的文件上传到局域网共享文件下
* @param remoteUrl 共享电脑路径 如:smb//administrator:[email protected]/smb
* @param localFilePath 本地路径 如:D:/
*/
public static void smbPut(String remoteUrl,String localFilePath){
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte []buffer = new byte[1024];
while((in.read(buffer)) != -1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//smbPut("smb://zhujohnle:[email protected]/beyond/", "");
smbGet("smb://zhujohnle:[email protected]/beyond/BC3Key.txt", "D:/");
}
}