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

Source: Internet
Author: User
Tags deprecated



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)

Using 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)

Using the default transfer mode: OVERWRITE

Monitor the file transfer progress using the monitor object that implements the Sftpprogressmonitor interface.

publicvoid Get (string src, string dst,sftpprogressmonitor Monitor, Intmode)

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)

Specifies that the file transfer mode is modes (mode selectable value: channelsftp.overwrite,channelsftp.resume,channelsftp.append)

Monitor the file transfer progress using the monitor object that implements the Sftpprogressmonitor interface.

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).

Using the default transfer mode: OVERWRITE

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).

Specifies the file transfer mode

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. Data can be read from the input stream and eventually transferred to the local (e.g., after reading the data and writing the data to a local file)

(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 the local (e.g., after reading the data and writing the data to a local file)

Monitor the file 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 the local (e.g., after reading the data and writing the data to a local file)

Monitor the file 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)

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:


1 package com.longyg.sftp;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.OutputStream;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 import com.jcraft.jsch.ChannelSftp;
 8 import com.jcraft.jsch.SftpATTRS;
 9 
10 public class SFTPGetTest {
11 public SFTPChannel getSFTPChannel () {
12 return new SFTPChannel ();
13}
14 public static void main (String [] args) throws Exception {
15 SFTPGetTest test = new SFTPGetTest ();
16 Map <String, String> sftpDetails = new HashMap <String, String> ();
17 // Set host IP, port, user name, password
18 sftpDetails.put (SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");
19 sftpDetails.put (SFTPConstants.SFTP_REQ_USERNAME, "root");
20 sftpDetails.put (SFTPConstants.SFTP_REQ_PASSWORD, "arthur");
21 sftpDetails.put (SFTPConstants.SFTP_REQ_PORT, "22");
twenty two         
23 SFTPChannel channel = test.getSFTPChannel ();
24 ChannelSftp chSftp = channel.getChannel (sftpDetails, 60000);
25
26 String filename = "/home/omc/ylong/sftp/INTPahcfg.tar.gz";
27 SftpATTRS attr = chSftp.stat (filename);
28 long fileSize = attr.getSize ();
29
30 String dst = "D: \\ INTPahcfg.tar.gz";
31 OutputStream out = new FileOutputStream (dst);
32 try {
33
34 chSftp.get (filename, dst, new FileProgressMonitor (fileSize)); // snippet 1
35
36 // chSftp.get (filename, out, new FileProgressMonitor (fileSize)); // snippet 2
37
38 / **
39 * snippet 3
40 *
41 InputStream is = chSftp.get (filename, new MyProgressMonitor ());
42 byte [] buff = new byte [1024 * 2];
43 int read;
44 if (is! = Null) {
45 System.out.println ("Start to read input stream");
46 do {
47 read = is.read (buff, 0, buff.length);
48 if (read> 0) {
49 out.write (buff, 0, read);
50}
51 out.flush ();
52} while (read> = 0);
53 System.out.println ("input stream read done.");
54}
55 * /
56} catch (Exception e) {
57 e.printStackTrace ();
58} finally {
59 chSftp.quit ();
60 channel.closeChannel ();
61}
62}
63} 
Sftpgettest.java





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 == 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 an implementation object for the Sftpprogressmonitor interface (Fileprogressmonitor and myprogressmonitor) to monitor the transfer progress. Specific 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 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:


 
 
1 Transferring begin.
 2 Start to read input stream
 3 Currently transferred total size: 2048 bytes
 4 Currently transferred total size: 4096 bytes
 5 Currently transferred total size: 6144 bytes
 6 Currently transferred total size: 8192 bytes
 7 Currently transferred total size: 10240 bytes
 8 Currently transferred total size: 12288 bytes
 9 Currently transferred total size: 14336 bytes
10 ...





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)






Reprint Address: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.

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.