与windows explorer打开相同的文件相比,JCIFS默认情况下要慢太多了。我找了大半天,总算找出了解决方法,增加如下配置,则读取速度会有质的飞跃,我这边从7s提升至0.2s:
1 | System.setProperty( "jcifs.smb.client.dfs.disabled" , "true" ); |
至于原因嘛,我也不知道,我是从此网站捞到的一条配置:
http://samba.2283325.n4.nabble.com/Peformance-questions-td4644647.html
执行程序代码如下:
01 | private static final int BUFFER_SIZE = 1024 ; |
03 | public static void main(String[]args) throws Exception { |
07 |
System.setProperty( "jcifs.smb.client.dfs.disabled" , "true" ); |
09 |
long startTime = System.currentTimeMillis(); |
10 |
UniAddress dc = UniAddress.getByName( "192.168.100.5" ); |
11 |
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication( "fgina" , "xiajl" , "orange" ); |
12 |
SmbSession.logon(dc, authentication); |
13 |
SmbFile file = new SmbFile( "smb://192.168.100.5/edse/users.txt" , authentication); |
15 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
16 |
BufferedInputStream in = new BufferedInputStream( new SmbFileInputStream(file)); |
18 |
byte buffer[] = new byte [BUFFER_SIZE] ; |
20 |
while ((count = in.read(buffer, 0 ,BUFFER_SIZE)) != - 1 ) { |
21 |
out.write(buffer, 0 , count); |
23 |
String content = new String(out.toByteArray(), "GBK" ); |
25 |
System.out.println(System.currentTimeMillis() - startTime); |
27 |
System.out.println(content); |