'okhttpClient requset download file fail,Server is built by Netty
Now,i build nettyServer and use okHttp request download, but okhttp response not has dataiostream, could someone help me solve the problem?
code
Server by Netty
mServerBootstrap.group(mBossGroup, mWorkerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SSLEngine engine = WebSocketUtils.createSslContext().createSSLEngine();
engine.setUseClientMode(false);
pipeline.addLast(new SslHandler(engine));
pipeline.addLast(new HttpServerCodec(4096, 8192, ArchiveStream.BUFFER_SIZE));
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new LoggingHandler());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new SimpleChannelInboundHandler<Object>() {
@Override /////216
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
handleDownload(ctx, fullHttpRequest);
}
});
}
});
void handleDownload(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) {
final ParcelFileDescriptor[] pipes = ParcelFileDescriptor.createPipe();
final InputStream pipeIn = new BufferedInputStream(new FileInputStream(pipes[0].getFileDescriptor()));
final OutputStream pipeOut = new BufferedOutputStream(new FileOutputStream(pipes[1].getFileDescriptor()));
mExecutor.execute(new Runnable() {
@Override
public void run() {
InputStream is = null;
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_DOWNLOADS;
File file = new File(path + File.separator + "a.txt");
is = new FileInputStream(file);
byte[] buffer = new byte[ArchiveStream.BUFFER_SIZE]; /////623
int N;
while (true) {
try {
N = is.read(buffer);
if (N < 0) break;
pipeOut.write(buffer, 0, N);
} catch (Exception e) {
Log.d(TAG, "read exception");
break;
}
}
}
});
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
ctx.write(response);
ctx.writeAndFlush(new HttpChunkedInput(new ChunkedStream(pipeIn)));
}
and client okhttp request download
Request request = new Request.Builder()
.url("https://" + "10.20.0.148" + ":" + "50000" +"/download?taskId=" + "15802749896")
.get().build();
Log.d(TAG, "download request :" + request);
mOkHttp.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure call= " + call , e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "onResponse call=" + call + ", reponse=" + response);
try {
final InputStream is = response.body().byteStream();
if (is == null) {
System.out.println("is null");
return;
}
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_DOWNLOADS;
File file = new File(path + File.separator + "a.txt");
byte[] buffer = new byte[ArchiveStream.BUFFER_SIZE];
try (OutputStream os = new FileOutputStream(file)){
while (true) {
int N = -1;
N = is.read(buffer);
System.out.println("read N=" + N);
if ( N < 0) break;
os.write(buffer, 0, N);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} finally {
response.close();
}
}
});
}
The reason is: clent can receiver repoonse log,but read buffer is error
onResponse call=okhttp3.RealCall@86ad38b, response=Response{protocol=http/1.1, code=200, message=OK, url=https://10.20.0.148:50000/download?taskId=15802749896} read N=-1
Could someone tell me how to solve this?? Thanks
Solution 1:[1]
DefaultFullHttpResponse cannot use with ChunkedWriteHandler...So in this situation, i should use DefaultHttpResponse instead of DefaultFullHttpResponse
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
