Guest User

Untitled

a guest
Jul 31st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. public static void FTPDownloadto() {
  2. String server = "192.168.0.103";
  3. int port = 21;
  4. String user = "anonymous";
  5. String pass = "";
  6.  
  7. FTPClient ftpClient = new FTPClient();
  8. try {
  9.  
  10. ftpClient.connect(server, port);
  11. ftpClient.login(user, pass);
  12. ftpClient.enterLocalPassiveMode();
  13.  
  14. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  15.  
  16.  
  17. // APPROACH #1: uploads first file using an InputStream
  18. File firstLocalFile = new File("\storage\ext_sd\DCIM\100MEDIA\IMAG0171.jpg");
  19.  
  20. String firstRemoteFile = "IMAG0171.jpg";
  21. InputStream inputStream = new FileInputStream(firstLocalFile);
  22.  
  23. Toast.makeText(MainActivity.this, "Начало загрузки первого файла", Toast.LENGTH_SHORT).show();
  24. boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  25. inputStream.close();
  26. if (done) {
  27. Toast.makeText(MainActivity.this, "Первый фаил загружен успешно", Toast.LENGTH_SHORT).show();
  28. }
  29.  
  30. inputStream.close();
  31.  
  32. boolean completed = ftpClient.completePendingCommand();
  33. if (completed) {
  34. Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
  35. }
  36.  
  37. } catch (IOException ex) {
  38. Toast.makeText(MainActivity.this, "Ошибка:" + ex.getMessage(), Toast.LENGTH_SHORT).show();
  39. ex.printStackTrace();
  40. } finally {
  41. try {
  42. if (ftpClient.isConnected()) {
  43. ftpClient.logout();
  44. ftpClient.disconnect();
  45. }
  46. } catch (IOException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. android.os.NetworkOnMain.ThreadExseotion
  53.  
  54. <uses-permission android:name="android.permission.INTERNET" />
  55.  
  56. class FTPUploadFile extends AsyncTask<Void, Integer, Void> {
  57. //прежде чем задача выполнена
  58. @Override
  59. protected void onPreExecute() {
  60. super.onPreExecute();
  61. Toast.makeText(MainActivity.this, "Начало процесса", Toast.LENGTH_SHORT).show();
  62. }
  63.  
  64. //после окончания задачи
  65. @Override
  66. protected void onPostExecute(Void aVoid) {
  67. super.onPostExecute(aVoid);
  68. Toast.makeText(MainActivity.this, "Процесс окончен", Toast.LENGTH_SHORT).show();
  69. }
  70.  
  71. @Override
  72. protected void onProgressUpdate(Integer... values) {
  73.  
  74. }
  75.  
  76. //задача
  77. @Override
  78. protected Void doInBackground(Void... params) {
  79. String server = "192.168.0.103";
  80. int port = 21;
  81. String user = "anonymous";
  82. String pass = "";
  83.  
  84. FTPClient ftpClient = new FTPClient();
  85. try {
  86.  
  87. ftpClient.connect(server, port);
  88. ftpClient.login(user, pass);
  89. ftpClient.enterLocalPassiveMode();
  90.  
  91. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  92.  
  93.  
  94. // APPROACH #1: uploads first file using an InputStream
  95. File firstLocalFile = new File("\storage\ext_sd\DCIM\100MEDIA\IMAG0171.jpg");
  96.  
  97. String firstRemoteFile = "IMAG0171.jpg";
  98. InputStream inputStream = new FileInputStream(firstLocalFile);
  99.  
  100. Toast.makeText(MainActivity.this, "Начало загрузки первого файла", Toast.LENGTH_SHORT).show();
  101. boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  102. inputStream.close();
  103. if (done) {
  104. Toast.makeText(MainActivity.this, "Первый фаил загружен успешно", Toast.LENGTH_SHORT).show();
  105. }
  106.  
  107. inputStream.close();
  108.  
  109. boolean completed = ftpClient.completePendingCommand();
  110. if (completed) {
  111. Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
  112. }
  113.  
  114. } catch (IOException ex) {
  115. Toast.makeText(MainActivity.this, "Ошибка:" + ex.getMessage(), Toast.LENGTH_SHORT).show();
  116. ex.printStackTrace();
  117. } finally {
  118. try {
  119. if (ftpClient.isConnected()) {
  120. ftpClient.logout();
  121. ftpClient.disconnect();
  122. }
  123. } catch (IOException ex) {
  124. ex.printStackTrace();
  125. }
  126. }
  127.  
  128. return null;
  129. }
Add Comment
Please, Sign In to add comment