- import java.io.*;
- import java.net.*;
- class ReceServer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(4005);
- Socket s = ss.accept();
-
- System.out.println(s.getInetAddress().getHostAddress()+"...conection...");
- BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\Music\\邓紫棋-泡沫.MP3"));
- byte[] buf = new byte[1024];
- int len = 0;
- while ((len=bis.read(buf))!=-1)
- {
- bos.write(buf,0,len);
- System.out.println("正在接收中");
- }
- BufferedWriter bufout = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- bufout.write("传输完成");
- bos.close();
- s.close();
- ss.close();
- }
- }
复制代码
/*
发送MP3文件到服务端
*/
import java.io.*;
import java.net.*;
class SendClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.27.190",4005);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Music\\华语歌\\G.E.M. 邓紫棋\\泡沫 - G.E.M. 邓紫棋.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
while ((len=bis.read(buf))!=-1)
{
bos.write(buf,0,len);
System.out.println("正在发送中");
}
BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = bufin.readLine();
System.out.println(line);
bis.close();
s.close();
}
}
一个是客户端,一个是服务端,我文件能发送完成,但是我不知道怎么判断结束
|
|