-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfromServer.java
More file actions
109 lines (93 loc) · 3.18 KB
/
fromServer.java
File metadata and controls
109 lines (93 loc) · 3.18 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
import java.io.*;
/**
* Created by linda on 2/4/2017.
*/
public class fromServer {
private theSocket kkSocket;
private Command command;
public static final String PASV = "pasv";
public fromServer(theSocket socket, Command command){
this.kkSocket = socket;
this.command = command;
}
/*
print server response
deal with multi-line response
*/
public void printResponse(){
String serverResponse = "";
do{
serverResponse = kkSocket.readTheLine();
System.out.println("<-- " + serverResponse);
}while(Utils.notlastline(serverResponse));
}
/*
print server response of command "dir"; get response from data connection
*/
private void printSpecial(theSocket second_socket){
String serverReponse;
while(( serverReponse = second_socket.readDataLine() ) != null){
System.out.println(serverReponse);
}
}
/*
feed user input to ftp server
*/
public void takeInput(String userInput){
command.setUserinput(userInput);
String userinput_command = command.getUserinput_command();
String userinput_var = command.getUserinput_var();
String ftp_command = command.getFTPcommand();
System.out.println("--> " + userinput_command.toUpperCase() + (userinput_var != null? " " + userinput_var : ""));
if(!command.specialCommand(userinput_command)){
kkSocket.getout().println(ftp_command);
printResponse();
}
else specialInput(userInput);
}
/*
when user input is "dir" or "get"
*/
private void specialInput(String userInput){
command.setUserinput(userInput);
kkSocket.getout().println(PASV);
String serverRes = kkSocket.readTheLine();
String[] info = Utils.IPandPort(serverRes).split(",");
// a second socket
theSocket second_socket = new theSocket(Utils.getIP(info), Utils.getPort(info));
second_socket.createSocket("data");
// switch to binary mode
kkSocket.getout().println("type I");
printResponse();
// send a second command
kkSocket.getout().println(command.getFTPcommand());
printResponse();
if(Integer.parseInt(Utils.response_code) == 550)
return;
if(command.getUserinput_command().equals("dir")){
printSpecial(second_socket);
}
else{
getFile(second_socket, userInput);
}
printResponse();
}
/*
transfer file to local machine
*/
private void getFile(theSocket second_socket, String userInput) {
command.setUserinput(userInput);
try{
OutputStream oos = new FileOutputStream(new File("./" + command.getUserinput_var()));
byte[] buf = new byte[10];
int toread;
while ((toread = second_socket.readChar(buf)) > -1) {
oos.write(buf, 0, toread);
oos.flush();
}
oos.close();
}catch(IOException e){
System.out.println("0x38E Access to local file " + command.getUserinput_var() + " denied");
}
}
}