import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File;
System.out.println("New session"); int act;
opt = Optional.empty();
dos.writeUTF("Enter existing filename"); do {
String filename = dis.readUTF(); if (filename.equals("!")) {
res = "Cancelled"; dos.writeInt(1); break;
}
opt = Arrays.stream(files)
.filter(file > file.getName().equals(filename))
.findFirst(); dos.writeInt(opt.isEmpty() ? 0 : 2);
} while (opt.isEmpty());
if (opt.isPresent()) {
byte[] bytes = Files.readAllBytes(opt.get().toPath()); dos.writeInt(bytes.length);
dos.write(bytes);
res = "Downloaded file: " + opt.get().getName();
}
}
System.out.println("Download > " + res);
}
break;
case 3: {
dos.writeUTF("Select your file"); String filename = dis.readUTF(); String originalName = filename; while (!filename.equals("!") &
new File(folder, filename).exists()
) {
dos.writeInt(0);
dos.writeUTF(filename + " exists. Dou you:\n" + "c: cancel\n" +
"r: rename\n" +
"s: select other file"); String s = dis.readUTF();
if (s.equalsIgnoreCase("c")) { res = "Cancelled";
break;
}
filename = dis.readUTF();
if (s.equalsIgnoreCase("s")) { originalName = filename;
}
}
if (filename.equals("!")) { res = "Cancelled"; dos.writeInt(1);
} else if (!new File(folder, filename).exists()) { dos.writeInt(2);
int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data);
Files.write(folder.toPath().resolve(filename), data); res = originalName + " uploaded as " + filename;
}
}
}
if (filename.equals("!")) { res = "Cancelled"; dos.writeInt(1);
} else if (!new File(folder, filename).exists()) { dos.writeInt(2);
int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data);
Files.write(folder.toPath().resolve(filename), data); res = originalName + " uploaded as " + filename;
}
System.out.println("Upload > " + res);
}
break; default: {
printMenu(dos); continue;
}
}
dos.writeUTF(res);
System.out.println(" ");
} while (act = 0);
}
}
static void printMenu(DataOutputStream dos) throws IOException { dos.writeUTF("Menu:\n" +
"0 > End session\n" +
"1 > List all files\n" +
"2 > Download file\n" +
"3 > Upload file\n" + "Other > Menu"
);
}
}
FTPClient klassi:
package lab6;
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket;
import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Scanner;
import java.util.stream.Collectors;
public class FTPClient {
public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in);
Socket soc = new Socket("localhost", 8080);
DataOutputStream dos = new DataOutputStream(soc.getOutputStream()); DataInputStream dis = new DataInputStream(soc.getInputStream());
int act;
Path folder = Path.of("/home/ik/Projects/tda_lab_IK/folder_client");
System.out.println("Client started"); System.out.println(dis.readUTF());
do { System.out.println(dis.readUTF()); act = in.nextInt(); dos.writeInt(act);
switch (act) { case 2: {
System.out.println(dis.readUTF()); String filename = in.next(); dos.writeUTF(filename);
int code;
while ((code = dis.readInt()) = 0) { System.out.println("File not found"); filename = in.next(); dos.writeUTF(filename);
}
if (code = 2) {
int len = dis.readInt(); byte[] data = new byte[len]; dis.read(data);
Files.write(folder.resolve(filename), data);
}
}
break; case 3: {
System.out.println(dis.readUTF());
List
list = Files.list(folder).collect(Collectors.toList()); if (list.isEmpty()) {
System.out.println("Empty client folder"); dos.writeUTF("!");
} else {
for (int i = 0; i < list.size(); i +) {
System.out.println(i + "." + list.get(i).getFileName());
}
int k; do {
System.out.print("Enter available file number: "); k = in.nextInt();
} while (k = list.size()); dos.writeUTF(list.get(k).getFileName().toString());
int code;
while ((code = dis.readInt()) = 0) { System.out.println(dis.readUTF()); String s = in.next(); dos.writeUTF(s);
if (s.equalsIgnoreCase("c")) { break;
}
if (s.equalsIgnoreCase("s")) {
for (int i = 0; i < list.size(); i +) {
System.out.println(i + "." + list.get(i).getFileName());
}
do {
System.out.print("Enter available file number: "); k = in.nextInt();
} while (k = list.size()); dos.writeUTF(list.get(k).getFileName().toString());
} else {
dos.writeUTF(in.next());
}
}
if (code = 2) {
byte[] bytes = Files.readAllBytes(list.get(k)); dos.writeInt(bytes.length);
dos.write(bytes);
}
}
}
break;
}
System.out.println(dis.readUTF()); System.out.println(" ");
} while (act = 0);
}
}