黑马程序员技术交流社区

标题: 大神帮忙看下哪里出问题了(除了注释问题以外) [打印本页]

作者: adomwon    时间: 2016-3-22 17:48
标题: 大神帮忙看下哪里出问题了(除了注释问题以外)
本人新马,基本学完才来逛论坛的,在做网络编程练习之客户端向服务器上传文件时发现自己写的代码运行起来没问题,本地路径能够判断成功,文件也能上传成功,
但我重复上传同一个文件的时候,却没有像老师的程序一样给我返回提示,大家帮忙看看,谢谢!
代码如下:
public class Test2_Client {

        /**
         * * 客户端向服务器上传文件
         *
         * @throws IOException
         * @throws UnknownHostException
         */
        public static void main(String[] args) throws UnknownHostException,
                        IOException {
                File file = getFile();

                Socket socket = new Socket("127.0.0.1", 11111);
                BufferedReader br = new BufferedReader(new InputStreamReader(
                                socket.getInputStream()));
                PrintStream ps = new PrintStream(socket.getOutputStream());
                ps.println(file.getName());
                String result = br.readLine();
                if ("存在".equals(result)) {
                        System.out.println("服务器上已存在该文件,请勿重复上传");
                        socket.close();
                        return;
                }

                FileInputStream fis = new FileInputStream(file);
                int len;
                byte[] arr = new byte[8192];
                while ((len = fis.read(arr)) != -1) {
                        ps.write(arr, 0, len);
                }

                fis.close();
                socket.close();
        }

        public static File getFile() {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入将要上传的文件路径:");
                while (true) {
                        String line = sc.nextLine();
                        File file = new File(line);
                        if (!file.exists()) {
                                System.out.println("路径不存在,请重新输入");
                        } else if (file.isDirectory()) {
                                System.out.println("您输入的是文件夹路径,请重新输入");
                        } else {
                                return file;
                        }
                }
        }

}
---------------------------------------------------------------------------------------

public class Test2_Server {

        /**
         * * 客户端向服务器上传文件
         *
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                ServerSocket server = new ServerSocket(11111);
                System.out.println("服务器启动,绑定11111端口");

                while (true) {
                        final Socket socket = server.accept();
                        new Thread() {
                                public void run() {
                                        try {
                                                InputStream is = socket.getInputStream();
                                                BufferedReader br = new BufferedReader(
                                                                new InputStreamReader(is));
                                                PrintStream ps = new PrintStream(
                                                                socket.getOutputStream());
                                                String fileName = br.readLine();

                                                File dir = new File("update");
                                                dir.mkdir();
                                                File file = new File(dir, fileName);
                                                if (file.exists()) {
                                                        ps.println("已存在");
                                                        socket.close();
                                                        return;
                                                } else {
                                                        ps.println("不存在");
                                                }

                                                FileOutputStream fos = new FileOutputStream(file);
                                                int len;
                                                byte[] arr = new byte[8192];
                                                while ((len = is.read(arr)) != -1) {
                                                        fos.write(arr, 0, len);
                                                }
                                                fos.close();
                                                socket.close();
                                        } catch (IOException e) {

                                                e.printStackTrace();
                                        }
                                }
                        }.start();

                }

        }
}



作者: wxf468907066    时间: 2016-3-22 19:40
大兄弟,你服务器里面给客户端返回的是"已存在",客户端里面判断的是"存在",细心一点,把币给我谢谢0.0

作者: adomwon    时间: 2016-3-23 08:19
我真的太粗心了,感谢解答,不过给黑马币怎么操作啊
作者: 278008379    时间: 2016-3-23 12:27
已经很厉害了
作者: adomwon    时间: 2016-3-23 13:18
差得远了,大家共勉
作者: sky0202    时间: 2016-3-24 12:42
服务器里面给客户端返回的是"已存在",客户端里面判断的是"存在",建议编程经常Debug ,很有帮助,另外我需要黑马币
作者: wxf468907066    时间: 2016-3-28 19:29
adomwon 发表于 2016-3-23 08:19
我真的太粗心了,感谢解答,不过给黑马币怎么操作啊

大兄弟,好好研究研究怎么给黑马币吧,整过来一行行看的0.0

作者: adomwon    时间: 2016-3-29 11:34
管理员没划走,我也不能操作,不知道怎么弄的




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