-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath
124 lines (105 loc) · 3.64 KB
/
path
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
package sample;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
/**
* Created by dmitry on 06.07.16.
*/
public class ClientPart {
JTextArea area;
JTextField field;
Socket socket;
ArrayList<String> selectFiles;
ClientPart(){
JFrame f = new JFrame("Client");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setLayout(new BorderLayout());
f.setVisible(true);
area = new JTextArea();
field = new JTextField(20);
final JButton selectBut = new JButton("Select");
final JButton but = new JButton("Send");
but.setEnabled(false);
f.add(but, BorderLayout.SOUTH);
f.add(area);
f.add(selectBut, BorderLayout.NORTH);
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
sendFiles(selectFiles);
}
});
selectBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
selectFiles = new ArrayList<String>();
area.setText("");
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION){
area.append("Выбранны файлы для передачи:\n" );
File[] file = chooser.getSelectedFiles();
for (File d : file){
selectFiles.add(d+"");
area.append(d+"\n");
}
but.setEnabled(true);
}
}
});
}
private void sendFiles(ArrayList<String> list){
//----------------------------------------------
int port = 2154;
String addres = "127.0.0.1";
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(addres);
socket = new Socket(ipAddress, port);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//----------------------------------------------
int countFiles = list.size();
DataOutputStream outD;
try{
outD = new DataOutputStream(socket.getOutputStream());
outD.writeInt(countFiles);//отсылаем количество файлов
for(int i = 0; i<countFiles; i++){
File f = new File(list.get(i));
outD.writeLong(f.length());//отсылаем размер файла
outD.writeUTF(f.getName());//отсылаем имя файла
FileInputStream in = new FileInputStream(f);
byte [] buffer = new byte[64*1024];
int count;
while((count = in.read(buffer)) != -1){
outD.write(buffer, 0, count);
}
outD.flush();
in.close();
}
socket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientPart();
}
}