"Go" Jsch-java implementation of SFTP (file download detailed article)

Source: Internet
Author: User

Tags: system util test Application home get HTM java size

The previous article described the use of Jsch to achieve file upload function, this article mainly about the Jsch implementation of file download function. and introduce some auxiliary methods of SFTP, such as Cd,ls and so on. Similarly, Jsch file download also supports three modes of transmission: OVERWRITE, resume and Append, please refer to the previous essay: Jsch-java implementation of SFTP (file upload detailed article)
File download
The Jsch file download is implemented by invoking the Get method of the Channelsftp object. There are 9 overloaded methods for Get methods in Channelsftp:
Publicvoid get (string src, string dst) The file named SRC on the destination server is downloaded locally, and the local file name is DST. If DST is a directory, the file name downloaded locally will be the same as the SRC file name. (Note: src must be a file and cannot be a directory) with the default transfer mode: OVERWRITE
publicvoid Get (string src, string dst, sftpprogressmonitor Monitor) The file named SRC on the destination server is downloaded locally, and the local file name is DST. If DST is a directory, the file name downloaded locally will be the same as the SRC file name. (Note: src must be a file and cannot be a directory) with the default transfer mode: Overwrite and monitor the file transfer progress using the monitor object that implements the Sftpprogressmonitor interface.
publicvoid Get (string src, string dst,sftpprogressmonitor Monitor, int mode) The file named SRC on the destination server is downloaded locally, and the local file name is DST. If DST is a directory, the file name downloaded locally will be the same as the SRC file name. (Note: src must be a file, Cannot specify the file transfer mode as mode (mode is selectable as: Channelsftp.overwrite,channelsftp.resume,channelsftp.append) and uses the Moni that implements the Sftpprogressmonitor interface Tor object to monitor the file's transfer progress.
Publicvoid get (String src, outputstream DST) The file named SRC on the destination server is downloaded to local, and the downloaded data is written to the output stream object DST (e.g. file output stream). Using the default transfer mode: OVERWRITE
publicvoid Get (String src, outputstream DST, sftpprogressmonitor Monitor) The file named SRC on the destination server is downloaded to local, and the downloaded data is written to the output stream object DST (e.g. file output stream). Use the default transport mode: Overwrite and monitor the file transfer progress using the monitor object that implements the Sftpprogressmonitor interface.
publicvoid Get (String src, outputstream DST, sftpprogressmonitor Monitor, int mode, long skip) The file named SRC on the destination server is downloaded to local, and the downloaded data is written to the output stream object DST (e.g. file output stream). Specify the file transfer mode as mode and monitor the file transfer progress using the monitor object that implements the Sftpprogressmonitor interface. Skip specifies a hop read, which is the data from the src file skipping skip bytes when downloading. (This parameter is generally deprecated and is set to 0 by default)
Public InputStream get (String src) The method returns an input stream that contains the file data named SRC on the target server. The data can be read from the input stream and eventually transferred to local (e.g., data is written to a local file after reading the data) (Note: This method does not support multiple file transfer modes, how to read and save data determined by the application itself)
Public InputStream get (String src, sftpprogressmonitor Monitor) The method returns an input stream that contains the file data named SRC on the target server. Data can be read from the input stream and eventually transferred to local data (e.g., after reading the data to a local file) and monitor the file's transfer progress using the monitor object that implements the Sftpprogressmonitor interface. (Note: This method does not support multiple file transfer modes, how to read and save data determined by the application itself)
Public InputStream get (String src, Final sftpprogressmonitor monitor, Finallong Skip) The method returns an input stream that contains the file data named SRC on the target server. Data can be read from the input stream and eventually transferred to local data (e.g., after reading the data to a local file) and monitor the file's transfer progress using the monitor object that implements the Sftpprogressmonitor interface. (Note: This method does not support multiple file transfer modes, how to read and save data as determined by the application itself) skip specifies a hop read, which is the data from the src file skipping skip bytes when downloading. (This parameter is generally deprecated and is set to 0 by default)
Application Examples:Sftpgettest.java
Package Com.longyg.sftp;import Java.io.fileoutputstream;import Java.io.outputstream;import java.util.HashMap;    Import Java.util.map;import Com.jcraft.jsch.channelsftp;import Com.jcraft.jsch.sftpattrs;public class SFTPGetTest {    Public Sftpchannel Getsftpchannel () {return new Sftpchannel ();        public static void Main (string[] args) throws Exception {sftpgettest test = new Sftpgettest ();        map<string, string> sftpdetails = new hashmap<string, string> ();        Set the host IP, port, username, password sftpdetails.put (sftpconstants.sftp_req_host, "10.9.167.55");        Sftpdetails.put (sftpconstants.sftp_req_username, "root");        Sftpdetails.put (Sftpconstants.sftp_req_password, "Arthur");                Sftpdetails.put (Sftpconstants.sftp_req_port, "22");        Sftpchannel channel = Test.getsftpchannel ();                Channelsftp chsftp = Channel.getchannel (sftpdetails, 60000);        String filename = "/home/omc/ylong/sftp/intpahcfg.tar.gz"; Sftpattrs ATTR = chsftp.stat (filename);                Long fileSize = Attr.getsize ();        String DST = "d:\\intpahcfg.tar.gz";        OutputStream out = new FileOutputStream (DST);                        try {chsftp.get (filename, DST, new Fileprogressmonitor (fileSize));//Code Snippet 1 Chsftp.get (filename, out, new Fileprogressmonitor (fileSize)); Code Snippet 2/** * Code Snippet 3 * InputStream is = chsftp.get (filename, new            Myprogressmonitor ());            byte[] Buff = new byte[1024 * 2];            int read;                if (is = null) {System.out.println ("Start to read input stream");                    do {read = Is.read (buff, 0, buff.length);                    if (Read > 0) {out.write (buff, 0, read);                } out.flush ();                } while (read >= 0);            SYSTEM.OUT.PRINTLN ("input stream read done.");} */} catch (Exception e) {e.printstacktrace ();            } finally {chsftp.quit ();        Channel.closechannel (); }    }}
Note: Uncomment the code Snippet 1, code snippet 2, code snippet 3, respectively, in the main method, and run the program to test it. These three pieces of code illustrate how to use the various put methods of Jsch to download files. Program section:
Sftpchannel channel = Test.getsftpchannel (); Channelsftp chsftp = Channel.getchannel (sftpdetails, 60000);
These two lines of statements get a Channelsftp object, concrete implementation see the previous essay: Jsch-java implementation of SFTP (file upload detailed article) Note: Each get method uses a Sftpprogressmonitor interface implementation object (Fileprogressmonitor and Myprogressmonitor) to monitor the transmission progress, concrete implementation see the previous essay: Jsch- Java implementation of SFTP (file upload detailed article) Code Snippet 1: Directly download the file named SRC on the target server to local and the local file name is DST. (Note: When using this method, DST can be a directory, and if DST is a directory, the file name downloaded to the local will be the same as the src file name) Code Snippet 2: Download the file named SRC on the target server to a local output stream object, which is a file output stream Code Snippet 3: Download the file by reading the input stream data returned by the Get method. In this example, the read data is written to a local file. In this way, the application can set the size of the data block per read input stream, that is, the size of each data block transferred. For example, in this case:
byte[] Buff = new byte[1024 * 2];

This statement specifies a data block size of 2KB per transmission, which can be seen from the output of the test snippet 3 that it does transmit only 2KB, or 2048 bytes, at a time:

logs
Transferring begin. Start to read input streamcurrently transferred total size:2048 bytescurrently transferred total size:4096 bytescurrentl Y transferred total size:6144 bytescurrently transferred total size:8192 bytescurrently transferred total size:10240 by tescurrently transferred total size:12288 bytescurrently transferred total size:14336 bytes ...

Jsch file download and file Upload transfer progress, are implemented through the implementation of the Sftpprogressmonitor interface. So here is not the details, please refer to the previous essay: Jsch-java implementation of SFTP (file upload detailed article)

article source:http://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html

"Go" Jsch-java implementation of SFTP (file download detailed article)

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.

Tags Index: