Dibawah ini contoh code untuk meng-copy file
ejlp12.txt
lewat folder sharing windows dari komputer 3.110.110.57 ke direktori lokal C:
import java.io.File;
import java.io.FileOutputStream;
import jcifs.smb.SmbFileInputStream;
/**
* Copy file from remote host to local disk
*/
public class CopyFile {
public static void main(String[] argx) throws Exception {
// Set IP lokal, nama file dan tempat dimana file hasil copy disimpan
String localIP = "3.110.110.12";
String localFile = "C:\\ejlp12.txt";
// Set IP remote, user, password, nama file dan tempat file yang akan di-copy
String sambaUser = "Administrator";
String sambaPassword = "master";
String remoteComputer = "3.110.110.57";
String sharedDirectory = "/D$/";
String remoteFile = "ejlp12.txt";
jcifs.Config.setProperty( "wins", localIP );
SmbFileInputStream in = new SmbFileInputStream(
new StringBuffer("smb://").append(sambaUser).append(":")
.append(sambaPassword).append("@").append(remoteComputer)
.append(sharedDirectory).append(remoteFile).toString());
FileOutputStream out = new FileOutputStream(new File(localFile));
byte[] b = new byte[8192];
int n;
while(( n = in.read( b )) > 0 ) {
out.write(b,0,n);
}
out.flush();
out.close();
}
}
Sederhana dan gampang kan?
No comments:
Post a Comment