Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lesson 3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
По проекту еще ничего не сделал. Время уходит на ДЗ. Постараюсь сделать максимум к первому CodeReview
Empty file.
50 changes: 46 additions & 4 deletions src/main/java/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package client;

import server.ClientHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Swing client - File Storage
Expand Down Expand Up @@ -36,10 +43,12 @@ public Client() throws IOException {
sendFile(cmd[1]);
} else if ("download".equals(cmd[0])) {
getFile(cmd[1]);
}else{
sendMessage(cmd[0]);
}

});


addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Expand All @@ -58,12 +67,45 @@ public void windowClosing(WindowEvent e) {
}

private void getFile(String filename) {
// TODO: 13.05.2021 downloading
try {
File file = new File("C:\\Program Files\\Java\\Java4\\src\\main\\java\\server\\" + filename);

File downloadFile = new File("C:\\Program Files\\Java\\Java4\\src\\main\\java\\client\\" + filename);
if (!downloadFile.exists()) {
downloadFile.createNewFile();
}

if (!file.exists()) {
throw new FileNotFoundException();
}

long fileLength = file.length();
FileInputStream fis = new FileInputStream(file);

out.writeUTF("download");
out.writeUTF(filename);
out.writeLong(fileLength);

int read = 0;
byte[] buffer = new byte[8 * 1024];
while ((read = fis.read(buffer)) != -1) {
out.write(buffer, 0, read);
}

out.flush();

String status = in.readUTF();
System.out.println("Sending status: " + status);
} catch (FileNotFoundException e) {
System.err.println("File not found - /client/" + filename);
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendFile(String filename) {
try {
File file = new File("client/" + filename);
File file = new File("C:\\Program Files\\Java\\Java4\\src\\main\\java\\client\\" + filename);
if (!file.exists()) {
throw new FileNotFoundException();
}
Expand Down Expand Up @@ -113,6 +155,6 @@ private void sendMessage(String message) {
}

public static void main(String[] args) throws IOException {
new Client();
new client.Client();
}
}
37 changes: 34 additions & 3 deletions src/main/java/server/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void run() {
uploading(out, in);
}
if ("download".equals(command)) {
// TODO: 13.05.2021 downloading
downloading(out,in,command);
}
if ("exit".equals(command)) {
out.writeUTF("DONE");
Expand All @@ -42,9 +42,40 @@ public void run() {
}
}

private void downloading(DataOutputStream out, DataInputStream in,String filename) throws IOException{
try {
File file = new File("C:\\Program Files\\Java\\Java4\\src\\main\\java\\server\\" + filename); // read file name
if (!file.exists()) {
throw new FileNotFoundException();
}

long fileLength = file.length();
FileInputStream fis = new FileInputStream(file);

out.writeUTF("download");
out.writeUTF(filename);
out.writeLong(fileLength);

int read = 0;
byte[] buffer = new byte[8 * 1024];
while ((read = fis.read(buffer)) != -1) {
out.write(buffer, 0, read);
}

out.flush();

String status = in.readUTF();
System.out.println("Sending status: " + status);
} catch (FileNotFoundException e) {
System.err.println("File not found - /server/" + filename);
} catch (IOException e) {
e.printStackTrace();
}
}

private void uploading(DataOutputStream out, DataInputStream in) throws IOException {
try {
File file = new File("server/" + in.readUTF()); // read file name
File file = new File("C:\\Program Files\\Java\\Java4\\src\\main\\java\\server\\" + in.readUTF()); // read file name
if (!file.exists()) {
file.createNewFile();
}
Expand All @@ -70,4 +101,4 @@ private void disconnected() {
e.printStackTrace();
}
}
}
}