-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderReciever.java
More file actions
251 lines (172 loc) · 6.13 KB
/
FolderReciever.java
File metadata and controls
251 lines (172 loc) · 6.13 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
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FolderReciever extends JFrame {
private Socket user;
private String host;
private int port;
private String originalPath;
private File saveFile = new File("H:\\schoolHDrive\\Fun\\src\\Saves.txt");
private JTextField inPath = new JTextField("Enter The Recieving File Location (path)");
private JTextField inIpAddress = new JTextField("Enter The Sender IP Address");
private JTextField inPort = new JTextField("Enter The Port");
private JButton recieve = new JButton("RECIEVE");
private JComboBox<RecieveSession> saves = new JComboBox<RecieveSession>();
private JButton saveButton = new JButton("Save Set");
/**
* @param host
* @param port
* @param originalPath
* @throws IOException
* @throws ClassNotFoundException
* @throws InterruptedException
*/
public FolderReciever() throws IOException, ClassNotFoundException, InterruptedException {
setup();
}
private void setup() {
setSize(720, 710);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
setLayout(null);
inPath.setBounds(10, 10, 690, 70);
inPath.setFont(new Font("Monospace", Font.PLAIN, 20));
add(inPath);
inIpAddress.setBounds(10, 100, 690, 70);
inIpAddress.setFont(new Font("Monospace", Font.PLAIN, 20));
add(inIpAddress);
inPort.setBounds(10, 190, 690, 70);
inPort.setFont(new Font("Monospace", Font.PLAIN, 20));
add(inPort);
recieve.setBounds(310, 300, 100, 80);
recieve.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
recieveFiles();
} catch (ClassNotFoundException | InterruptedException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
add(recieve);
saves.setBounds(10, 400, 690, 50);
saves.setFont(new Font("Monospace", Font.PLAIN, 20));
saves.addItem(null);
try {
readInSaves();
} catch (IOException e1) { }
saves.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateSelectedItem();
}
});
add(saves);
saveButton.setBounds(310, 480, 100, 75);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
try {
saveIn();
readInSaves();
}
catch (IOException | NumberFormatException e1) { }
}
});
add(saveButton);
}
private void saveIn() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new FileReader(saveFile));
String contents = "";
String line = "";
while((line = br.readLine()) != null) {
contents += line + "\n";
}
contents += inIpAddress.getText()+" "+inPort.getText()+" "+inPath.getText();
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile));
bw.write(contents);
bw.close();
}
private void readInSaves() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(saveFile));
String line = "";
saves.removeAllItems();
while((line = br.readLine()) != null) {
String[] session = line.split(" ");
saves.addItem(new RecieveSession(session[0], Integer.parseInt(session[1]), session[2]));
}
br.close();
}
public void updateSelectedItem() {
RecieveSession item;
if((item = (RecieveSession) saves.getSelectedItem()) != null) {
inPath.setText(item.path);
inIpAddress.setText(item.ipAddress);
inPort.setText("" + item.port);
}
}
public void recieveFiles() throws InterruptedException, IOException, ClassNotFoundException {
originalPath = inPath.getText();
host = inIpAddress.getText();
port = Integer.parseInt(inPort.getText());
user = new Socket(host, port);
Thread.sleep(500);
InputStream in = user.getInputStream();
byte[] inBytes = new byte[2000000000];
int index = 0;
while (in.available() > 0) {
inBytes[index] = (byte) in.read();
index++;
}
Folder folder = (Folder) deserialize(inBytes);
Files.createDirectories(Paths.get(originalPath + folder.getAbsolutePath().substring(folder.getAbsolutePath().indexOf("\\"))));
process(folder, originalPath);
in.close();
user.close();
}
public static void process(Folder fol, String originalPath) throws IOException {
for(File f : fol.files) {
String filepath = originalPath + f.getAbsolutePath().substring(f.getAbsolutePath().indexOf("\\"));
//System.out.println(filepath);
if(f.getName().indexOf(".") < 0) {
Files.createDirectories(Paths.get(filepath));
process((Folder)f, originalPath);
} else if(f instanceof FolderFile){
System.out.println(filepath);
new File(filepath).createNewFile();
FileOutputStream fos = new FileOutputStream(new File(filepath));
fos.write(((FolderFile)f).contents);
}
}
}
private Object deserialize(byte[] ary) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(ary);
ObjectInputStream objstrm = new ObjectInputStream(in);
return objstrm.readObject();
}
public static void main(String[] args) throws Exception {
new FolderReciever();
}
}