求一个:java socket 采用ObjectInputStream序列化收发文件例子
服务器端:
class javaserver extends Thread {
ServerSocket server;
public javaserver() {
try {
server = new ServerSocket(6000);
} catch (IOException e) {
System.out.println("Cannot create Server");
System.exit(0);
}
System.out.println("Now socket server will Start");
this.start();
}
public void run() {
try {
while (true) {
Socket client = server.accept();
// System.out.println("IP: "+client.getInetAddress());//get
// client's ip address;
service ss = new service(client);
}
} catch (IOException e) {
System.out.println("cannot provide service !");
System.exit(1);
}
}
public static void main(String args[]) {
new javaserver();
/*
* KeyInput = new DataInputStream(System.in); try { data =
* KeyInput.readLine(); } catch (IOException e) { return; } if
* (data.equals("quit")) System.exit(1);
*/
}
}
class service extends Thread {
DataInputStream InputS;
AddImages addimag ;
DataInputStream dis = null;
DataOutputStream dos = null;
DataOutputStream OutputS;
Socket Client;
public service(Socket ClientSocket) {
Client = ClientSocket;
try {
InputS = new DataInputStream(Client.getInputStream());
} catch (IOException e) {
System.out.println("Cannot Connect with Client !");
return;
}
this.start();
}
public void run() {
try {
ObjectOutputStream out = new ObjectOutputStream(Client
.getOutputStream());
OutputStream ops = Client.getOutputStream();
List images = message.getImageFiles();
if (!images.isEmpty()) {
out.writeObject(images);
out.flush();
File image = null;
for (int j = 0; j < images.size(); j++) {
image = (File)images.get(j); // 文件已经根据路径New File了
BufferedInputStream imgstream = new BufferedInputStream(
new FileInputStream(image));
dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(image)));
dos = new DataOutputStream(
new BufferedOutputStream(ops));
int imglength = Integer.parseInt(String
.valueOf(image.length()));
byte[] buf = new byte[imglength];
while ((dis.read(buf)) != -1) {
dos.write(buf, 0, buf.length);
}
}
dos.close();
dos.flush();
}
} catch (Exception e) {
System.out.println("Read Data error");
e.printStackTrace();
}
try {
Client.close();
} catch (IOException e) {
System.out.println("Cannot close socket");
}
}
}
客户端:
class javaclient {
private static Socket Client;
public static void main(String args[]) {
try {
InetAddress in = InetAddress.getLocalHost();
System.out.println(in.getHostAddress());// get host ip address;
Client = new Socket(in.getHostAddress(), 6000);
} catch (IOException e) {
System.out.println("Cannot Connect with Server");
e.printStackTrace();
}
try {
ObjectInputStream in = null;
if (Client.getInputStream() != null) {
in = new ObjectInputStream(Client.getInputStream());
} else {
return;
}
DataOutputStream dos = null;
DataInputStream dis = null;
Collection messages = (Collection) in.readObject(); // 接收到的文字信息集合
List info = (List) messages;
System.out.println("Size: " + info.size());
if (!messages.isEmpty()) {
for (int i = 0; i < info.size(); i++) {
File imageFile = (File) info.get(i);
String imagename = imageFile.getName(); // 图片名字
System.out.println("Length: " + imageFile.length());
int imglength = Integer.parseInt(String. // 图片长度
valueOf(imageFile.length()));
InputStream ips =Client.getInputStream();
System.out.println("available: "+ips.available());
File image = new File("E:\\" + imagename);
File renameimg = new File("E:\\" + i + imagename);
BufferedInputStream br = new BufferedInputStream(
ips);
dis = new DataInputStream(br);
// 以DataInputStream来包装字节缓冲输入流
if (!image.exists()) {
// 以DataOutputStream来包装字节缓冲输出流
dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(image))); // 将图片保存到文件夹
} else {
dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(renameimg)));
}
byte[] bufr = new byte[imglength];
// 网络传输都是以字节的方式传递的
while ((dis.read(bufr)) != -1) {
// 一边读,一边写
dos.write(bufr, 0, bufr.length);
}
System.out.println("客户端端接收完毕");
}
} else {
}
} catch (Exception e) {
System.out.println("IOException Happened");
e.printStackTrace();
}
try {
System.out.println("Now will end this program");
Client.close();
} catch (IOException e) {
System.out.println("system cannot close socket");
e.printStackTrace();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。