本帖最后由 张文豪 于 2013-9-13 12:07 编辑
- /*
- 需求:上传图片。
- */
- /*
- 客户端
- 1.服务端点
- 2.读取客户端已有的图片数据
- 3.通过socket输出流将数据发给服务端、
- 4.读取服务端反馈信息。
- 5.关闭、
- */
- import java.io.*;
- import java.net.*;
- class PicClient
- {
- public static void main(String[] args)throws Exception
- {
- Socket s =new Socket("125.127.XXX.XX",10008);
- FileInputStream fis = new FileInputStream("c:\\1.bmp");
- OutputStream out = s.getOutputStream();
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=fis.read(buf))!=-1)
- {
- out.write(buf,0,len);
- }
- //告诉服务端数据已写完。
- s.shutdownOutput();
- InputStream in =s.getInputStream();
- byte[] bufIn = new byte[1024];
- int num = in.read(bufIn);
- System.out.println(new String(bufIn,0,num));
- fis.close();
- s.close();
-
-
- }
- }
- /*
- 服务端
- */
- class PicServer
- {
- public static void main(String[] args)throws Exception
- {
- ServerSocket ss =new ServerSocket(10008);
- Socket s =ss.accept();
- System.out.println("...connected");
- InputStream in = s.getInputStream();
- FileOutputStream fos =new FileOutputStream("server.bmp");
- byte[]buf = new byte[1024];
- int len = 0;
- while((len=in.read(buf))!=-1);
- {
- fos.write(buf,0,len);
- }
- OutputStream out =s.getOutputStream();
- out.write("上传成功".getBytes());
- fos.close();
- s.close();
- ss.close();
- }
- }
- <div class="quote"> </div>
复制代码
是什么情况导致的呢?
|