复制代码
- 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();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |