| 发表于:2008-01-20 18:11:54 楼主 |
谁能帮我看看这个代码,我开始是向客户端发送file的大小,然后发送file流,在客户端先根据file的大小来接收固定的file流啊,可是第一文件可以接收到,但是第二个,第三个....都为0字节哦,请教哪位高手能帮帮小弟哦!! 代码如下: 服务器: package com.sunyard.hefei; import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.objectoutputstream; import java.io.outputstream; import java.net.serversocket; import java.net.socket; import java.util.list; 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"); } } } 客户端: package com.sunyard.hefei; import java.io.*; import java.net.*; import java.util.collection; import java.util.list; 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(); } } } |
|
|
|
|