A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴林飞 中级黑马   /  2013-4-13 14:41  /  1864 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 吴林飞 于 2013-4-14 14:23 编辑
  1. import java.io.*;
  2. import java.net.*;
  3. /*
  4. 需求:实现多个客户端的并发上传图片
  5. */
  6. class ClientThread implements Runnable
  7. {
  8.         private Socket s;
  9.         ClientThread(Socket s)
  10.         {
  11.                 this.s = s;
  12.         }
  13.         public void run()
  14.         {
  15.                 int count = 1;
  16.                 try
  17.                 {
  18.                         InputStream in = s.getInputStream();

  19.                         File file = new File(count+".jpg");

  20.                          while(file.exists())
  21.                         {
  22.                                 file = new File((++count)+".jpg");
  23.                         }
  24.                         
  25.                         FileOutputStream fos = new FileOutputStream(file);

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

  27.                         int len = 0;

  28.                         while((len=in.read(buf))!=-1)
  29.                         {
  30.                                 fos.write(buf,0,len);
  31.                         }
  32.                         //s.shutdownInput();

  33.                         OutputStream out = s.getOutputStream();

  34.                         out.write("恭喜您,图片上传成功".getBytes());

  35.                         s.close();        
  36.                 }
  37.                 catch(Exception e)
  38.                 {
  39.                         throw new RuntimeException("图片上传失败");
  40.                 }
  41.         }
  42. }
  43. //服务端
  44. class TcpServer6
  45. {
  46.         public static void main(String[] args)throws Exception
  47.         {
  48.                 ServerSocket ss = new ServerSocket(9999);

  49.                 while(true)
  50.                 {
  51.                         Socket s = ss.accept();
  52.                         new Thread(new ClientThread(s)).start();
  53.                 }
  54.         }
  55. }
  56. //客户端
  57. class TcpClient6
  58. {
  59.         public static void main(String[] args) throws Exception
  60.         {
  61.                 Socket s = new Socket("127.0.0.1",9999);

  62.                 FileInputStream fis = new FileInputStream("d:\\1.jpg");

  63.                 OutputStream out = s.getOutputStream();

  64.                 byte[] bufin = new byte[1024];

  65.                 int lenin = 0;

  66.                 while((lenin=fis.read(bufin))!=-1)
  67.                 {
  68.                         out.write(bufin,0,lenin);
  69.                 }
  70.                 s.shutdownOutput();

  71.                 InputStream in = s.getInputStream();

  72.                 byte[] bufout = new byte[1024];

  73.                 int lenout = in.read(bufout);

  74.                 String info = new String(bufout,0,lenout);

  75.                 System.out.println(info);
  76.                
  77.                 s.close();
  78.         }
  79. }
复制代码
问题是这样的,我把服务端开启后,用客户端给服务端发了几个图片,但是我发现我发到服务端的图片怎么都不能显示?我感觉我代码没写错,而且用的都是字节流,应该不存在刷新的问题吧,怎么发生这种问题,连续几个图片都是如此。




未命名.JPG (1.56 KB, 下载次数: 16)

未命名.JPG

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

7 个回复

倒序浏览
本帖最后由 梁航斌 于 2013-4-13 14:55 编辑
  1. import java.io.*;
  2. import java.net.*;
  3. /*
  4. 需求:实现多个客户端的并发上传图片
  5. */
  6. class ClientThread implements Runnable
  7. {
  8.         private Socket s;
  9.         ClientThread(Socket s)
  10.         {
  11.                 this.s = s;
  12.         }
  13.         public void run()
  14.         {
  15.                 int count = 1;
  16.                 try
  17.                 {
  18.                         InputStream in = s.getInputStream();

  19.                         File file = new File(count+".jpg");

  20.                          while(file.exists())
  21.                         {
  22.                                 file = new File((++count)+".jpg");
  23.                         }
  24.                         
  25.                         FileOutputStream fos = new FileOutputStream(file);

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

  27.                         int len = 0;

  28.                         while((len=in.read(buf))!=-1)
  29.                         {
  30.                                 fos.write(buf,0,len);
  31.                                 //********************************
  32.                                fos.flush();//兄弟,请记得刷新
  33.                                //********************************
  34.                         }
  35.                         //s.shutdownInput();

  36.                         OutputStream out = s.getOutputStream();

  37.                         out.write("恭喜您,图片上传成功".getBytes());

  38.                         s.close();        
  39.                 }
  40.                 catch(Exception e)
  41.                 {
  42.                         throw new RuntimeException("图片上传失败");
  43.                 }
  44.         }
  45. }
复制代码
你注意打“*”号的地方。可能是没刷新,文件不全所以打不开

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
梁航斌 发表于 2013-4-13 14:54
你注意打“*”号的地方。可能是没刷新,文件不全所以打不开

FileOutputStream是字节流OK?字节流有刷新操作么?
回复 使用道具 举报
本帖最后由 梁航斌 于 2013-4-13 18:00 编辑
吴林飞 发表于 2013-4-13 17:41
FileOutputStream是字节流OK?字节流有刷新操作么?

看成这个了
回复 使用道具 举报
梁航斌 发表于 2013-4-13 17:56
看成这个了

看了下API,有flush()但是是从OutputStream继承的,
flush()方法加进去还是没用。
回复 使用道具 举报
你的程序没有阻塞吧?阻塞的话没有释放资源,图片是打不开的,你ctrl+c停止运行,然后看看图片能打开吗
回复 使用道具 举报
程序运行完,客户端就停止了,只有服务端还开着,我试过了只有把服务端ctrl+c图片才能刷新变成可浏览状态。
回复 使用道具 举报
黄玉昆 黑马帝 2013-4-14 14:16:38
8#
如果问题未解决,请继续追问,如果没有问题了,请将帖子分类 改为“已解决”,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马