Socket端:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;public class Socketdemo {
public static void main(String[] args) {
Socket sk = null;
try {
sk = new Socket("127.0.0.1", 8088);
PrintWriter output = new PrintWriter(sk.getOutputStream());
BufferedInputStream input = new BufferedInputStream(sk
.getInputStream());
output.println("are you the one.mp3");
output.flush();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("f:/copysong.mp3"));
byte[] bb = new byte[5120];
int b;
while ((b = input.read(bb)) > 0) {
bos.write(bb, 0, b);
bos.flush();
}
bos.close();
input.close();
output.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
ServerSocket端:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class Serversocketdemo {
public static void main(String[] args) {
System.out.println("服务器启动....");
ServerSocket ssk = null;
try {
ssk = new ServerSocket(8088);
while (true) {
Socket sk = ssk.accept();
PrintWriter output = new PrintWriter(sk.getOutputStream());
BufferedReader input = new BufferedReader(
new InputStreamReader(sk.getInputStream()));
String str = input.readLine();
System.out.println(str);
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("e:/" + str));
byte[] buf = new byte[5120];
int a;
while ((a = bis.read(buf)) > 0) {
String send = new String(buf);
output.println(send);
output.flush();
} bis.close();
input.close();
output.close();
System.out.println("e:/" + str);
}
} catch (IOException e) {
e.printStackTrace();
}
}}
====================================================
传输老是出错,本人刚刚入门学习,望各位大侠多多指教,谢谢了

解决方案 »

  1.   

    while ((a = bis.read(buf)) > 0) {
    String send = new String(buf);
    output.println(send);
    output.flush();
    }
    to
    while ((a = bis.read(buf,0,buf.length)) > 0) {
    String send = new String(buf,0,buf.length);
    output.println(send);
    output.flush();
    }
    另外 他本身就是Byte,干嘛还特意转成字符串,直接用outputStream写出去就行了
      

  2.   

    我自己写的一个,还可以加一些聊天请求功能
    客户端import java.io.*;
    import java.net.*;public class SocketClient_mp3 {
    public static void main(String[] args) {
    Socket s = null;
    try {
    s = new Socket("127.0.0.1", 8866);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } try {
    BufferedOutputStream bos = new BufferedOutputStream(
    s.getOutputStream());
    BufferedInputStream bis = new BufferedInputStream(
    s.getInputStream());
    FileInputStream fis = new FileInputStream(
    "C:/Users/yuhaibin/Desktop/fukua.mp3");
    int data;
    while ((data = fis.read()) != -1) {
    bos.write(data);
    } bos.close();
    fis.close();
    s.close(); } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } }
    }
    服务器端
    import java.io.*;
    import java.net.*;public class SocketServer_mp3 {
    public static void main(String[] args) {
    System.out.println("服务器正在启动...");
    ServerSocket ss = null;
    try {
    ss = new ServerSocket(8866);
    while (true) {
    Socket s = ss.accept();
    BufferedOutputStream bos = new BufferedOutputStream(
    s.getOutputStream());
    BufferedInputStream bis = new BufferedInputStream(
    s.getInputStream()); FileOutputStream fos = new FileOutputStream("F:/java/fukua.mp3");
    int data;
    while ((data = bis.read()) != -1) {
    fos.write(data);
    }
    bis.close();
    fos.close();
    if (s == null) {
    ss.close();
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    } }}
      

  3.   

    1:output.println("are you the one.mp3"); 
    --这么写---你传的是mp3吗?2:流结束标记;参照下4楼的吧  
      

  4.   

    用bufferdoutputstream发送
    用bufferdinputstream接收、
    不要用字符流、