flylee 2014-03-19 09:35 采纳率: 0%
浏览 2189

java socket 传文件文件接收后不能打开

java socket传文件,客户端将文件读取到byte[]中,然后用Map对相关内容进行包装之后输出到服务器端,但服务器端读取文件内容的循环除第一次读取的Map是客户端写入过来的内容外,从第二次循环开始Map的内容都跟第一次一样的,请问怎么回事?
服务器端代码:
`public void run() {
ObjectInputStream is = null;
OutputStream os = null;
byte cache[] = new byte[1024];
Map map = null;
int len = 0;
String fileName = null;
int i = 0;
try {
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
map = (Map)is.readObject();
fileName = "d:/" +(String)map.get("fn");
os = new BufferedOutputStream(new FileOutputStream(fileName));

        //文件内容接收
        while(true){
            //map在第二次通过readObject方法读出后内容与第一次读取的值一样
            map = (Map)is.readObject();
            if(null==map){
                continue;
            }
            if(((Boolean)map.get("fe"))){
                break;
            }
            cache = (byte[])map.get("c");
            if(null==cache || 0==cache.length){
                continue;
            }

            len = (Integer)map.get("l");
            os.write(cache,0,len);

            map = null;
            cache = null;
        }
        os.flush();

        System.out.println("--->over");
    } catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try{
            if(null!=is){
                is.close();
                is = null;
            }
            if(null!=os){
                os.close();
                os = null;
            }
            if(null!=socket){
                socket.close();
                socket = null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

客户端代码:
`public void actionPerformed(ActionEvent e) {
Map map = null;
Socket socket = null;
ObjectOutputStream oos = null;
InputStream is = null;
byte cache[] = new byte[1024];
int len = 0;
try {
socket = new Socket("192.168.88.154",8888);
socket.setSoTimeout(10*1000);
socket.setKeepAlive(true);

                File file = new File(jTextField.getText());
                is = new BufferedInputStream(new FileInputStream(file));
                oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));

                //文件名称
                map = new HashMap();
                map.put("fn",file.getName());
                map.put("fl",file.length());
                map.put("fe",false);
                oos.writeObject(map);
                oos.flush();

                //文件内容
                while(true){
                    map.clear();
                    if((len=is.read(cache))<=0){
                        break;
                    }

                    map.put("c",cache);
                    map.put("l", len);
                    map.put("fe", false);

                    oos.writeObject(map);
                }
                oos.flush();

                //文件结束
                map = new HashMap();
                map.put("fe",true);
                oos.writeObject(map);

                oos.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try{
                    if(null!=is){
                        is.close();
                        is = null;
                    }
                    if(null!=oos){
                        oos.close();
                        oos = null;
                    }
                    if(null!=socket){
                        socket.close();
                        socket = null;
                    }
                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
        }
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-21 03:59
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?