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 !");
/** * Creates an output stream to write the file content to. */ @Override protected OutputStream doGetOutputStream(boolean bAppend) throws Exception { return new SmbFileOutputStream(file, bAppend); }
try { String filePath = "myserver/dir"; String fileName = "myFile.txt"; String user = "username"; String password = "password"; // URL: smb://user:passwd@host/share/filname SmbFileOutputStream out = new SmbFileOutputStream("smb://" + user + ":" + password + "@" + filePath + File.separator + fileName); out.write("test".getBytes()); out.close(); } catch (Exception e) { e.printStackTrace(); }
protected void doSendFragment(byte[] buf, int off, int length, boolean isDirect) throws IOException { if (out != null && out.isOpen() == false) throw new IOException("DCERPC pipe is no longer open"); if (in == null) in = (SmbFileInputStream)pipe.getNamedPipeInputStream(); if (out == null) out = (SmbFileOutputStream)pipe.getNamedPipeOutputStream(); if (isDirect) { out.writeDirect( buf, off, length, 1 ); return; } out.write(buf, off, length); } protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException {
/** * Copy file to smb share. * * @param sourceFile the source file * @param destinationFile the destination file * @param domain the domain * @param domainUsername the domain username * @param domainPassword the domain password * @implNote |copyFileToSMBShare;|c:\test.xml|smb://Server/folder/copied.xml|domain|username|password| */ public void copyFileToSMBShare(String sourceFile, String destinationFile, String domain, String domainUsername, String domainPassword) { Path source = Paths.get(sourceFile); if (!destinationFile.startsWith("smb:")) { destinationFile = "smb:" + destinationFile; } try { NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, domainUsername, domainPassword); SmbFile sFile = new SmbFile(destinationFile, auth); SmbFileOutputStream sfos = new SmbFileOutputStream(sFile); Files.copy(source, sfos); sfos.close(); } catch (IOException e) { throw new CommandException("Something went wrong copying the file " + e.getMessage()); } }
throw new IOException( "Bad file descriptor" ); ensureOpen();
public void close() throws IOException { state = 0; if (out != null) out.close(); } }
/** * Writes len bytes from the specified byte array starting at * offset off to this file output stream. * * @param b * The array * @throws IOException * if a network error occurs */ @Override public void write ( byte[] b, int off, int len ) throws IOException { writeDirect(b, off, len, 0); }
SmbFileOutputStream ( SmbFile file, boolean append, int openFlags, int access, int sharing ) throws SmbException { this.file = file; this.append = append; this.openFlags = openFlags; this.sharing = sharing; this.access = access | SmbConstants.FILE_WRITE_DATA; try ( SmbTreeHandleImpl th = file.ensureTreeConnected() ) { this.smb2 = th.isSMB2(); try ( SmbFileHandleImpl fh = ensureOpen() ) { if ( append ) { this.fp = fh.getInitialSize(); } init(th); if ( !append && this.smb2 ) { // no open option for truncating, need to truncate the file Smb2SetInfoRequest treq = new Smb2SetInfoRequest(th.getConfig(), fh.getFileId()); treq.setFileInformation(new FileEndOfFileInformation(0)); th.send(treq, RequestParam.NO_RETRY); } } } catch ( CIFSException e ) { throw SmbException.wrap(e); } }
protected synchronized SmbFileHandleImpl ensureOpen () throws CIFSException { if ( !isOpen() ) { // one extra acquire to keep this open till the stream is released this.handle = this.file.openUnshared(this.openFlags, this.access, this.sharing, SmbConstants.ATTR_NORMAL, 0).acquire(); if ( this.append ) { this.fp = this.handle.getInitialSize(); if ( log.isDebugEnabled() ) { log.debug("File pointer is at " + this.fp); } } return this.handle; } log.trace("File already open"); return this.handle.acquire(); }
/** * This URLConnection method just returns a new <tt>SmbFileOutputStream</tt> created with this file. * * @throws IOException thrown by <tt>SmbFileOutputStream</tt> constructor */ public OutputStream getOutputStream() throws IOException { return new SmbFileOutputStream( this ); }
protected void doSendFragment(byte[] buf, int off, int length, boolean isDirect) throws IOException { if (out != null && out.isOpen() == false) throw new IOException("DCERPC pipe is no longer open"); if (in == null) in = (SmbFileInputStream)pipe.getNamedPipeInputStream(); if (out == null) out = (SmbFileOutputStream)pipe.getNamedPipeOutputStream(); if (isDirect) { out.writeDirect( buf, off, length, 1 ); return; } out.write(buf, off, length); } protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException {
String user = "user:password"; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user); String path = "smb://my_machine_name/D/MyDev/test.txt"; SmbFile sFile = new SmbFile(path, auth); SmbFileOutputStream sfos = new SmbFileOutputStream(sFile); sfos.write("Test".getBytes()); sfos.close();
throw new IOException( "Bad file descriptor" ); ensureOpen();
public void close() throws IOException { state = 0; if (out != null) out.close(); } }
/** * Writes len bytes from the specified byte array starting at * offset off to this file output stream. * * @param b * The array * @throws IOException * if a network error occurs */ @Override public void write ( byte[] b, int off, int len ) throws IOException { writeDirect(b, off, len, 0); }
SmbFileOutputStream ( SmbFile file, boolean append, int openFlags, int access, int sharing ) throws SmbException { this.file = file; this.append = append; this.openFlags = openFlags; this.sharing = sharing; this.access = access | SmbConstants.FILE_WRITE_DATA; try ( SmbTreeHandleImpl th = file.ensureTreeConnected() ) { this.smb2 = th.isSMB2(); try ( SmbFileHandleImpl fh = ensureOpen() ) { if ( append ) { this.fp = fh.getInitialSize(); } init(th); if ( !append && this.smb2 ) { // no open option for truncating, need to truncate the file Smb2SetInfoRequest treq = new Smb2SetInfoRequest(th.getConfig(), fh.getFileId()); treq.setFileInformation(new FileEndOfFileInformation(0)); th.send(treq, RequestParam.NO_RETRY); } } } catch ( CIFSException e ) { throw SmbException.wrap(e); } }
protected synchronized SmbFileHandleImpl ensureOpen () throws CIFSException { if ( !isOpen() ) { // one extra acquire to keep this open till the stream is released this.handle = this.file.openUnshared(this.openFlags, this.access, this.sharing, SmbConstants.ATTR_NORMAL, 0).acquire(); if ( this.append ) { this.fp = this.handle.getInitialSize(); if ( log.isDebugEnabled() ) { log.debug("File pointer is at " + this.fp); } } return this.handle; } log.trace("File already open"); return this.handle.acquire(); }