黑马程序员技术交流社区

标题: Tcp上传图片问题, [打印本页]

作者: 天方地圆    时间: 2014-3-29 00:25
标题: Tcp上传图片问题,
本帖最后由 天方地圆 于 2014-3-31 10:12 编辑


程序编译通过,但是运行的时候出现这种想象,怎么解决:不吝赐教。。

作者: 黄晓鑫    时间: 2014-3-29 00:26
代码列出来
作者: xietansheng    时间: 2014-3-29 00:36
不知道代码做了什么事,如何赐教
作者: 小周务商    时间: 2014-3-29 08:43
好呆把代码发上来吧大哥。
作者: 天方地圆    时间: 2014-3-29 09:27
  1. /*
  2. 需求:上传图片。


  3. */
  4. import java.io.*;
  5. import java.net.*;

  6. class  PicClient
  7. {
  8.         public static void main(String[] args) throws Exception
  9.         {
  10.                 Socket s = new Socket("49.122.68.11",10004);

  11.                 FileInputStream fis = new FileInputStream("c:\\1.bmp");

  12.                 OutputStream out = s.getOutputStream();

  13.                 byte[] buf = new byte[1024];

  14.                 int len = 0;

  15.                 while ((len=fis.read(buf))!=-1)
  16.                 {
  17.                         out.write(buf,0,len);
  18.                 }
  19.                
  20.                 //告诉服务端数据已写完。
  21.                 s.shutdownOutput();

  22.                 InputStream in = s.getInputStream();

  23.                 byte[] bufIn = new byte[1024];

  24.                 int num = in.read(bufIn);
  25.                 System.out.println(new String(bufIn,0,num));

  26.                 fis.close();
  27.                 s.close();

  28.         }
  29. }

  30. class PicServer
  31. {
  32.         public static void main(String[] args) throws Exception
  33.         {
  34.                 ServerSocket ss = new ServerSocket(10004);

  35.                 Socket s = ss.accept();

  36.                 InputStream in = s.getInputStream();

  37.                 FileOutputStream fos = new FileOutputStream("server.bmp");

  38.                 byte[] buf = new byte[1024];

  39.                 int len = 0;
  40.                 while ((len=in.read(buf))!=-1)
  41.                 {
  42.                         fos.write(buf,0,len);
  43.                 }

  44.                 OutputStream out = s.getOutputStream();

  45.                 out.write("上传成功".getBytes());

  46.                 fos.close();

  47.                 s.close();

  48.                 ss.close();


  49.         }
  50. }
复制代码
代码如上,不好意思啊。。

作者: ╰青青子佩ˊゝ    时间: 2014-3-29 10:01
你试试看是不是没有先打开服务端。
作者: 天方地圆    时间: 2014-3-29 10:14
代码全复制上来了 ,没变,就是运行出错了 ,而且我先开的服务端
作者: xietansheng    时间: 2014-3-31 18:32

我电脑没问题,估计是你的IP填错了,建议你把IP地址改为localhost





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2