import
java.io.*;
import
java.net.Socket;
import
java.util.Scanner;
public
class
ClientSide {
public
static
void
main(String[] args)
throws
Exception
{
System.out.println(
"Client"
);
int
port =
2500
,
choice;
String ip
=
"localhost"
;
while
(
true
) {
Socket clientSocket =
new
Socket(
ip, port);
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter Your Choice - 1 For Sending File and 2 For Downloading File : "
);
choice = sc.nextInt();
sendReq(
choice,
clientSocket);
}
}
public
static
void
sendReq(
int
choice,
Socket clientSocket)
throws
Exception
{
if
(choice ==
1
)
{
sendFile(
clientSocket);
}
else
if
(choice
==
2
)
{
receiveFile(
clientSocket);
}
else
{
System.out.println(
"Invalid Choice"
);
}
}
public
static
void
sendFile(Socket clientSocket)
{
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter file Name : "
);
String name = sc.nextLine();
System.out.println(
"Enter Content : "
);
String content
= sc.nextLine();
Data data =
new
Data(
1
, name,
content);
ObjectOutputStream
outputStream;
try
{
outputStream =
new
ObjectOutputStream(
clientSocket.getOutputStream());
outputStream.writeObject(
data);
System.out.println(
"File Sent Successfully!!"
);
}
catch
(IOException e) {
System.out.println(
"Something Went Wrong"
);
e.printStackTrace();
}
}
public
static
void
receiveFile(Socket clientSocket)
throws
Exception
{
ObjectOutputStream outputStream
=
new
ObjectOutputStream(
clientSocket.getOutputStream());
ObjectInputStream inputStream
=
new
ObjectInputStream(
clientSocket.getInputStream());
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter file Name : "
);
String name
= sc.nextLine();
Data data =
new
Data(
2
, name,
""
);
outputStream.writeObject(data);
Data receivedData
= (Data)inputStream
.readObject();
File fileToBeDownloaded =
new
File(
"C:\\Users\\Manish Sharma\\Documents\\Java File Server\\Client Downloaded Files\\"
+ receivedData.fileName);
FileOutputStream fileOutputStream
=
new
FileOutputStream(fileToBeDownloaded);
fileOutputStream.write(
(receivedData.fileContent).getBytes());
}
}