Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. package de.rsmg.mi.ftp;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.apache.commons.vfs2.FileObject;
  10. import org.apache.commons.vfs2.FileSystemOptions;
  11. import org.apache.commons.vfs2.Selectors;
  12. import org.apache.commons.vfs2.impl.StandardFileSystemManager;
  13. import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Qualifier;
  17. import org.springframework.stereotype.Service;
  18.  
  19. @Service
  20. @Qualifier("sftpService")
  21. public class SftpService implements IFtpService {
  22.  
  23. private static Logger logger = LoggerFactory.getLogger(SftpService.class);
  24.  
  25. @Override
  26. public List<String> getBackupInfo(String server, String login, String password) throws FtpServiceException {
  27. List<String> result = new ArrayList<String>();
  28.  
  29. return result;
  30. }
  31.  
  32.  
  33. @Override
  34. public boolean deleteFileFromFtp(String server, int port, String login, String password, String remoteFile, boolean ssl) throws FtpServiceException {
  35. boolean success = false;
  36. return success;
  37. }
  38.  
  39. @Override
  40. public boolean uploadFileToFtp(String server, int port, String login, String password, String remoteFile, String localFile, boolean ssl) throws FtpServiceException {
  41. StandardFileSystemManager manager = new StandardFileSystemManager();
  42. try {
  43.  
  44. // Initializes the file manager
  45. manager.init();
  46.  
  47. // Setup our SFTP configuration
  48. FileSystemOptions opts = new FileSystemOptions();
  49. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
  50. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  51. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  52.  
  53. // check if the file exists
  54. //String filepath = localDirectory + fileToFTP;
  55. File file = new File(localFile);
  56. //if (!file.exists()) throw new RuntimeException("Error. Local file not found");
  57.  
  58. // Create the SFTP URI using the host name, userid, password, remote path and
  59. // file name
  60. String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteFile;
  61.  
  62. // Create local file object
  63. FileObject localFileObj = manager.resolveFile(file.getAbsolutePath());
  64.  
  65. // Create remote file object
  66. FileObject remoteFileObj = manager.resolveFile(sftpUri, opts);
  67.  
  68. // Copy local file to sftp server
  69. remoteFileObj.copyFrom(localFileObj, Selectors.SELECT_SELF);
  70. //System.out.println("File upload successful");
  71.  
  72. } catch (Exception ex) {
  73. ex.printStackTrace();
  74. return false;
  75. } finally {
  76. manager.close();
  77. }
  78.  
  79. return true;
  80. }
  81.  
  82. @Override
  83. public boolean downloadFileFromFtp(String server, int port, String login, String password, String remoteFile, String localFile, boolean ssl, boolean deleteFile) throws FtpServiceException {
  84. StandardFileSystemManager manager = new StandardFileSystemManager();
  85. try {
  86.  
  87. // Initializes the file manager
  88. manager.init();
  89.  
  90. // Setup our SFTP configuration
  91. FileSystemOptions opts = new FileSystemOptions();
  92. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
  93. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  94. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  95.  
  96. // check if the file exists
  97. //String filepath = localDirectory + fileToFTP;
  98. File file = new File(localFile);
  99. //if (!file.exists()) throw new RuntimeException("Error. Local file not found");
  100.  
  101. // Create the SFTP URI using the host name, userid, password, remote path and
  102. // file name
  103. String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteFile;
  104.  
  105. // Create local file object
  106. FileObject localFileObj = manager.resolveFile(file.getAbsolutePath());
  107.  
  108. // Create remote file object
  109. FileObject remoteFileObj = manager.resolveFile(sftpUri, opts);
  110.  
  111. // Copy local file to sftp server
  112. localFileObj.copyFrom(remoteFileObj, Selectors.SELECT_SELF);
  113. //System.out.println("File upload successful");
  114.  
  115. } catch (Exception ex) {
  116. ex.printStackTrace();
  117. return false;
  118. } finally {
  119. manager.close();
  120. }
  121.  
  122. return true;
  123. }
  124.  
  125. @Override
  126. public boolean checkIfDirectoryExists(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
  127. StandardFileSystemManager manager = new StandardFileSystemManager();
  128. boolean success = false;
  129. try {
  130.  
  131. // Initializes the file manager
  132. manager.init();
  133.  
  134. // Setup our SFTP configuration
  135. FileSystemOptions opts = new FileSystemOptions();
  136. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
  137. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  138. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  139.  
  140. // Create the SFTP URI using the host name, userid, password, remote path and
  141. // file name
  142. String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
  143.  
  144. // List all the files in that directory.Try to give the directory path
  145. FileObject remoteDirectory=manager.resolveFile(sftpUri);
  146. //Check if the file exists
  147. if(remoteDirectory.exists()){
  148. success = true;
  149. } else {
  150. success = false;
  151. }
  152.  
  153. } catch (Exception ex) {
  154. ex.printStackTrace();
  155. } finally {
  156. manager.close();
  157. }
  158.  
  159. return success;
  160. }
  161.  
  162. @Override
  163. public Map<String, Long> listDirectoryFiles(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
  164. StandardFileSystemManager manager = new StandardFileSystemManager();
  165. Map<String, Long> resultFiles = new LinkedHashMap<>();
  166. try {
  167.  
  168. // Initializes the file manager
  169. manager.init();
  170.  
  171. // Setup our SFTP configuration
  172. FileSystemOptions opts = new FileSystemOptions();
  173. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
  174. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  175. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  176.  
  177. // Create the SFTP URI using the host name, userid, password, remote path and
  178. // file name
  179. String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
  180.  
  181. // List all the files in that directory.Try to give the directory path
  182. FileObject localFileObject=manager.resolveFile(sftpUri);
  183. FileObject[] children = localFileObject.getChildren();
  184. for ( int i = 0; i < children.length; i++ ){
  185. //System.out.println( children[ i ].getName().getBaseName() + children[i].getContent().getSize() );
  186. resultFiles.put(children[ i ].getName().getBaseName(), children[i].getContent().getSize() );
  187. }
  188.  
  189. } catch (Exception ex) {
  190. ex.printStackTrace();
  191. } finally {
  192. manager.close();
  193. }
  194.  
  195. return resultFiles;
  196. }
  197.  
  198. @Override
  199. public boolean createNewRemoteDirectory(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
  200. StandardFileSystemManager manager = new StandardFileSystemManager();
  201. boolean success = false;
  202. try {
  203.  
  204. // Initializes the file manager
  205. manager.init();
  206.  
  207. // Setup our SFTP configuration
  208. FileSystemOptions opts = new FileSystemOptions();
  209. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
  210. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  211. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  212.  
  213. // Create the SFTP URI using the host name, userid, password, remote path and
  214. // file name
  215. String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
  216.  
  217. // List all the files in that directory.Try to give the directory path
  218. FileObject remoteDirectory=manager.resolveFile(sftpUri);
  219.  
  220. remoteDirectory.createFolder();
  221. success = true;
  222.  
  223. } catch (Exception ex) {
  224. ex.printStackTrace();
  225. } finally {
  226. manager.close();
  227. }
  228.  
  229. return success;
  230.  
  231.  
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement