import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class SMBTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
SmbFile file = new SmbFile(
"smb://administrator:password@ip/hejian/hello.txt");
SmbFileInputStream sfis = new SmbFileInputStream(file);
byte b[] = new byte[2];
while (-1 != sfis.read(b)) {
System.out.print(new String(b));
}
sfis.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}
假如公司有域认证的话,上面的方法就行不通了,不过还有另外一方法:
package com.rits.samplescan.smb;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class SMBTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String domain = "domain";
String username = "username";
String password = "password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain,username,password);
try {
SmbFile file = new SmbFile(
"smb://10.10.10.18/share/hello.txt" ,auth);
SmbFileInputStream sfis = new SmbFileInputStream(file);
byte b[] = new byte[2];
while (-1 != sfis.read(b)) {
System.out.print(new String(b));
}
sfis.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}