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();
}
}}
====================================================
传输老是出错,本人刚刚入门学习,望各位大侠多多指教,谢谢了
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();
}
}}
====================================================
传输老是出错,本人刚刚入门学习,望各位大侠多多指教,谢谢了
解决方案 »
- 考思维,拼数组问题
- 请教关于执行时间的问题
- java执行cmd命令的基础问题
- 怎么用java写过滤 html标记的方法?
- Struts1中的ActionForm 对象为空,并且抛出 No origin bean specified 没分了没分了
- 如何在gui程序中运行用c语言写的控制台程序?
- 一个超菜的问题:怎样在JCreator中使用jar文件?
- 有没有人对邮件服务器有兴趣,手头有一个开源的,实现了POP3/SMTP协议的最小功能集,想在此基础上实现一些其他的协议,如SMTP扩展(命令
- 请教一个内存回收问题
- 哪儿可以找到java类库的帮助文件。类似于msdn那种
- HttpURLConnection的getResponseCode无限延时
- 自己做的一道题,有关重写的,为什么输出结果不对头?
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写出去就行了
客户端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();
} }}
--这么写---你传的是mp3吗?2:流结束标记;参照下4楼的吧
用bufferdinputstream接收、
不要用字符流、