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
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条