Java 如何通过读取块和写入netty中的通道来写入文件数据?

Java 如何通过读取块和写入netty中的通道来写入文件数据?,java,file-io,netty,heap-memory,Java,File Io,Netty,Heap Memory,我使用了netty 3.6,我想通过任何区域将文件数据写入通道,也可以通过加密我在通道上使用FileRegion写入文件数据,没有任何问题,工作正常,不吃我的RAM,但我想在写入通道之前读取encypted(通过RC4)(块大小为512)的块块块,我使用以下代码: if (e.getChannel().isWritable()) { FileInputStream fin = new FileInputStream(file); byte[] data = new byte[

我使用了netty 3.6,我想通过任何区域将文件数据写入通道,也可以通过加密
我在通道上使用FileRegion写入文件数据,没有任何问题,工作正常,不吃我的RAM,但我想在写入通道之前读取encypted(通过RC4)(块大小为512)的块块块,我使用以下代码:

if (e.getChannel().isWritable()) {

    FileInputStream fin = new FileInputStream(file);

    byte[] data = new byte[512];

         for (int i = 512; i < file.length(); i += 512) {

                        fin.read(data);
                        index_range += 512;
                        e.getChannel().write(ChannelBuffers.wrappedBuffer(RC4.encrypte(data)));

                    }

                    int remain_len=(int)(file.length() - index_range);
                    if(remain_len>0){
                    data = new byte[remain_len];
                    fin.read(data);
                    e.getChannel().write(ChannelBuffers.wrappedBuffer(RC4.encrypte(data)));
                    }
        fin.close();

     }
if(如getChannel().isWritable()){
FileInputStream fin=新的FileInputStream(文件);
字节[]数据=新字节[512];
对于(int i=512;i0){
数据=新字节[保留长度];
财务读取(数据);
e、 getChannel().write(ChannelBuffers.wrappedBuffer(RC4.encrypt(data)));
}
fin.close();
}


但是我在线程“pool-5-thread-1”java.lang.OutOfMemoryError:java堆空间中遇到了异常,我也使用了RandomAccessFile类,但问题是一样的,我如何解决这个问题

使用ChunkedWriteHandler和ChunkedNioFile。然后,您可以截取生成的ChannelBuffer并进行动态加密。

@normanm,您在服务器端给出的示例是used ChunkedFile,我可以在发布请求时使用ChunkedFile吗?

谢谢@normanm,我搜索了任何示例来执行此操作,但我找不到任何示例,你能用一个示例代码给我看看你的解释吗master@NormanMaurer没有找到该示例。你能分享一个新的链接吗。