黑马程序员技术交流社区

标题: args.length!=1这是什么意思?不懂 [打印本页]

作者: 靓仔    时间: 2013-11-11 18:00
标题: args.length!=1这是什么意思?不懂
本帖最后由 FFF 于 2013-11-14 21:02 编辑

import java.io.*;
import java.net.*;
class picClient
{
        public static void main(String[] args) throws Exception

        {
                if(args.length!=1)
                {
                        System.out.println("请选择一个jpg格式的图片");
                        return ;
                }

                File file = new File(args[0]);

                if(!(file.exists() && file.isFile()))
                {
                        System.out.println("该文件有问题,要么不存在,要么不是文件");
                        return ;
                }

                if(!file.getName().endsWith(".jpg"))
                {
                        System.out.println("图片格式错误,请重新选择");
                }

                if(file.length()>1024*1024*4)
                {
                        System.out.println("文件过大");
                        return ;
                }

                Socket s = new Socket("192.168.1.100",10007);

                BufferedInputStream bur = new BufferedInputStream(new FileInputStream(file));

                OutputStream out = s.getOutputStream();

                byte[] buf = new byte[1024];

                int len = 0;

                while((len=bur.read(buf))!=-1)
                {
                        out.write(buf,0,len);
                }
               
                //告诉服务端已经写完
                s.shutdownOutput();

                InputStream in = s.getInputStream();

                byte[] buIn=new byte[1024];

                int num = in.read(buIn);

                System.out.println(new String(buf,0,num));


                bur.close();

                s.close();
        }
}


class threadPic implements Runnable
{
        private Socket s;
        threadPic(Socket s)
        {
                this.s = s;
        }
        public void run()
        {
                try
                {
                        int count = 1;

                        String ip = s.getInetAddress().getHostAddress();

                        System.out.println(ip+"....conection");

                        InputStream in = s.getInputStream();


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

                        while(file.exists())
                                file = new File(ip+"("+(count++)+")"+".jpg");


                        FileOutputStream out = new FileOutputStream(file);

                        byte[] buIn = new byte[1024];

                        int num = 0;
                        
                        while((num=in.read(buIn))!=-1)
                        {
                                out.write(buIn,0,num);
                        }
                        OutputStream os = s.getOutputStream();

                        os.write("上传成功".getBytes());

                        out.close();

                        s.close();
                }
                catch (Exception e)
                {
                        throw new RuntimeException("上传失败");
                }
        }
}
class picServer
{
        public static void main(String[] args) throws Exception
        {
                ServerSocket ss = new ServerSocket(10007);
                while(true)
                {
                        Socket s = ss.accept();
                        new Thread(new threadPic(s)).start();
                }

               
        }
}
作者: 零下五度的水    时间: 2013-11-11 18:54
主函数:public static void main(String[] args)
参数是 String[] args 吧,
如果这个参数的长度不为1,说明要么你没传参数,要么你传了不只一个参数
P.S. 主函数传参是直接在命令行 javac xxx.java -(主函数参数)
作者: ciowok    时间: 2013-11-11 19:02
"!="符号: 不等于
作者: 黄炳期    时间: 2013-11-11 22:55
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html




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