Ganymed SSH-2 for Java series 8 sftpv3client Description __java

Source: Internet
Author: User
Tags lstat mkdir

Ganymed SSH-2 for Java Series 8 Sftpv3client Description:


Sftpv3client is a SFTP (Protocol version 3) of SSH for Java, and the client implements the Java class through a simple synchronization of the SSH-2 connection.


Its basic external methods are as follows:


Public sftpv3fileattributes Stat (String path) throws IOException;

--Retrieves the file properties of the file.

Returns the Sftpv3fileattributes object, which is mentioned in an article before this object, mainly to record directories, permissions, and other information;

	
	Public sftpv3fileattributes Stat (String path) throws IOException
	{return
		statboth (path, Packet.ssh_fxp_stat) ;
	}


Public sftpv3fileattributes Lstat (String path) throws IOException

--Retrieves the file properties of the file. This method does not follow symbolic links on the server.

	Public sftpv3fileattributes Lstat (String path) throws IOException
	{return
		statboth (path, packet.ssh_fxp_ LSTAT);
	}

Public Vector ls (String dirname) throws IOException

--"lists the contents of a directory."

This is demonstrated when you delete a non-empty directory, as it does with the Linux environment LS display information, and can not be presented recursively.

Public Vector ls (String dirname) throws IOException
	{
		byte[] handle = Opendirectory (dirname);
		Vector result = Scandirectory (handle);
		CloseHandle (handle);
		return result;
	}

public void mkdir (String dirname, int posixpermissions) throws IOException

--"Create a new directory

The first parameter is the directory path, and the second is the permission code: for example, 0600 represents RW's specific reference to Linux permissions.

public void mkdir (String dirname, int posixpermissions) throws IOException
	{
		int req_id = Generatenextrequestid ( );

		Typeswriter tw = new Typeswriter ();
		Tw.writestring (dirname, charsetname);
		Tw.writeuint32 (attribflags.ssh_filexfer_attr_permissions);
		Tw.writeuint32 (posixpermissions);

		SendMessage (Packet.ssh_fxp_mkdir, req_id, Tw.getbytes ());

		Expectstatusokmessage (req_id);
	}

public void rm (String fileName) throws IOException
--"Delete a file

preceded by a demo;

	public void rm (String fileName) throws IOException
	{
		int req_id = Generatenextrequestid ();

		Typeswriter tw = new Typeswriter ();
		Tw.writestring (FileName, charsetname);

		SendMessage (Packet.ssh_fxp_remove, req_id, Tw.getbytes ());

		Expectstatusokmessage (req_id);
	}

public void RmDir (String dirname) throws IOException

--"Delete an empty directory

Before there is a demo, also implements a how to delete a non-empty directory;

public void RmDir (String dirname) throws IOException
	{
		int req_id = Generatenextrequestid ();

		Typeswriter tw = new Typeswriter ();
		Tw.writestring (dirname, charsetname);

		SendMessage (Packet.ssh_fxp_rmdir, req_id, Tw.getbytes ());

		Expectstatusokmessage (req_id);
	}


public void mv (string oldpath, String NewPath) throws IOException

--"Move a file or directory

public void mv (string oldpath, String newpath) throws IOException
	{
		int req_id = Generatenextrequestid ();

		Typeswriter tw = new Typeswriter ();
		Tw.writestring (OldPath, charsetname);
		Tw.writestring (NewPath, charsetname);

		SendMessage (Packet.ssh_fxp_rename, req_id, Tw.getbytes ());

		Expectstatusokmessage (req_id);
	}

Public Sftpv3filehandle Openfilero (String fileName) throws IOException

--"Open a file to read content;"

	Public Sftpv3filehandle Openfilero (String filename) throws IOException
	{return
		openfile (filename, 0x00000001, NULL); Ssh_fxf_read	
	}

Let's keep looking: Sftpv3filehandle

public class Sftpv3filehandle
{
	final sftpv3client client;
	Final byte[] FileHandle;
	Boolean isclosed = false;

	/* The constructor isn't public

	/sftpv3filehandle (sftpv3client client, byte[] h)
	{
		this.client = client;
  this.filehandle = h;
	}

	/**
	 * Get the Sftpv3client instance which created this handle. 
	 * 
	 * @return A sftpv3client instance.
	 * * Public
	sftpv3client getclient ()
	{return
		client;
	}

	/**
	 * Check If this handle is closed with the {@link sftpv3client#closefile (Sftpv3filehandle)} method
	 * of the <code>SFTPv3Client</code> instance which created the handle.
	 * 
	 * @return If the handle is closed.
	 *
	/public boolean isclosed ()
	{return
		isclosed;
	}

The content is read in binary notation byte[];


Public Sftpv3filehandle openfilerw (String fileName) throws IOException

--"Open the file to read and write operations;"

	Public Sftpv3filehandle openfilerw (String filename) throws IOException
	{return
		openfile (FileName, 0x00000003 , null); Ssh_fxf_read | Ssh_fxf_write
	}

Public Sftpv3filehandle CreateFile (String fileName) throws IOException

--"Create a file for read and write operations

	Public Sftpv3filehandle CreateFile (String filename) throws IOException
	{return
		CreateFile (fileName, null);
	

Public Sftpv3filehandle CreateFile (String fileName, Sftpv3fileattributes attr) throws IOException

--"Create a file to open it and read and write

Sftpv3fileattributes permissions and other settings objects;

	Public Sftpv3filehandle CreateFile (String fileName, Sftpv3fileattributes attr) throws IOException
	{
		return OpenFile (fileName, 0x00000008 | 0x00000003, attr); Ssh_fxf_creat | Ssh_fxf_read | Ssh_fxf_write
	}

Public Sftpv3filehandle createfiletruncate (String fileName) throws IOException

--"Create a file (if it already exists, truncate), open it and read and write

	Public Sftpv3filehandle createfiletruncate (String fileName) throws IOException
	{return
		createfiletruncate ( FileName, null);
	

Public Sftpv3filehandle createfiletruncate (String fileName, Sftpv3fileattributes attr) throws IOException

--"Create a file (if it already exists, truncate), open it and read the append write operation;"

Public Sftpv3filehandle createfiletruncate (String fileName, Sftpv3fileattributes attr) throws IOException
{
Return OpenFile (fileName, 0x00000018 | 0x00000003, attr); Ssh_fxf_creat | Ssh_fxf_trunc | Ssh_fxf_read | Ssh_fxf_write
}


Use:

Incoming conn Parameters:


	Connection conn = null;

		try {
			conn = getopenedconnection (host, username, password, port);

			Sftpv3client sftpclient = new sftpv3client (conn);
Finally, you need to close the session connection:

	Sftpclient.close ();
Conn. close ();

The rest of the way everyone can see for themselves;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.