问题描述
发送文件时,您可以执行 ctx.writeAndFlush(new ChunkedFile(new File(file.png)));
。
When sending file, you can do ctx.writeAndFlush(new ChunkedFile(new File("file.png")));
.
列表< Object>
怎么样?
该列表包含字符串
和图像字节
。
来自文档 ChunkedInput()
但是我无法使用它。
from the documentation there's ChunkedInput()
but I'm not able to get the use of it.
UPDATE
让我们在我的处理程序中说,在$ code> channelRead0中(ChannelHandlerContext ctx,Object o)我想发送 List< Object>
的方法我已经完成了以下
let's say in my Handler, inside channelRead0(ChannelHandlerContext ctx, Object o)
method where I want to send the List<Object>
I've done the following
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
List<Object> msg = new ArrayList<>();
/**getting the bytes of image**/
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(fileName));
// convert BufferedImage to byte array
ByteArrayOutputStream bAoS = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", bAoS);
bAoS.flush();
imageInByte = baos.toByteArray();
baos.close();
msg.clear();
msg.add(0, "String"); //add the String into List
msg.add(1, imageInByte); //add the bytes of images into list
/**Chunk the List<Object> and Send it just like the chunked file**/
ctx.writeAndFlush(new ChunkedInput(DONT_KNOW_WHAT_TO_DO_HERE)); //
}
推荐答案
只需实现自己的 ChunkedInput< ByteBuf>
。在Netty附带的实现之后,您可以按如下方式实现它:
Just implement your own ChunkedInput<ByteBuf>
. Following the implementations shipped with Netty you can implement it as follows:
public class ChunkedList implements ChunkedInput<ByteBuf> {
private static final byte[] EMPTY = new byte[0];
private byte[] previousPart = EMPTY;
private final int chunkSize;
private final Iterator<Object> iterator;
public ChunkedList(int chunkSize, List<Object> objs) {
//chunk size in bytes
this.chunkSize = chunkSize;
this.iterator = objs.iterator();
}
public ByteBuf readChunk(ChannelHandlerContext ctx) {
return readChunk(ctx.alloc());
}
public ByteBuf readChunk(ByteBufAllocator allocator) {
if (isEndOfInput())
return null;
else {
ByteBuf buf = allocator.buffer(chunkSize);
boolean release = true;
try {
int bytesRead = 0;
if (previousPart.length > 0) {
if (previousPart.length > chunkSize) {
throw new IllegalStateException();
}
bytesRead += previousPart.length;
buf.writeBytes(previousPart);
}
boolean done = false;
while (!done) {
if (!iterator.hasNext()) {
done = true;
previousPart = EMPTY;
} else {
Object o = iterator.next();
//depending on the encoding
byte[] bytes = o instanceof String ? ((String) o).getBytes() : (byte[]) o;
bytesRead += bytes.length;
if (bytesRead > chunkSize) {
done = true;
previousPart = bytes;
} else {
buf.writeBytes(bytes);
}
}
}
release = false;
} finally {
if (release)
buf.release();
}
return buf;
}
}
public long length() {
return -1;
}
public boolean isEndOfInput() {
return !iterator.hasNext() && previousPart.length == 0;
}
public long progress() {
return 0;
}
public void close(){
//close
}
}
为了写 ChunkedContent
,有一个特殊的处理程序附带 Netty
。参见 io.netty.handler.stream.ChunkedWriteHandler
。所以只需添加到您的下游。以下是文档中的引用:
In order to write ChunkedContent
there is a special handler shipped with Netty
. See io.netty.handler.stream.ChunkedWriteHandler
. So just add to your downstream. Here is the quote from documentation:
这篇关于如何分块列表<对象>在Netty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!