This repository was archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuploadFTP.java
More file actions
494 lines (431 loc) · 16 KB
/
uploadFTP.java
File metadata and controls
494 lines (431 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* By attaching this document to the given files (the �work�), you, the licensee,
* are hereby granted free usage in both personal and commerical environments,
* without any obligation of attribution or payment (monetary or otherwise).
*
* The licensee is free to use, copy, modify, publish, distribute, sublicence,
* and/or merchandise the work, subject to the licensee inflecting a positive
* message unto someone. This includes (but is not limited to): smiling,
* being nice, saying �thank you�, assisting other persons, or any
* similar actions percolating the given concept.
*
* The above copyright notice serves as a permissions notice also,
* and may optionally be included in copies or portions of the work.
*
* The work is provided �as is�, without warranty or support, express or implied.
* The author(s) are not liable for any damages, misuse, or other claim, whether
* from or as a consequence of usage of the given work.
*/
package Proiect;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
/**
* This class opens the FTP upload window that allow the user to upload a file
* to his/hers FTP server.
*
* @author Stefan Cosma
*
*/
public class uploadFTP {
final JFrame uploadFTP = new JFrame("FTP Transfer");
final JFrame tree = new JFrame("Server documents");
JFileChooser chooserf = new JFileChooser(".");
public static FileDialog sd = null;
String filename = null;
String filepath = null;
static JLabel titleFTP = new JLabel("Connection");
public static JTextField adressf = new JTextField("", 35);
public static JTextField userf = new JTextField("", 35);
public static JPasswordField passf = new JPasswordField("", 35);
public static JTextField folderf = new JTextField(35);
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel inpan1 = new JPanel();
JPanel inpan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
final JLabel adress = new JLabel("Server Address: ");
final JLabel user = new JLabel("User: ");
final JLabel pass = new JLabel("Password: ");
final JLabel folder = new JLabel("Default Folder: ");
final JLabel filen = new JLabel("File: ");
final JLabel filenf = new JLabel();
final JLabel status = new JLabel();
JButton connect = new JButton("Transfer");
JButton browsef = new JButton("Add..");
JButton adv = new JButton("");
JButton exit = new JButton("");
final ComponentMover cm = new ComponentMover(); // muta fereastra
public static ImageIcon getImageIcon(String name) {
return new ImageIcon(ClassLoader.getSystemResource(name));
}
public uploadFTP() {
uploadFTP.setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getClassLoader().getResource("assets/ico.png")));
uploadFTP.setUndecorated(true);
uploadFTP.getRootPane().setBorder(
BorderFactory.createLineBorder(Encrypter.color_black, 2));
uploadFTP.setSize(400, 240);
uploadFTP.setLocationRelativeTo(Encrypter.Center);
uploadFTP.setResizable(false);
uploadFTP.setVisible(true);
cm.registerComponent(uploadFTP);
uploadFTP.setLayout(new BorderLayout());
pan1.setLayout(new BorderLayout());
pan2.setLayout(new BorderLayout());
inpan1.setLayout(new GridLayout(6, 2, 0, 10));
inpan2.setLayout(new GridLayout(1, 2, 20, 0));
pan3.setLayout(new BorderLayout());
pan4.setLayout(new FlowLayout());
uploadFTP.add(pan3, BorderLayout.EAST);
pan3.add(adv, BorderLayout.SOUTH);
uploadFTP.add(pan1, BorderLayout.NORTH);
pan1.add(titleFTP, BorderLayout.WEST);
pan1.add(exit, BorderLayout.EAST);
uploadFTP.add(pan2, BorderLayout.CENTER);
pan2.add(inpan1, BorderLayout.NORTH);
inpan1.add(adress);
inpan1.add(adressf);
inpan1.add(user);
inpan1.add(userf);
inpan1.add(pass);
inpan1.add(passf);
inpan1.add(folder);
inpan1.add(folderf);
inpan1.add(filen);
inpan1.add(filenf);
inpan1.add(status);
pan2.add(inpan2, BorderLayout.SOUTH);
inpan2.add(browsef);
inpan2.add(connect);
uploadFTP.add(pan4, BorderLayout.WEST);
propFTP();
actionFTP();
/* BOF Load state */
try {
FileInputStream fileStream = new FileInputStream(ClassLoader
.getSystemResource("upload.prop").getPath());
ObjectInputStream os = new ObjectInputStream(fileStream);
adressf.setText(os.readObject().toString());
userf.setText(os.readObject().toString());
passf.setText(os.readObject().toString());
os.close();
fileStream.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
/* EOF Load state */
}
public void propFTP() {
uploadFTP.setBackground(Encrypter.color_light);
pan1.setBackground(Encrypter.color_light);
pan1.setBorder(BorderFactory.createEmptyBorder(6, 5, 10, 5));
pan2.setBackground(Encrypter.color_light);
inpan1.setBackground(Encrypter.color_light);
inpan2.setBackground(Encrypter.color_light);
pan3.setBackground(Encrypter.color_light);
pan4.setBackground(Encrypter.color_light);
adress.setForeground(Encrypter.color_blue);
user.setForeground(Encrypter.color_blue);
pass.setForeground(Encrypter.color_blue);
folder.setForeground(Encrypter.color_blue);
filen.setForeground(Encrypter.color_blue);
adressf.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0,
Encrypter.color_dark));
adressf.setSelectionColor(Encrypter.color_black);
adressf.setSelectedTextColor(Encrypter.color_white);
adressf.setBackground(Encrypter.color_light);
userf.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0,
Encrypter.color_dark));
userf.setSelectionColor(Encrypter.color_black);
userf.setSelectedTextColor(Encrypter.color_white);
userf.setBackground(Encrypter.color_light);
passf.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0,
Encrypter.color_dark));
passf.setSelectionColor(Encrypter.color_black);
passf.setSelectedTextColor(Encrypter.color_white);
passf.setBackground(Encrypter.color_light);
folderf.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0,
Encrypter.color_dark));
folderf.setSelectionColor(Encrypter.color_black);
folderf.setSelectedTextColor(Encrypter.color_white);
folderf.setBackground(Encrypter.color_light);
status.setHorizontalAlignment(SwingConstants.RIGHT);
ImageIcon title_icon = getImageIcon("assets/icons/upload.png");
titleFTP.setForeground(Encrypter.color_blue);
titleFTP.setFont(Encrypter.font16);
titleFTP.setIcon(title_icon);
connect.setBackground(Encrypter.color_light);
connect.setBorder(BorderFactory.createEmptyBorder());
connect.setForeground(Encrypter.color_black);
connect.setFont(Encrypter.font16);
connect.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
connect.setToolTipText("Transfer File");
browsef.setBackground(Encrypter.color_light);
browsef.setBorder(BorderFactory.createEmptyBorder());
browsef.setForeground(Encrypter.color_black);
browsef.setFont(Encrypter.font16);
browsef.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
browsef.setToolTipText("Add file");
ImageIcon exit_icon = getImageIcon("assets/icons/exit.png");
exit.setBackground(Encrypter.color_light);
exit.setBorder(BorderFactory.createLineBorder(Encrypter.color_dark, 0));
exit.setForeground(Encrypter.color_black);
exit.setFont(Encrypter.font16);
exit.setIcon(exit_icon);
exit.setToolTipText("Exit");
ImageIcon adv_icon = getImageIcon("assets/icons/adv_ftp.png");
adv.setIcon(adv_icon);
adv.setBackground(Encrypter.color_light);
adv.setBorder(BorderFactory.createLineBorder(Encrypter.color_light, 0));
adv.setFont(Encrypter.font16);
adv.setToolTipText("Display server files");
adv.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
chooserf.addChoosableFileFilter(new FileNameExtensionFilter("Text",
"txt"));
chooserf.addChoosableFileFilter(new FileNameExtensionFilter("HTM",
"htm"));
chooserf.addChoosableFileFilter(new FileNameExtensionFilter("XHTML",
"xhtml"));
chooserf.addChoosableFileFilter(new FileNameExtensionFilter("HTML",
"html"));
chooserf.setAcceptAllFileFilterUsed(false);
}
/**
* Save State
*/
public void saveState() {
try {
String index1 = adressf.getText();
String index2 = userf.getText();
String index3 = String.valueOf(passf.getPassword());
FileOutputStream fileStream = new FileOutputStream(ClassLoader
.getSystemResource("upload.prop").getPath());
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(index1);
os.writeObject(index2);
os.writeObject(index3);
os.close();
fileStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionFTP() {
adressf.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
InetAddress thisIp;
try {
thisIp = InetAddress.getLocalHost();
titleFTP.setText("Connection: " + thisIp.getHostAddress()
+ " -> " + adressf.getText());
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveState();
uploadFTP.dispose();
tree.dispose();
}
});
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
String pass = String.valueOf(passf.getPassword());
try {
if (filename == null) {
status.setText("File does not exist!");
} else {
// Server address
client.connect(adressf.getText());
// Login credentials
client.login(userf.getText(), pass);
if (client.isConnected()) {
status.setText("Succesfull transfer!");
// File type
client.setFileType(FTP.BINARY_FILE_TYPE);
// File location
File file = new File(filepath);
fis = new FileInputStream(file);
// Change the folder on the server
client.changeWorkingDirectory(folderf.getText());
// Save the file on the server
client.storeFile(filename, fis);
} else {
status.setText("Transfer failed!");
}
}
client.logout();
} catch (IOException e1) {
Encrypter.printException(e1);
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e1) {
Encrypter.printException(e1);
}
}
}
});
browsef.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retval = chooserf.showOpenDialog(chooserf);
if (retval == JFileChooser.APPROVE_OPTION) {
status.setText("");
filename = chooserf.getSelectedFile().getName().toString();
filepath = chooserf.getSelectedFile().getPath();
filenf.setText(chooserf.getSelectedFile().getName()
.toString());
}
}
});
adv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tree.setSize(220, uploadFTP.getHeight());
tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
tree.setResizable(false);
tree.setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getClassLoader().getResource(
"assets/ico.png")));
tree.setUndecorated(true);
tree.getRootPane().setBorder(
BorderFactory.createLineBorder(Encrypter.color_black, 2));
tree.setVisible(true);
tree.setLayout(new BorderLayout());
JLabel labeltree = new JLabel("Server documents");
labeltree.setOpaque(true);
labeltree.setBackground(Encrypter.color_light);
labeltree.setBorder(BorderFactory.createMatteBorder(8, 10, 10,
0, Encrypter.color_light));
labeltree.setForeground(Encrypter.color_blue);
labeltree.setFont(Encrypter.font16);
JButton refresh = new JButton("");
ImageIcon refresh_icon = getImageIcon("assets/icons/refresh.png");
refresh.setIcon(refresh_icon);
refresh.setBackground(Encrypter.color_light);
refresh.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
refresh.setForeground(Encrypter.color_black);
refresh.setFont(Encrypter.font16);
refresh.setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
final FTPClient client = new FTPClient();
DefaultMutableTreeNode top = new DefaultMutableTreeNode(adressf
.getText());
DefaultMutableTreeNode files = null;
DefaultMutableTreeNode leaf = null;
final JTree tree_view = new JTree(top);
tree_view.setForeground(Encrypter.color_black);
tree_view.setBorder(BorderFactory
.createEmptyBorder(0, 10, 0, 0));
tree_view.putClientProperty("JTree.lineStyle", "None");
tree_view.setBackground(Encrypter.color_light);
JScrollPane scrolltree = new JScrollPane(tree_view);
scrolltree.setBackground(Encrypter.color_light);
scrolltree.getVerticalScrollBar().setPreferredSize(
new Dimension(0, 0));
UIManager.put("Tree.textBackground", Encrypter.color_light);
UIManager.put("Tree.selectionBackground", Encrypter.color_blue);
UIManager.put("Tree.selectionBorderColor", Encrypter.color_blue);
tree_view.updateUI();
final String pass = String.valueOf(passf.getPassword());
try {
client.connect(adressf.getText());
client.login(userf.getText(), pass);
client.enterLocalPassiveMode();
if (client.isConnected()) {
try {
FTPFile[] ftpFiles = client.listFiles();
for (FTPFile ftpFile : ftpFiles) {
files = new DefaultMutableTreeNode(ftpFile
.getName());
top.add(files);
if (ftpFile.getType() == FTPFile.DIRECTORY_TYPE) {
FTPFile[] ftpFiles1 = client
.listFiles(ftpFile.getName());
for (FTPFile ftpFile1 : ftpFiles1) {
leaf = new DefaultMutableTreeNode(
ftpFile1.getName());
files.add(leaf);
}
}
}
} catch (IOException e1) {
Encrypter.printException(e1);
}
client.disconnect();
} else {
status.setText("Failed connection!");
}
} catch (IOException e1) {
Encrypter.printException(e1);
} finally {
try {
client.disconnect();
} catch (IOException e1) {
Encrypter.printException(e1);
}
}
tree.add(labeltree, BorderLayout.NORTH);
tree.add(scrolltree, BorderLayout.CENTER);
tree.add(refresh, BorderLayout.SOUTH);
uploadFTP.addComponentListener(new ComponentListener() {
public void componentMoved(ComponentEvent e) {
tree.setLocation(uploadFTP.getX() + 405,
uploadFTP.getY());
}
public void componentShown(ComponentEvent e) {
}
public void componentResized(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
});
uploadFTP.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {
tree.toFront();
}
public void windowOpened(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
});
refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tree.dispose();
tree.setVisible(true);
}
});
}
});
}
}