黑马程序员技术交流社区
标题:
客户端向服务端上传MP3文件
[打印本页]
作者:
许云龙
时间:
2013-8-18 08:45
标题:
客户端向服务端上传MP3文件
//客户端向服务端上传MP3文件
import java.io.*;
import java.net.*;
class Mp3Client
{
public static void main(String[] args)throws Exception
{
Socket s= new Socket("192.168.1.101",10004);
FileInputStream fis = new FileInputStream("1.mp3");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufin = new byte[1024];
int num = in.read(bufin);
String str = new String(bufin,0,num);
System.out.println(str);
fis.close();
s.close();
}
}
class Mp3Server
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(10004);
Socket s =ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".......");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("2.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2