Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public class FTPUtil {
  2.  
  3. public static void uploadDirectory(FTPClient ftpClient,
  4. String remoteDirPath, String localParentDir, String remoteParentDir)
  5. throws IOException {
  6.  
  7. System.out.println("LISTING directory: " + localParentDir);
  8.  
  9. File localDir = new File(localParentDir);
  10. File[] subFiles = localDir.listFiles();
  11. if (subFiles != null && subFiles.length > 0) {
  12. for (File item : subFiles) {
  13. String remoteFilePath = remoteDirPath + "/" + remoteParentDir
  14. + "/" + item.getName();
  15. if (remoteParentDir.equals("")) {
  16. remoteFilePath = remoteDirPath + "/" + item.getName();
  17. }
  18.  
  19. if (item.isFile()) {
  20. // upload the file
  21. String localFilePath = item.getAbsolutePath();
  22. System.out.println("About to upload the file: " + localFilePath);
  23. boolean uploaded = uploadSingleFile(ftpClient,
  24. localFilePath, remoteFilePath);
  25. if (uploaded) {
  26. System.out.println("UPLOADED a file to: "
  27. + remoteFilePath);
  28. } else {
  29. System.out.println("COULD NOT upload the file: "
  30. + localFilePath);
  31. }
  32. } else {
  33. // create directory on the server
  34. boolean created = ftpClient.makeDirectory(remoteFilePath);
  35. if (created) {
  36. System.out.println("CREATED the directory: "
  37. + remoteFilePath);
  38. } else {
  39. System.out.println("COULD NOT create the directory: "
  40. + remoteFilePath);
  41. }
  42.  
  43. // upload the sub directory
  44. String parent = remoteParentDir + "/" + item.getName();
  45. if (remoteParentDir.equals("")) {
  46. parent = item.getName();
  47. }
  48.  
  49. localParentDir = item.getAbsolutePath();
  50. uploadDirectory(ftpClient, remoteDirPath, localParentDir,
  51. parent);
  52. }
  53. }
  54. }
  55. }
  56.  
  57. public static boolean uploadSingleFile(FTPClient ftpClient,
  58. String localFilePath, String remoteFilePath) throws IOException {
  59. File localFile = new File(localFilePath);
  60.  
  61. InputStream inputStream = new FileInputStream(localFile);
  62. try {
  63. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  64. return ftpClient.storeFile(remoteFilePath, inputStream);
  65. } finally {
  66. inputStream.close();
  67. }
  68. }
  69.  
  70. public class MainApp {
  71.  
  72. public static void main(String[] args) {
  73. String server = "127.0.0.1";
  74. int port = 14147;
  75. String user = "user";
  76. String pass = "pass";
  77.  
  78. FTPClient ftpClient = new FTPClient();
  79.  
  80. try {
  81. // connect and login to the server
  82. ftpClient.connect(server, port);
  83. // use local passive mode to pass firewall
  84. ftpClient.enterLocalPassiveMode();
  85.  
  86. ftpClient.login(user, pass);
  87.  
  88. System.out.println("Connected");
  89.  
  90. String remoteDirPath = "D:/WebServer";
  91. String localDirPath = "D:/Учёба/Сети Тяпаев/Лабы";
  92.  
  93. FTPUtil.uploadDirectory(ftpClient, remoteDirPath, localDirPath, "");
  94.  
  95. // log out and disconnect from the server
  96. ftpClient.logout();
  97. ftpClient.disconnect();
  98.  
  99. System.out.println("Disconnected");
  100. } catch (IOException ex) {
  101. ex.printStackTrace();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement