最近在学网络编程的时候 发现 用客户端 给服务器端 传 的MP3文件 居然不能播放。。而且 原本是 40.9M的 传完后后变为了 43M 。。呵呵是奇怪 请各位大虾 给点指点!
服务器端
import java.io.*;
import java.net.*;
public class Sendserver {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(9999);
while (true){
Socket d= s.accept();
System.out.println("客户端连接成功");
DataInputStream dis = new DataInputStream(d.getInputStream());
int b = 0;
BufferedWriter bw = new BufferedWriter(new FileWriter("G:\\Track 01.mp3"));
while((b=dis.read())!=-1){
bw.write(b);
}
bw.flush();
bw.close();
dis.close();
System.out.println("传送完毕");
}
}
客户端
import java.io.*;
import java.net.*;
public class sendclient {
public static void main(String[] args) throws UnknownHostException, IOException{
Socket f= new Socket("127.0.0.1",9999);
try{
FileInputStream cc= new FileInputStream("D:\\Track 01.mp3");
BufferedInputStream bis = new BufferedInputStream(cc);
OutputStream out=f.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
BufferedOutputStream xie = new BufferedOutputStream(dos);
int b=0;
while((b=bis.read())!=-1){
xie.write(b);
}
xie.flush();
out.close();
bis.close();
}catch (IOException e){ }
}
}