62,614
社区成员
发帖
与我相关
我的任务
分享
public class Method
{
// 发送方法
public void sender(Socket _socket, String _filePath, Boolean _sendFile)
throws IOException
{
// 如果发送的是文件
if (_sendFile == true)
{
File file = new File(_filePath);
// 读文件
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 写文件到输出流
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(true);
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
}
if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dis.close();
dos.flush();
dos.close();
System.out.println("file send done!!!\r\n\r\n\r\n");
}
// 如果发送的是请求
else if (_sendFile == false)
{
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(false);
dos.flush();
dos.close();
}
}
// 接收方法
public void receiver(Socket _socket, String _filePath) throws IOException
{
// 接收输入流
DataInputStream dis = new DataInputStream(new BufferedInputStream(
_socket.getInputStream()));
Boolean getFile = dis.readBoolean();
// 如果收到的是文件,保存
if (getFile == true)
{
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(_filePath))));
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
}
if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dos.flush();
dos.close();
System.out.println("file receive success!!!\r\n\r\n\r\n");
}
// 如果收到的是请求,发送所需文件
else if(getFile == false)
{
this.sender(_socket, _filePath, true);
}
}
public void serverManager(Socket _socket, String _filePath)
throws IOException
{
this.receiver(_socket, _filePath);
}
}
public class Server
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
String filePath = "E:\\TEST\\Server\\contact.txt";
ServerSocket ss = new ServerSocket(30000);
System.out.println("waiting client ...");
while(true)
{
Socket server = ss.accept();
System.out.println("client has been linked!!!");
Method method = new Method();
method.serverManager(server, filePath);
}
}
}
public class Client
{
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException
{
// TODO Auto-generated method stub
String IP = "192.168.1.102";
int PORT = 30000;
Socket client = new Socket(IP, PORT);
String filePath = "E:\\TEST\\Client\\contact.txt";
Method method = new Method();
int readInt = 65535;
while (readInt != 0)
{
System.out.println("would do you want to do ?");
System.out.println("===============================");
System.out.println("for upload, input 1");
System.out.println("for download , input 2");
System.out.println("for quit, input 0");
System.out.println("===============================");
System.out.println("plz make a choice:");
Scanner in = new Scanner(System.in);
readInt = in.nextInt();
if (readInt == 1)
{
//send file
method.sender(client, filePath, true);
} else if (readInt == 2)
{
//send file request
method.sender(client, filePath, false);
//get file
DataInputStream dis = new DataInputStream(new BufferedInputStream(
client.getInputStream()));
do
{
method.receiver(client, filePath);
}while(dis == null);
} else if(readInt == 0)
{
break;
}else
{
System.out.println("wrong choice!!!\r\n\r\n\r\n");
}
}
}
}
public class Method
{
// 发送方法
public void sender(Socket _socket, String _filePath, Boolean _sendFile)
throws IOException
{
// 如果发送的是文件
if (_sendFile == true)
{
File file = new File(_filePath);
// 读文件
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 写文件到输出流
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(true);
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
}
if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dis.close();
dos.flush();
dos.close();
System.out.println("file send done!!!\r\n\r\n\r\n");
}
// 如果发送的是请求
else if (_sendFile == false)
{
DataOutputStream dos = new DataOutputStream(
_socket.getOutputStream());
dos.writeBoolean(false);
dos.flush();
dos.close();
}
}
// 接收方法
public void receiver(Socket _socket, String _filePath) throws IOException
{
// 接收输入流
DataInputStream dis = new DataInputStream(new BufferedInputStream(
_socket.getInputStream()));
Boolean getFile = dis.readBoolean();
// 如果收到的是文件,保存
if (getFile == true)
{
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(_filePath))));
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
while (true)
{
int read = 0;
if (dis != null)
{
read = dis.read(buf);
}
if (read == -1)
{
break;
}
dos.write(buf, 0, read);
}
dos.flush();
dos.close();
System.out.println("file receive success!!!\r\n\r\n\r\n");
}
// 如果收到的是请求,发送所需文件
else if(getFile == false)
{
this.sender(_socket, _filePath, true);
}
}