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

a8336675

中级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© a8336675 中级黑马   /  2015-9-26 11:29  /  849 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 a8336675 于 2015-9-26 11:30 编辑
  1. 从客户端将图片转换二进制数据,并发送到服务器端时,数据长度丢失(比原来小一些),但是图片能正常显示出来。
  2. 请问这是正常的吗?还是代码有问题?
  3. 代码如下:
复制代码

class Client implements Runnable {
        @Override
        public void run() {
                try {
                        // 1, 使用JFileChooser打开系统文件选择框,选择一个文件,并获取该文件对象
                        JFileChooser chooser = new JFileChooser();
                        int returnVal = chooser.showOpenDialog(null);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                                File file = chooser.getSelectedFile();
                                String checkResult = null;
                                if ((checkResult = checkFile(file)) != null) {
                                        System.out.println(checkResult);
                                        return;
                                }
                               
                                // 2, 确认是否上传
                                System.out.println("File Name: " + file.getName());
                                System.out.println("File Path: " + file.getAbsolutePath());
                                System.out.println("File Size: " + file.length() + " B");
                                System.out.println("确定是否上传该文件?(yes/no)");
                                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                                if (reader.readLine().trim().equalsIgnoreCase("yes")) {
                                        reader.close();
                                       
                                        // 3, 建立Socket对象,连接服务器
                                        Socket client = new Socket("192.168.0.102", 10004);
                                        BufferedOutputStream output = new BufferedOutputStream(client.getOutputStream());

                                        // 4, 从文件中读取数据,并写入Socket输出流中,发送到服务器
                                        FileInputStream fis = new FileInputStream(file);
                                        int length = 0;
                                        byte[] buf = new byte[1024];
                                        while ((length = fis.read(buf)) != -1) {
                                                output.write(buf, 0, length);
                                        }
                                        client.shutdownOutput();
                                        fis.close();
                                       
                                        // 5, 获得服务器返回信息
                                        byte[] returnStr = new byte[1024];
                                        length = client.getInputStream().read(returnStr);
                                        System.out.println(new String(returnStr, 0, length));
                                       
                                        client.close();
                                } else {
                                        reader.close();
                                        System.out.println("上传操作取消.");
                                }
                        }
                } catch (Exception e) {
                        MyLog.printLog(e);
                }
        }
       
        /**
         * 检查文件规范性
         * @param file
         * @return
         */
        public String checkFile(File file) {
                String fileName = file.getName();
                if (!fileName.endsWith(".jpg")) {
                        return "仅支持jpg图片上传.";
                }
                if (file.length() > 1024 * 1024 * 10) {
                        return "仅支持10M以下的图片上传.";
                }
                return null;
        }
}

/**
* 用于上传图片的服务器端
*/
class Server implements Runnable {
        @Override
        public void run() {
                try {
                        // 1, 建立ServerSocket对象,监听端口
                        ServerSocket server = new ServerSocket(10004);
                        while (true) {
                                // 2, 等待客户端连接
                                Socket client = server.accept();
                                new Thread(new uploadPicThread(client)).start();
                        }
                } catch (Exception e) {
                        MyLog.printLog(e);
                }
        }
}

/**
* 用于接收图片数据,并封装成文件的线程类
*/
class uploadPicThread implements Runnable {
        private Socket client;
        public uploadPicThread(Socket client) {
                this.client = client;
        }
        @Override
        public void run() {
                try {
                        String ip = client.getInetAddress().getHostAddress();
                        int port = client.getPort();
                        System.out.println("客户端(" + ip + ":" + port + ")连接成功.");
                       
                        System.out.println("文件开始上传...");
                       
                        // 3, 接受数据,封装成文件
                        FileOutputStream fos = new FileOutputStream("E:/ithima/dest/day23/" + ip + "-" + System.currentTimeMillis() / 1000 + ".jpg");
                        int length = 0;
                        int count = 0;
                        byte[] buf = new byte[1024];
                        while ((length = client.getInputStream().read(buf)) != -1) {
                                fos.write(buf, 0, length);
                                count += length;
                        }
                        System.out.println("上传完毕.文件大小: " + count + " B");
                       
                        byte[] returnStr = "上传完毕.".getBytes();
                        client.getOutputStream().write(returnStr);
                       
                        System.out.println("客户端(" + ip + ":" + port + ")断开连接.");
                        System.out.println();
                        fos.close();
                        client.close();
                } catch (Exception e) {
                        MyLog.printLog(e);
                }
        }
}

3 个回复

倒序浏览
udp本身就是不安全链接,有可能会出现数据包丢失的情况。一般用于视频聊天,网络电话等对数据完整性要求不高的应用中 ,像你这种file传输还是建立tcp较好
回复 使用道具 举报
a8336675 来自手机 中级黑马 2015-9-26 17:27:32
藤椅
大哥,我这用的就是tcp协议啊
回复 使用道具 举报
来学习!!!!!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马