Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.BufferedOutputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.swing.*;
- class ftpUpload extends JFrame implements Runnable {
- static long b;
- public static void main(String[] args) {
- new ftpUpload();
- }
- public ftpUpload() {
- initGui();
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- }
- JTextField server;
- JTextField username;
- JPasswordField password;
- JLabel sServer;
- JLabel sUsername;
- JLabel sPassword;
- JProgressBar jP;
- JButton sFile;
- JButton uploadButton;
- void initGui() {
- /* init window components */
- server = new JTextField();
- username = new JTextField();
- password = new JPasswordField();
- jP = new JProgressBar();
- sFile = new JButton();
- uploadButton = new JButton();
- sServer = new JLabel();
- sUsername = new JLabel();
- sPassword = new JLabel();
- sServer.setLocation(0, 0);
- sServer.setSize(100, 15);
- sServer.setText("Server address");
- add(sServer);
- server.setLocation(0, 15);
- server.setSize(250, 20);
- add(server);
- sUsername.setLocation(0, 40);
- sUsername.setSize(100, 15);
- sUsername.setText("Username");
- add(sUsername);
- username.setLocation(0, 55);
- username.setSize(100, 20);
- add(username);
- sPassword.setLocation(0, 80);
- sPassword.setSize(100, 15);
- sPassword.setText("Password");
- add(sPassword);
- password.setLocation(0, 95);
- password.setSize(100, 20);
- add(password);
- sFile.setLocation(0, 120);
- sFile.setSize(120, 30);
- sFile.setText("Select file");
- sFile.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- select_file();
- }
- });
- add(sFile);
- uploadButton.setLocation(125, 120);
- uploadButton.setSize(120, 30);
- uploadButton.setText("Upload");
- uploadButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent e) {
- upload_file();
- }
- });
- add(uploadButton);
- jP.setLocation(0, 160);
- jP.setSize(250, 15);
- add(jP);
- setLayout(null);
- setSize(260, 220);
- setTitle("Ftp Upload");
- setVisible(true);
- }
- File f;
- void select_file() {
- JFileChooser j = new JFileChooser();
- if (j.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
- f = j.getSelectedFile();
- }
- }
- void upload_file() {
- if (f != null) {
- enableComponents(false);
- new Thread(this).start();
- }
- }
- void enableComponents(boolean a) {
- uploadButton.setEnabled(a);
- sFile.setEnabled(a);
- server.setEnabled(a);
- username.setEnabled(a);
- password.setEnabled(a);
- }
- void update(int a, int b) {
- jP.setValue(a);
- uploadButton.setText(String.valueOf(a) + "% - " + b + "Kbps");
- }
- String getPassword(char[] a) {
- StringBuilder s = new StringBuilder();
- for (char b : a)
- s.append(b);
- return s.toString();
- }
- public void run() {
- try {
- if (server.getText().substring(0, 6).equals("ftp://"))
- server.setText(server.getText().substring(6));
- URL url = new URL("ftp://" + username.getText() + ":" + getPassword(password.getPassword()) + "@" + server.getText() + "/" + f.getName());
- URLConnection con = url.openConnection();
- BufferedOutputStream out = new BufferedOutputStream(con.getOutputStream());
- FileInputStream in = new FileInputStream(f);
- int i = 0;
- int c = 0;
- byte[] bytesIn = new byte[102400];
- b = System.currentTimeMillis();
- while ((i = in.read(bytesIn)) >= 0) {
- out.write(bytesIn, 0, i);
- c += i;
- try {
- update((int)(((double)c / (double)f.length()) * 100), (int)(i / (System.currentTimeMillis() - b)));
- b = System.currentTimeMillis();
- } catch (Exception e) {}
- }
- out.close();
- in.close();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(this, "Error!\n Ftp server information may be incorrect.");
- ex.printStackTrace();
- }
- enableComponents(true);
- uploadButton.setText("Upload");
- }
- }
Add Comment
Please, Sign In to add comment