@Override
public void download(@NonNull String fileName, @NonNull File file) throws Exception {
if (!file.exists() || !file.isFile()) {
throw new NotFoundException("Not found file: " + file.getPath());
}
RandomAccessFile raf = new RandomAccessFile(file, "r");
Long fileLength = raf.length();
this.contentType = StringKit.mimeType(file.getName());
io.netty.handler.codec.http.HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
HttpHeaders httpHeaders = httpResponse.headers().add(getDefaultHeader());
boolean keepAlive = WebContext.request().keepAlive();
if (keepAlive) {
httpResponse.headers().set(HttpConst.CONNECTION, KEEP_ALIVE);
}
httpHeaders.set(HttpConst.CONTENT_TYPE, this.contentType);
httpHeaders.set("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1"));
httpHeaders.setInt(HttpConst.CONTENT_LENGTH, fileLength.intValue());
// Write the initial line and the header.
ctx.write(httpResponse);
ChannelFuture sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
// Write the end marker.
ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
sendFileFuture.addListener(ProgressiveFutureListener.build(raf));
// Decide whether to close the connection or not.
if (!keepAlive) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
isCommit = true;
}
HttpResponse.java 文件源码
java
阅读 25
收藏 0
点赞 0
评论 0
项目:blade
作者:
评论列表
文章目录