-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.util.Scanner; | ||
|
||
public class FileDownloadClient { | ||
private static final int PORT = 9060; | ||
private static final String IP = "127.0.0.1"; | ||
static DataInputStream in; | ||
static DataOutputStream out; | ||
static Scanner scanner; | ||
private static Socket socket; | ||
|
||
public static void main(String[] args) { | ||
scanner = new Scanner(System.in); | ||
String name = scanner.nextLine(); | ||
String path = scanner.nextLine(); | ||
try { | ||
socket = new Socket(IP, PORT); | ||
in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); | ||
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | ||
out.writeUTF("DownloadName:" + name); | ||
out.flush(); | ||
int byteNumber = (int) Double.parseDouble(in.readUTF()); | ||
byte[] allBytes = new byte[byteNumber]; | ||
for (int i = 0; i < byteNumber; i++) { | ||
allBytes[i] = in.readByte(); | ||
} | ||
FileOutputStream fileOutputStream = new FileOutputStream(path); | ||
fileOutputStream.write(allBytes); | ||
fileOutputStream.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package server; | ||
|
||
import java.io.*; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.nio.file.Files; | ||
|
||
public class FileServer { | ||
public static final int PORT = 9060; | ||
private static final String PATH = "src\\main\\resources\\"; | ||
private DataOutputStream out; | ||
private DataInputStream in; | ||
private ServerSocket fileServer; | ||
|
||
public FileServer() throws IOException { | ||
fileServer = new ServerSocket(PORT); | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
while (true) { | ||
try { | ||
Socket socket = fileServer.accept(); | ||
in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); | ||
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | ||
new main.java.FileServer.ClientHandler(out, in, socket).start(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}).start(); | ||
} | ||
|
||
static class ClientHandler extends Thread { | ||
private DataOutputStream dataOutputStream; | ||
private DataInputStream dataInputStream; | ||
private Socket socket; | ||
|
||
private ClientHandler(DataOutputStream dataOutputStream, DataInputStream dataInputStream, Socket socket) { | ||
this.dataOutputStream = dataOutputStream; | ||
this.dataInputStream = dataInputStream; | ||
this.socket = socket; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
String input; | ||
while (true) { | ||
input = dataInputStream.readUTF(); | ||
if (input.startsWith("DownloadName:")) { | ||
int colonIndex = input.indexOf(":"); | ||
String fileName = input.substring(colonIndex + 1); | ||
File file = new File(PATH + fileName); | ||
byte[] fileContent = Files.readAllBytes(file.toPath()); | ||
dataOutputStream.writeUTF("" + fileContent.length); | ||
dataOutputStream.flush(); | ||
for (byte b : fileContent) { | ||
dataOutputStream.write(b); | ||
dataOutputStream.flush(); | ||
} | ||
return; | ||
} else if (input.startsWith("UploadName:")) { | ||
String[] parts = input.split(":"); | ||
String fileName = parts[1]; | ||
int fileByteSize = Integer.parseInt(parts[2]); | ||
byte[] allBytes = new byte[fileByteSize]; | ||
for (int i = 0; i < fileByteSize; i++) { | ||
allBytes[i] = dataInputStream.readByte(); | ||
} | ||
FileOutputStream fileOutputStream = new FileOutputStream(PATH + fileName); | ||
fileOutputStream.write(allBytes); | ||
fileOutputStream.close(); | ||
return; | ||
} | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.nio.file.Files; | ||
import java.util.Scanner; | ||
|
||
public class FileUploadClient { | ||
private static final int PORT = 9060; | ||
private static final String IP = "127.0.0.1"; | ||
static DataInputStream in; | ||
static DataOutputStream out; | ||
static Scanner scanner; | ||
private static Socket socket; | ||
|
||
|
||
public static void main(String[] args) { | ||
scanner = new Scanner(System.in); | ||
String name = scanner.nextLine(); | ||
String location = scanner.nextLine(); | ||
try { | ||
socket = new Socket(IP, PORT); | ||
in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); | ||
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | ||
File file = new File(location + name); | ||
byte[] fileContent = Files.readAllBytes(file.toPath()); | ||
out.writeUTF("UploadName:" + name + ":" + fileContent.length); | ||
out.flush(); | ||
for (byte b : fileContent) { | ||
out.write(b); | ||
out.flush(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |