InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(filePath);
int bytesRead = 0;
byte[] buffer = new byte[8196];
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();

解决方案 »

  1.   

    我现在已经可以传文件了,用StreamIoHandler传。可是传大文件,客户端传到一半就会自动断掉,谁知道是为什么。
      

  2.   

    用ProtocolCodecFilter+StreamIoHandler传输文件时,服务器端已经发送出去了,可是客户端接收是CumulativeProtocolDecoder总是出错。
      

  3.   

    mina框架中那个过滤器不要为什么就不能进行通讯
    // 创建acceptor服务器对象
    IoAcceptor acceptor = new SocketAcceptor();
    IoAcceptorConfig config = new SocketAcceptorConfig();
    DefaultIoFilterChainBuilder chain = config.getFilterChain();
    // 使用字符串编码
    chain.addLast("codec", new ProtocolCodecFilter(
    new TextLineCodecFactory()));
    为什么不写这句代码就不能进行通讯呢?
    // 设置acceptor处理器类,启动服务器.
    // IoHandler是IoSession的监听器,也就是说当有网络事件发生时会通知IoHandler
    acceptor.bind(new InetSocketAddress(port),
    (IoHandler) new SheetServerHandler(), config);我在使用的时候需要在客户端把文件读取出来放在byte数组中然后传到客户端,可是使用了这个编码后在客户端将得到的数据转成byte数组然后通过输入流对象写入文件。操作完毕后用鼠标双击文件会提示文件已破损的错误。。大家帮帮忙 。 为了 这个问题我郁闷了不少的时间,谢谢