Guest User

Untitled

a guest
Jul 11th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import javax.swing.*;
  7.  
  8. class ftpUpload extends JFrame implements Runnable {
  9. static long b;
  10. public static void main(String[] args) {
  11. new ftpUpload();
  12. }
  13.  
  14. public ftpUpload() {
  15. initGui();
  16. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17. }
  18.  
  19. JTextField server;
  20. JTextField username;
  21. JPasswordField password;
  22. JLabel sServer;
  23. JLabel sUsername;
  24. JLabel sPassword;
  25. JProgressBar jP;
  26. JButton sFile;
  27. JButton uploadButton;
  28.  
  29. void initGui() {
  30. /* init window components */
  31. server = new JTextField();
  32. username = new JTextField();
  33. password = new JPasswordField();
  34. jP = new JProgressBar();
  35. sFile = new JButton();
  36. uploadButton = new JButton();
  37. sServer = new JLabel();
  38. sUsername = new JLabel();
  39. sPassword = new JLabel();
  40.  
  41. sServer.setLocation(0, 0);
  42. sServer.setSize(100, 15);
  43. sServer.setText("Server address");
  44. add(sServer);
  45.  
  46. server.setLocation(0, 15);
  47. server.setSize(250, 20);
  48. add(server);
  49.  
  50. sUsername.setLocation(0, 40);
  51. sUsername.setSize(100, 15);
  52. sUsername.setText("Username");
  53. add(sUsername);
  54.  
  55. username.setLocation(0, 55);
  56. username.setSize(100, 20);
  57. add(username);
  58.  
  59. sPassword.setLocation(0, 80);
  60. sPassword.setSize(100, 15);
  61. sPassword.setText("Password");
  62. add(sPassword);
  63.  
  64. password.setLocation(0, 95);
  65. password.setSize(100, 20);
  66. add(password);
  67.  
  68. sFile.setLocation(0, 120);
  69. sFile.setSize(120, 30);
  70. sFile.setText("Select file");
  71. sFile.addActionListener(new java.awt.event.ActionListener() {
  72. public void actionPerformed(java.awt.event.ActionEvent e) {
  73. select_file();
  74. }
  75. });
  76. add(sFile);
  77.  
  78. uploadButton.setLocation(125, 120);
  79. uploadButton.setSize(120, 30);
  80. uploadButton.setText("Upload");
  81. uploadButton.addActionListener(new java.awt.event.ActionListener() {
  82. public void actionPerformed(java.awt.event.ActionEvent e) {
  83. upload_file();
  84. }
  85. });
  86. add(uploadButton);
  87.  
  88. jP.setLocation(0, 160);
  89. jP.setSize(250, 15);
  90. add(jP);
  91.  
  92. setLayout(null);
  93. setSize(260, 220);
  94. setTitle("Ftp Upload");
  95. setVisible(true);
  96. }
  97.  
  98. File f;
  99. void select_file() {
  100. JFileChooser j = new JFileChooser();
  101. if (j.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
  102. f = j.getSelectedFile();
  103. }
  104. }
  105.  
  106. void upload_file() {
  107. if (f != null) {
  108. enableComponents(false);
  109. new Thread(this).start();
  110. }
  111. }
  112.  
  113. void enableComponents(boolean a) {
  114. uploadButton.setEnabled(a);
  115. sFile.setEnabled(a);
  116. server.setEnabled(a);
  117. username.setEnabled(a);
  118. password.setEnabled(a);
  119. }
  120.  
  121. void update(int a, int b) {
  122. jP.setValue(a);
  123. uploadButton.setText(String.valueOf(a) + "% - " + b + "Kbps");
  124. }
  125.  
  126. String getPassword(char[] a) {
  127. StringBuilder s = new StringBuilder();
  128. for (char b : a)
  129. s.append(b);
  130. return s.toString();
  131. }
  132.  
  133. public void run() {
  134. try {
  135. if (server.getText().substring(0, 6).equals("ftp://"))
  136. server.setText(server.getText().substring(6));
  137. URL url = new URL("ftp://" + username.getText() + ":" + getPassword(password.getPassword()) + "@" + server.getText() + "/" + f.getName());
  138. URLConnection con = url.openConnection();
  139. BufferedOutputStream out = new BufferedOutputStream(con.getOutputStream());
  140. FileInputStream in = new FileInputStream(f);
  141. int i = 0;
  142. int c = 0;
  143. byte[] bytesIn = new byte[102400];
  144. b = System.currentTimeMillis();
  145. while ((i = in.read(bytesIn)) >= 0) {
  146. out.write(bytesIn, 0, i);
  147. c += i;
  148. try {
  149. update((int)(((double)c / (double)f.length()) * 100), (int)(i / (System.currentTimeMillis() - b)));
  150. b = System.currentTimeMillis();
  151. } catch (Exception e) {}
  152. }
  153. out.close();
  154. in.close();
  155. } catch (Exception ex) {
  156. JOptionPane.showMessageDialog(this, "Error!\n Ftp server information may be incorrect.");
  157. ex.printStackTrace();
  158. }
  159. enableComponents(true);
  160. uploadButton.setText("Upload");
  161. }
  162. }
Add Comment
Please, Sign In to add comment