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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

打开图片显示:这个程序运行后图片无法打开,因为另一程序正在编辑此图片。
求大虾帮忙找一下错误之处,谢谢!程序如下:
import java.net.*;
import java.io.*;
class PicClient
{
        public static void main(String[] args) throws Exception
        {
                Socket s=new Socket("192.167.12.98",10006);
                File file=new File(args[0]);
                FileInputStream fis=new FileInputStream(file);
                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 PicThread implements Runnable
{
        private Socket s;
        PicThread(Socket s)
        {
                this.s=s;
        }
        public void run()
        {
                int count=1;
                String ip=s.getInetAddress().getHostAddress();
                try
                {
                        System.out.println(ip+"..contect..");
                        InputStream in=s.getInputStream();
                        File file=new File(ip+"("+(count)+")"+"jpg");
                        while(file.exists())
                                file=new File(ip+"("+(count++)+")"+"jpg");
                        FileOutputStream fos=new FileOutputStream(file);
                       
                        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();
                }
                        catch (Exception e)
                        {
                                throw new RuntimeException(ip+"上传失败");
                        }       
        }
}
class PicServer
{
        public static void main(String[] args)throws Exception
                {
                        ServerSocket ss=new ServerSocket(10006);
                       
                        while(true)
                        {       
                                Socket s=ss.accept();
                       
                                new Thread(new PicThread(s)).start();
                       
                        }
                }
}

13 个回复

倒序浏览
  1. File file=new File(ip+"("+(count)+")"+"jpg");
  2.                         while(file.exists())
  3.                                 file=new File(ip+"("+(count++)+")"+"jpg");
复制代码

“.jpg”不是“jpg”,
回复 使用道具 举报
金逗逗 发表于 2015-8-10 18:00
“.jpg”不是“jpg”,

这个没问题的,只是一个打印名称。
回复 使用道具 举报
楼主你的程序怎么回事?检查了好几遍都是上传失败,文件大小为0啊,你的是不是?
回复 使用道具 举报
是呀,就是这个情况搞不懂呀!

回复 使用道具 举报
我给写了一个和你的一模一样的,但是会上传成功,你运行看能不能,奇怪了??

  1. import java.io.*;
  2. import java.net.*;
  3. class PicClient
  4. {
  5.         public static void main(String[] args)throws Exception{
  6.                 Socket s=new Socket("192.168.1.106",5555);
  7.                 File file=new File(args[0]);
  8.                 FileInputStream fis=new FileInputStream(file);
  9.                 OutputStream out=s.getOutputStream();
  10.                 byte[] buf=new byte[1024];
  11.                 int len=0;
  12.                 while ((len=fis.read(buf))!=-1)
  13.                 {
  14.                         out.write(buf,0,len);
  15.                 }
  16.                 s.shutdownOutput();
  17.                 InputStream in=s.getInputStream();
  18.                 byte[] bufIn=new byte[1024];
  19.                 int num=in.read(bufIn);
  20.                 System.out.println(new String(bufIn,0,num));
  21.                 s.close();
  22.                 fis.close();
  23.         }
  24. }
  25. class PicThread implements Runnable
  26. {
  27.         private Socket s;
  28.         PicThread(Socket s){
  29.                 this.s=s;
  30.         }
  31.         public void run(){
  32.                 String ip=s.getInetAddress().getHostAddress();
  33.                 try
  34.                 {
  35.                         System.out.println(ip+"...connect");
  36.                         int count=1;
  37.                         InputStream in=s.getInputStream();
  38.                         File file=new File(ip+"("+(count)+")"+".jpg");
  39.                         while (file.exists())
  40.                         {
  41.                                 file=new File(ip+"("+(count++)+")"+".jpg");
  42.                         }
  43.                         FileOutputStream fos=new FileOutputStream(file);                       
  44.                         byte[] buf=new byte[1024];
  45.                         int len=0;
  46.                         while ((len=in.read(buf))!=-1)
  47.                         {
  48.                                 fos.write(buf,0,len);
  49.                         }
  50.                         OutputStream out=s.getOutputStream();
  51.                         out.write("上传成功".getBytes());
  52.                         s.close();
  53.                         fos.close();
  54.                 }
  55.                 catch (Exception e)
  56.                 {
  57.                         throw new RuntimeException(ip+"连接失败");
  58.                 }
  59.         }

  60. }
  61. class PicServer
  62. {
  63.         public static void main(String[] args)throws Exception{
  64.                 ServerSocket ss=new ServerSocket(5555);
  65.                 while (true)
  66.                 {
  67.                         Socket s=ss.accept();
  68.                         new Thread(new PicThread(s)).start();
  69.                 }
  70.         }
  71. }
复制代码

点评

挺厉害的!  发表于 2015-8-10 22:28
回复 使用道具 举报
楼主解决了吗?怎么解决的?
回复 使用道具 举报
金逗逗 发表于 2015-8-10 19:02
我给写了一个和你的一模一样的,但是会上传成功,你运行看能不能,奇怪了??

...

还是不行,你把我的代码复制到你机器上,帮我试一下,谢谢了
回复 使用道具 举报
金逗逗 发表于 2015-8-10 19:02
我给写了一个和你的一模一样的,但是会上传成功,你运行看能不能,奇怪了??

...

我把你的代码复制就可以运行,哎。。。。
回复 使用道具 举报
蓬头小道士 发表于 2015-8-10 22:26
我把你的代码复制就可以运行,哎。。。。

我就是复制你的代码运行直接抛上传失败的,然后我才写了一个,但是写的和你的一样的啊,怎么回事?你找到原因了没?我都纠结了一个下午了,没找到原因,很奇怪啊
回复 使用道具 举报
金逗逗 发表于 2015-8-10 22:42
我就是复制你的代码运行直接抛上传失败的,然后我才写了一个,但是写的和你的一样的啊,怎么回事?你找到 ...

没有,我也是搞了好久,到现在就写了两个小程序,两个都不行,郁闷的不行!我正在重新写一个,试试。
回复 使用道具 举报
蓬头小道士 发表于 2015-8-10 22:46
没有,我也是搞了好久,到现在就写了两个小程序,两个都不行,郁闷的不行!我正在重新写一个,试试。 ...

你的程序能连上服务端,但是就是抛异常,而且复制的图片是0kb。你找找原因
回复 使用道具 举报
金逗逗 发表于 2015-8-10 23:03
你的程序能连上服务端,但是就是抛异常,而且复制的图片是0kb。你找找原因 ...

是呀,我也很疑惑
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马