从SFTP服务器下载所有文件的Java代码:

huangapple go评论172阅读模式
英文:

downloading all files from sftp server in java

问题

以下是您的代码的翻译部分:

  1. package svb.ftp.bs.load;
  2. import com.jcraft.jsch.JSchException;
  3. import com.jcraft.jsch.SftpException;
  4. import java.io.FileNotFoundException;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import com.jcraft.jsch.Channel;
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSch;
  10. import java.io.InputStream;
  11. import java.io.IOException;
  12. import java.io.PrintWriter;
  13. import java.io.StringWriter;
  14. import java.util.List;
  15. import org.apache.commons.net.ftp.FTP;
  16. import org.apache.commons.io.IOUtils;
  17. import org.junit.Test;
  18. import java.util.Vector;
  19. public class downloadFtp {
  20. public static void main(String[] args) {
  21. String SFTPHOST = "host";
  22. int SFTPPORT = 22;
  23. String SFTPUSER = "user";
  24. String SFTPPASS = "Pass";
  25. String privateKey = "E:\\Oracle\\mywork\\DownloadFTP\\ppk.ppk";
  26. String SFTPWORKINGDIR = "Inbox/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT";
  27. com.jcraft.jsch.Session session = null;
  28. Channel channel = null;
  29. ChannelSftp channelSftp = null;
  30. JSch jsch = new JSch();
  31. try {
  32. jsch.addIdentity(privateKey);
  33. session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  34. session.setPassword(SFTPPASS);
  35. java.util.Properties config = new java.util.Properties();
  36. config.put("StrictHostKeyChecking", "no");
  37. session.setConfig(config);
  38. session.connect();
  39. channel = session.openChannel("sftp");
  40. channel.connect();
  41. System.out.println("Connected");
  42. channelSftp = (ChannelSftp) channel;
  43. channelSftp.cd(SFTPWORKINGDIR);
  44. InputStream is = channelSftp.get(SFTPWORKINGDIR);
  45. IOUtils.copy(is, System.out);
  46. is.close();
  47. System.out.println("File Uploaded to FTP Server Sucessfully.");
  48. } catch (JSchException jse) {
  49. // TODO: Add catch code
  50. jse.printStackTrace();
  51. } catch (SftpException se) {
  52. // TODO: Add catch code
  53. se.printStackTrace();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }

您提供的错误信息如下:

  1. 4: Can't change directory: /Inbox/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT
  2. at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:350)
  3. at svb.ftp.bs.load.downloadFtp.main(downloadFtp.java:67)
  4. Process exited with exit code 0.

由于您的代码是从SFTP服务器下载文件,但是您尝试使用cd命令切换到文件的路径上。这是不正确的,因为您只需要提供远程文件的路径来下载它,而不是切换到该目录。如果您想要下载指定路径下的所有文件,您需要列出目录中的文件列表,然后逐个下载。

英文:

I am trying to Download All files from folder on SFTP server , Below is my Code Please suggest changes so i can download All available files from remote directory and can save to Local Directory

  1. package svb.ftp.bs.load;
  2. import com.jcraft.jsch.JSchException;
  3. import com.jcraft.jsch.SftpException;
  4. import java.io.FileNotFoundException;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import com.jcraft.jsch.Channel;
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSch;
  10. import java.io.InputStream;
  11. import java.io.IOException;
  12. import java.io.PrintWriter;
  13. import java.io.StringWriter;
  14. import java.util.List;
  15. import org.apache.commons.net.ftp.FTP;
  16. import org.apache.commons.io.IOUtils;
  17. import org.junit.Test;
  18. import com.jcraft.jsch.Channel;
  19. import com.jcraft.jsch.ChannelExec;
  20. import com.jcraft.jsch.ChannelSftp;
  21. import com.jcraft.jsch.JSch;
  22. import com.jcraft.jsch.JSchException;
  23. import com.jcraft.jsch.Session;
  24. import com.jcraft.jsch.SftpException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.Vector;
  28. public class downloadFtp {
  29. public static void main(String[] args) {
  30. String SFTPHOST = "host";
  31. int SFTPPORT = 22;
  32. String SFTPUSER = "user";
  33. String SFTPPASS = "Pass";
  34. String privateKey = "E:\\Oracle\\mywork\\DownloadFTP\\ppk.ppk";
  35. String SFTPWORKINGDIR = "Inbox/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT";
  36. com.jcraft.jsch.Session session = null;
  37. Channel channel = null;
  38. ChannelSftp channelSftp = null;
  39. JSch jsch = new JSch();
  40. try {
  41. jsch.addIdentity(privateKey);
  42. session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  43. session.setPassword(SFTPPASS);
  44. java.util.Properties config = new java.util.Properties();
  45. config.put("StrictHostKeyChecking", "no");
  46. session.setConfig(config);
  47. session.connect();
  48. channel = session.openChannel("sftp");
  49. channel.connect();
  50. System.out.println("Connected");
  51. channelSftp = (ChannelSftp) channel;
  52. channelSftp.cd(SFTPWORKINGDIR);
  53. InputStream is = channelSftp.get(SFTPWORKINGDIR);
  54. IOUtils.copy(is, System.out);
  55. is.close();
  56. System.out.println("File Uploaded to FTP Server Sucessfully.");
  57. } catch (JSchException jse) {
  58. // TODO: Add catch code
  59. jse.printStackTrace();
  60. } catch (SftpException se) {
  61. // TODO: Add catch code
  62. se.printStackTrace();
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }

Currently when i run the code it show below error

  1. 4: Can't change directory: /Inbox/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT
  2. at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:350)
  3. at svb.ftp.bs.load.downloadFtp.main(downloadFtp.java:67)
  4. Process exited with exit code 0.

答案1

得分: 3

以下是翻译好的内容:


堆栈跟踪正在告诉您出了什么问题。

> 无法更改目录:/收件箱/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT

问题出在:

  1. channelSftp.cd(SFTPWORKINGDIR);

在这里,您正在传递文件名,但它期望一个目录(cd 代表更改目录)。您不能将 CD 用于文件。

如果传递文件夹/路径,它会起作用。请尝试以下内容:

  1. channelSftp.cd("/收件箱/一个文件夹/");

如何下载全部:
参考:https://ganeshtiwaridotcomdotnp.blogspot.com/2020/07/download-files-from-ftp-using-jsch-java.html

为了下载所有文件,首先列出根文件夹中的所有文件,遍历它...如果是文件,则下载;如果是文件夹,则列出内容,并对子文件夹进行递归操作。

以下是如何从指定文件夹下载文件的示例:

  1. import com.jcraft.jsch.*;
  2. import java.util.Properties;
  3. import java.util.Vector;
  4. public class JschDownload {
  5. public static void main(String[] args) {
  6. Session session = null;
  7. Channel channel = null;
  8. JSch jsch = new JSch();
  9. try {
  10. session = jsch.getSession("demo", "test.rebex.net", 22);
  11. session.setPassword("password");
  12. Properties config = new Properties();
  13. config.put("StrictHostKeyChecking", "no");
  14. session.setConfig(config);
  15. session.connect();
  16. System.out.println("会话已连接");
  17. channel = session.openChannel("sftp");
  18. channel.connect();
  19. System.out.println("已连接");
  20. ChannelSftp channelSftp = (ChannelSftp) channel;
  21. downloadFromFolder(channelSftp, "/");
  22. downloadFromFolder(channelSftp, "/pub/example/");
  23. System.out.println("文件已成功上传到FTP服务器。");
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. } finally {
  27. if (channel != null) {
  28. channel.disconnect();
  29. }
  30. session.disconnect();
  31. }
  32. }
  33. static void downloadFromFolder(ChannelSftp channelSftp, String folder) throws SftpException {
  34. Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(folder);
  35. System.out.println(entries);
  36. // 从根文件夹下载所有文件
  37. for (ChannelSftp.LsEntry en : entries) {
  38. if (en.getFilename().equals(".") || en.getFilename().equals("..") || en.getAttrs().isDir()) {
  39. continue;
  40. }
  41. System.out.println(en.getFilename());
  42. channelSftp.get(folder + en.getFilename(), en.getFilename());
  43. }
  44. }
  45. }

有关 jsch 库的示例和文档,请参考 http://www.jcraft.com/jsch/examples/


英文:

The stacktrace is telling you what's wrong.

> Can't change directory: /Inbox/ARR_SI_MedicalTranscriptionBillCorp_PD_20200724_32137.TXT

Problem is with:

  1. channelSftp.cd(SFTPWORKINGDIR);

Here you are passing filename but its expecting a directory (cd stands for Change Directory). You cannot do CD to a file.

If you pass a folder/path it would work. Try the following :

  1. channelSftp.cd(&quot;/Inbox/a_folder/&quot;);

How to Download All:
Reference: https://ganeshtiwaridotcomdotnp.blogspot.com/2020/07/download-files-from-ftp-using-jsch-java.html

In order to download all files, first list all files in root folder, iterate over it.. if its a file download, if its a folder list the content and keep doing it recursively for sub folders.

The following is an example of how to download files from a defined folder:

  1. import com.jcraft.jsch.*;
  2. import java.util.Properties;
  3. import java.util.Vector;
  4. public class JschDownload {
  5. public static void main(String[] args) {
  6. Session session = null;
  7. Channel channel = null;
  8. JSch jsch = new JSch();
  9. try {
  10. session = jsch.getSession(&quot;demo&quot;, &quot;test.rebex.net&quot;, 22);
  11. session.setPassword(&quot;password&quot;);
  12. Properties config = new Properties();
  13. config.put(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
  14. session.setConfig(config);
  15. session.connect();
  16. System.out.println(&quot;session connect&quot;);
  17. channel = session.openChannel(&quot;sftp&quot;);
  18. channel.connect();
  19. System.out.println(&quot;Connected&quot;);
  20. ChannelSftp channelSftp = (ChannelSftp) channel;
  21. downloadFromFolder(channelSftp, &quot;/&quot;);
  22. downloadFromFolder(channelSftp, &quot;/pub/example/&quot;);
  23. System.out.println(&quot;File Uploaded to FTP Server Sucessfully.&quot;);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. } finally {
  27. if (channel != null) {
  28. channel.disconnect();
  29. }
  30. session.disconnect();
  31. }
  32. }
  33. static void downloadFromFolder(ChannelSftp channelSftp, String folder) throws SftpException {
  34. Vector&lt;ChannelSftp.LsEntry&gt; entries = channelSftp.ls(folder);
  35. System.out.println(entries);
  36. //download all from root folder
  37. for (ChannelSftp.LsEntry en : entries) {
  38. if (en.getFilename().equals(&quot;.&quot;) || en.getFilename().equals(&quot;..&quot;) || en.getAttrs().isDir()) {
  39. continue;
  40. }
  41. System.out.println(en.getFilename());
  42. channelSftp.get(folder + en.getFilename(), en.getFilename());
  43. }
  44. }
  45. }

Refer to http://www.jcraft.com/jsch/examples/ for examples and documentation of the jsch library.

答案2

得分: 0

以下是您提供的代码部分的翻译结果:

这是一个用于获取所有子文件夹中文件的递归方法。

  1. private void downloadFromFolder(String remotePath, String localPath) throws SftpException {
  2. try {
  3. Vector<ChannelSftp.LsEntry> entries = channel.ls(remotePath);
  4. System.out.println(entries);
  5. // 从根文件夹下载所有内容
  6. for (ChannelSftp.LsEntry en : entries) {
  7. if (!en.getFilename().equals(".") && !en.getFilename().equals("..") && en.getAttrs().isDir()) {
  8. Path path = Paths.get(localPath + "\\" + en.getFilename());
  9. Files.createDirectories(path);
  10. downloadFromFolder(remotePath + "/" + en.getFilename(), path.toString());
  11. continue;
  12. }
  13. if (en.getFilename().equals(".") || en.getFilename().equals("..")) {
  14. continue;
  15. }
  16. System.out.println(en.getFilename());
  17. channel.get(remotePath + "/" + en.getFilename(), localPath + "\\" + en.getFilename());
  18. }
  19. } catch (Exception e) {
  20. }
  21. }

请注意,我只翻译了您提供的代码部分,不包括问题或其他内容。如有需要,您可以将这些翻译应用到您的项目中。

英文:

here a recursive methode to get all files in sub folder also

  1. private void downloadFromFolder( String remotePath, String localPath ) throws SftpException {
  2. try
  3. {
  4. Vector&lt;ChannelSftp.LsEntry&gt; entries = channel.ls(remotePath);
  5. System.out.println(entries);
  6. //download all from root folder
  7. for (ChannelSftp.LsEntry en : entries) {
  8. if(!en.getFilename().equals(&quot;.&quot;) &amp;&amp; !en.getFilename().equals(&quot;..&quot;) &amp;&amp; en.getAttrs().isDir())
  9. {
  10. Path path = Paths.get(localPath + &quot;\\&quot; + en.getFilename());
  11. Files.createDirectories(path);
  12. downloadFromFolder(remotePath + &quot;/&quot; + en.getFilename(),path.toString());
  13. continue;
  14. }
  15. if (en.getFilename().equals(&quot;.&quot;) || en.getFilename().equals(&quot;..&quot;)) {
  16. continue;
  17. }
  18. System.out.println(en.getFilename());
  19. channel.get(remotePath + &quot;/&quot; + en.getFilename(), localPath + &quot;\\&quot; + en.getFilename());
  20. }
  21. }catch(Exception e)
  22. {
  23. }
  24. }

huangapple
  • 本文由 发表于 2020年7月28日 23:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63137475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定