Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. enter code here
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import org.apache.commons.io.FilenameUtils;
  9. import org.apache.commons.net.ftp.FTP;
  10. import org.apache.commons.net.ftp.FTPClient;
  11.  
  12.  
  13. enter code here
  14.  
  15. /**
  16. * Created by mufarrij on 8/18/17.
  17. */
  18. public class FtpClient3 {
  19.  
  20. public static boolean uploadSingleFile(FTPClient ftpClient,
  21. String localFilePath, String remoteFilePath) throws IOException {
  22. File localFile = new File(localFilePath);
  23.  
  24. InputStream inputStream = new FileInputStream(localFile);
  25. String ext1 = FilenameUtils.getExtension(localFilePath);
  26. try {
  27. if(ext1== "ajx" || ext1== "am" || ext1=="asa" || ext1=="properties" || ext1=="asc" || ext1=="asp" || ext1=="aspx" || ext1=="awk" || ext1=="bat" || ext1=="c" || ext1=="cdf" || ext1=="cf" || ext1=="cfg" || ext1=="cfm" || ext1=="cgi" || ext1=="cnf" || ext1=="conf" || ext1=="cpp" || ext1=="css" || ext1=="csv" || ext1=="ctl" || ext1=="dat" || ext1=="dhtml"|| ext1=="diz" || ext1=="file" || ext1=="forward" || ext1=="grp" || ext1=="h" || ext1=="hpp"|| ext1=="hqx" || ext1=="hta" || ext1=="htaccess" || ext1=="htc" || ext1=="htm" || ext1=="html" || ext1=="htpasswd" || ext1=="htt" || ext1=="htx"|| ext1=="in"|| ext1=="inc"|| ext1=="info"|| ext1=="ini"|| ext1=="ink"|| ext1=="java" || ext1=="js" || ext1=="jsp" || ext1=="log" || ext1=="logfile"|| ext1=="m3u"|| ext1=="m4a"|| ext1=="m4a"|| ext1=="mak"|| ext1=="map"|| ext1=="model"|| ext1=="msg"|| ext1=="nfo"|| ext1=="nsi" || ext1=="info" || ext1=="old" || ext1=="pas" || ext1=="patch" || ext1=="perl" || ext1=="php"|| ext1=="php2"|| ext1=="php3" || ext1=="php4" || ext1=="php5" || ext1=="php6" || ext1=="phtml" || ext1=="pix" || ext1=="pl" || ext1=="pm" || ext1=="po"|| ext1=="pwd"|| ext1=="py" || ext1=="qmail" || ext1=="rb" || ext1=="rbl" || ext1=="rbw" || ext1=="readme" || ext1=="reg" || ext1=="rss" || ext1=="ruby" || ext1=="session" || ext1=="setup" || ext1=="sh" || ext1=="shtm" || ext1=="shtml" || ext1=="sql" || ext1=="ssh" || ext1=="stm" || ext1=="style" || ext1=="svg" || ext1=="tcl" || ext1=="text" || ext1=="threads" || ext1=="tmpl" || ext1=="tpl" || ext1=="txt" || ext1=="ubb" || ext1=="vbs" || ext1=="xhtml" || ext1=="xml" || ext1=="xrc" || ext1=="xsl" || ext1=="rtf"){
  28. ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
  29. ftpClient.setFileTransferMode(ftpClient.ASCII_FILE_TYPE);
  30. }
  31. else{
  32. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  33. ftpClient.setFileTransferMode(ftpClient.BINARY_FILE_TYPE);
  34. }
  35.  
  36.  
  37. return ftpClient.storeFile(remoteFilePath, inputStream);
  38. } catch (Exception e) {
  39. System.out.println("error: " + e.getMessage());
  40. e.printStackTrace();
  41. throw e;
  42. } finally {
  43. inputStream.close();
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. enter code here
  52.  
  53. System.out.println("LISTING directory: " + localParentDir);
  54.  
  55. File localDir = new File(localParentDir);
  56. File[] subFiles = localDir.listFiles();
  57. if (subFiles != null && subFiles.length > 0) {
  58. for (File item : subFiles) {
  59. String remoteFilePath = remoteDirPath + "/" + remoteParentDir
  60. + "/" + item.getName();
  61. if (remoteParentDir.equals("")) {
  62. remoteFilePath = remoteDirPath + "/" + item.getName();
  63. }
  64.  
  65.  
  66. if (item.isFile()) {
  67. // upload the file
  68. String localFilePath = item.getAbsolutePath();
  69. System.out.println("About to upload the file: " + localFilePath);
  70. //CHANGING
  71. boolean uploaded = upfile(ftpClient,
  72. localFilePath, remoteFilePath);
  73. if (uploaded) {
  74. System.out.println("UPLOADED a file to: "
  75. + remoteFilePath);
  76. } else {
  77. System.out.println("COULD NOT upload the file: "
  78. + localFilePath);
  79. }
  80. } else {
  81. // create directory on the server
  82. boolean created = ftpClient.makeDirectory(remoteFilePath);
  83. if (created) {
  84. System.out.println("CREATED the directory: "
  85. + remoteFilePath);
  86. } else {
  87. System.out.println("COULD NOT create the directory: "
  88. + remoteFilePath);
  89. }
  90.  
  91. // upload the sub directory
  92. String parent = remoteParentDir + "/" + item.getName();
  93. if (remoteParentDir.equals("")) {
  94. parent = item.getName();
  95. }
  96.  
  97. localParentDir = item.getAbsolutePath();
  98. uploadDirectory(ftpClient, remoteDirPath, localParentDir,
  99. parent);
  100. }
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. public static void main(String args[])throws Exception{
  111.  
  112. String server = "ruby.aepona.com";
  113. int port = 21;
  114. String user = "ftp_iot";
  115. String pass = "ftp_iot";
  116.  
  117.  
  118.  
  119.  
  120. FTPClient ftpclient = new FTPClient();
  121.  
  122.  
  123. try {
  124. // connect and login to the server
  125. ftpclient.connect(server, port);
  126. ftpclient.login(user, pass);
  127.  
  128. // use local passive mode to pass firewall
  129. ftpclient.enterLocalPassiveMode();
  130.  
  131. System.out.println("Connected");
  132.  
  133. String remoteDirPath = "/dist";
  134. String localDirPath ="/data/IOT/ase-core/com.aepona.iotp.parent/com.aepona.iotp.concert.ui/dist";
  135. //String localDirPath ="/data/classes";
  136.  
  137. FtpClient3.uploadDirectory(ftpclient, remoteDirPath, localDirPath, "");
  138.  
  139. // log out and disconnect from the server
  140. ftpclient.logout();
  141. ftpclient.disconnect();
  142.  
  143. System.out.println("Disconnected");
  144. } catch (IOException ex) {
  145. ex.printStackTrace();
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement