String url = "smb://yourhost/yourpath/"; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "user", "password"); SmbFile dir = new SmbFile(url, auth); for (SmbFile f : dir.listFiles()) { System.out.println(f.getName()); }
String user = "your_user_name"; String pass ="your_pass_word"; String sharedFolder="shared"; String path="smb://ip_address/"+sharedFolder+"/test.txt"; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass); SmbFile smbFile = new SmbFile(path,auth); SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile); smbfos.write("testing....and writing to a file".getBytes()); System.out.println("completed ...nice !");
@Override public String[] call() { try { return file.list(); } catch (final SmbException e) { //Log.logWarning("TimeoutRequest:list", file.toString() + " - no list", e); return null; } } }).call(timeout);
/** * Get directory listing of file or smb url * respects the hidden attribute of a directory (return null if hidden) * * @return names of files and directories or null * @throws IOException */ public String[] list() throws IOException { if (isFile() && !isHidden()) return getFSFile().list(); if (isSMB()) try { final SmbFile sf = getSmbFile(); if (!sf.isDirectory() || sf.isHidden()) return null; try { return TimeoutRequest.list(sf, SMB_TIMEOUT); } catch (final SmbException e) { throw new IOException("SMB.list SmbException for " + sf.toString() + ": " + e.getMessage()); } } catch (final MalformedURLException e) { throw new IOException("SMB.list MalformedURLException for " + toNormalform(false) + ": " + e.getMessage()); } return null; }
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, null, null);
@Override public Long call() { try { return file.length(); } catch (final SmbException e) { return Long.valueOf(0); } } }).call(timeout).longValue();
/** * create a smb File * Please call isSMB() before using this class * @throws MalformedURLException */ public SmbFile getSmbFile() throws MalformedURLException { if (!isSMB()) throw new MalformedURLException(); final String url = unescape(this.toNormalform(true)); return new SmbFile(url); }
@Override public Boolean call() { try { return file.exists(); } catch (final SmbException e) { return Boolean.FALSE; } } }).call(timeout).booleanValue();
public long lastModified() throws IOException { if (isFile()) return getFSFile().lastModified(); if (isSMB()) try { return getSmbFile().lastModified(); // return TimeoutRequest.lastModified(getSmbFile(), SMB_TIMEOUT); // a timeout request is a bad idea, that will create a lot of concurrent threads during crawling } catch (final SmbException e) { throw new IOException("SMB.lastModified SmbException (" + e.getMessage() + ") for " + toNormalform(false)); } catch (final MalformedURLException e) { throw new IOException("SMB.lastModified MalformedURLException (" + e.getMessage() + ") for " + toNormalform(false)); } return 0; }
@Override public Boolean call() { try { return file.isDirectory(); } catch (final SmbException e) { return Boolean.FALSE; } } }).call(timeout).booleanValue();
public String getName() throws IOException { if (isFile()) return getFSFile().getName(); if (isSMB()) try { return getSmbFile().getName(); } catch (final MalformedURLException e) { throw new IOException("SMB.getName MalformedURLException (" + e.getMessage() + ") for " + toNormalform(false) ); } if (isFTP()) { return this.getFileName(); } return null; }
@Override public Long call() { try { return file.lastModified(); } catch (final SmbException e) { return Long.valueOf(0); } } }).call(timeout).longValue();
public boolean isDirectory() throws IOException { if (isFile()) return getFSFile().isDirectory(); if (isSMB()) try { return TimeoutRequest.isDirectory(getSmbFile(), SMB_TIMEOUT); } catch (final SmbException e) { throw new IOException("SMB.isDirectory SmbException (" + e.getMessage() + ") for " + toNormalform(false)); } catch (final MalformedURLException e) { throw new IOException("SMB.isDirectory MalformedURLException (" + e.getMessage() + ") for " + toNormalform(false)); } return false; }
@Override public Boolean call() { try { return file.canRead(); } catch (final SmbException e) { return Boolean.FALSE; } } }).call(timeout).booleanValue();
@Override public Boolean call() { try { return file.isHidden(); } catch (final SmbException e) { return Boolean.FALSE; } } }).call(timeout).booleanValue();
@Override public Boolean call() { try { return file.canWrite(); } catch (final SmbException e) { return Boolean.FALSE; } } }).call(timeout).booleanValue();
public long length() { if (isFile()) try { return getFSFile().length(); } catch (final Throwable e) { return -1; } if (isSMB()) try { return getSmbFile().length(); //return TimeoutRequest.length(getSmbFile(), SMB_TIMEOUT); // a timeout request is a bad idea, that will create a lot of concurrent threads during crawling } catch (final Throwable e) { return -1; } return -1; }
public boolean exists() throws IOException { if (isFile()) return getFSFile().exists(); if (isSMB()) try { return TimeoutRequest.exists(getSmbFile(), SMB_TIMEOUT); } catch (final SmbException e) { throw new IOException("SMB.exists SmbException (" + e.getMessage() + ") for " + toNormalform(false)); } catch (final MalformedURLException e) { throw new IOException("SMB.exists MalformedURLException (" + e.getMessage() + ") for " + toNormalform(false)); } return false; }
public boolean canRead() throws IOException { if (isFile()) return getFSFile().canRead(); if (isSMB()) try { return TimeoutRequest.canRead(getSmbFile(), SMB_TIMEOUT); } catch (final SmbException e) { throw new IOException("SMB.canRead SmbException (" + e.getMessage() + ") for " + toNormalform(false)); } catch (final MalformedURLException e) { throw new IOException("SMB.canRead MalformedURLException (" + e.getMessage() + ") for " + toNormalform(false)); } return false; }