private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
}
java类io.netty.channel.DefaultFileRegion的实例源码
FileOperationEncoder.java 文件源码
项目:fastdfs-spring-boot
阅读 27
收藏 0
点赞 0
评论 0
FileOperationEncoder.java 文件源码
项目:azeroth
阅读 25
收藏 0
点赞 0
评论 0
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException(
"unknown content type : " + content.getClass().getName());
}
FileOperationEncoder.java 文件源码
项目:fastdfs-client
阅读 26
收藏 0
点赞 0
评论 0
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
}
FileServerHandler.java 文件源码
项目:java_learn
阅读 25
收藏 0
点赞 0
评论 0
@Override
protected void messageReceived(ChannelHandlerContext cxt, String msg)
throws Exception {
File file = new File(msg);
if(file.exists()) {
if(!file.isFile()){
cxt.writeAndFlush("No file " + file + CR);
}
cxt.writeAndFlush("file " + file.length() + CR);
RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
FileRegion fileRegion = new DefaultFileRegion(randomAccessFile.getChannel(), 0, randomAccessFile.length());
cxt.write(fileRegion);
cxt.writeAndFlush(CR);
randomAccessFile.close();
}else{
cxt.writeAndFlush("File not found: " + file + CR);
}
}
FileOperationEncoder.java 文件源码
项目:fastdfs-spring-boot
阅读 26
收藏 0
点赞 0
评论 0
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
}
FileOperationEncoder.java 文件源码
项目:jeesuite-libs
阅读 24
收藏 0
点赞 0
评论 0
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
}
FileServerHandler.java 文件源码
项目:JavaAyo
阅读 23
收藏 0
点赞 0
评论 0
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
RandomAccessFile raf = null;
long length = -1;
try {
raf = new RandomAccessFile(msg, "r");
length = raf.length();
} catch (Exception e) {
ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
return;
} finally {
if (length < 0 && raf != null) {
raf.close();
}
}
ctx.write("OK: " + raf.length() + '\n');
if (ctx.pipeline().get(SslHandler.class) == null) {
// SSL not enabled - can use zero-copy file transfer.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));
} else {
// SSL enabled - cannot use zero-copy file transfer.
ctx.write(new ChunkedFile(raf));
}
ctx.writeAndFlush("\n");
}
FileRegionEncoderTest.java 文件源码
项目:rocketmq
阅读 27
收藏 0
点赞 0
评论 0
/**
* This unit test case ensures that {@link FileRegionEncoder} indeed wraps {@link FileRegion} to
* {@link ByteBuf}.
* @throws IOException if there is an error.
*/
@Test
public void testEncode() throws IOException {
FileRegionEncoder fileRegionEncoder = new FileRegionEncoder();
EmbeddedChannel channel = new EmbeddedChannel(fileRegionEncoder);
File file = File.createTempFile(UUID.randomUUID().toString(), ".data");
file.deleteOnExit();
Random random = new Random(System.currentTimeMillis());
int dataLength = 1 << 10;
byte[] data = new byte[dataLength];
random.nextBytes(data);
write(file, data);
FileRegion fileRegion = new DefaultFileRegion(file, 0, dataLength);
Assert.assertEquals(0, fileRegion.transfered());
Assert.assertEquals(dataLength, fileRegion.count());
Assert.assertTrue(channel.writeOutbound(fileRegion));
ByteBuf out = (ByteBuf) channel.readOutbound();
byte[] arr = new byte[out.readableBytes()];
out.getBytes(0, arr);
Assert.assertArrayEquals("Data should be identical", data, arr);
}
FileServerHandler.java 文件源码
项目:netty-book
阅读 22
收藏 0
点赞 0
评论 0
public void messageReceived(ChannelHandlerContext ctx, String msg)
throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("Not a file : " + file + CR);
return;
}
ctx.write(file + " " + file.length() + CR);
RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
FileRegion region = new DefaultFileRegion(
randomAccessFile.getChannel(), 0, randomAccessFile.length());
ctx.write(region);
ctx.writeAndFlush(CR);
randomAccessFile.close();
} else {
ctx.writeAndFlush("File not found: " + file + CR);
}
}
FileServerHandler.java 文件源码
项目:netty4.0.27Learn
阅读 25
收藏 0
点赞 0
评论 0
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
RandomAccessFile raf = null;
long length = -1;
try {
raf = new RandomAccessFile(msg, "r");
length = raf.length();
} catch (Exception e) {
ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
return;
} finally {
if (length < 0 && raf != null) {
raf.close();
}
}
ctx.write("OK: " + raf.length() + '\n');
if (ctx.pipeline().get(SslHandler.class) == null) {
// SSL not enabled - can use zero-copy file transfer.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));
} else {
// SSL enabled - cannot use zero-copy file transfer.
ctx.write(new ChunkedFile(raf));
}
ctx.writeAndFlush("\n");
}
AbstractEpollStreamChannel.java 文件源码
项目:netty4.0.27Learn
阅读 33
收藏 0
点赞 0
评论 0
protected boolean doWriteSingle(ChannelOutboundBuffer in, int writeSpinCount) throws Exception {
// The outbound buffer contains only one message or it contains a file region.
Object msg = in.current();
if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg;
if (!writeBytes(in, buf, writeSpinCount)) {
// was not able to write everything so break here we will get notified later again once
// the network stack can handle more writes.
return false;
}
} else if (msg instanceof DefaultFileRegion) {
DefaultFileRegion region = (DefaultFileRegion) msg;
if (!writeFileRegion(in, region, writeSpinCount)) {
// was not able to write everything so break here we will get notified later again once
// the network stack can handle more writes.
return false;
}
} else {
// Should never reach here.
throw new Error();
}
return true;
}
FileServerHandler.java 文件源码
项目:netty-study
阅读 26
收藏 0
点赞 0
评论 0
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("not a file :" + file + CR);
return;
}
ctx.write(file + " " + file.length() + CR);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileRegion fileRegion = new DefaultFileRegion(raf.getChannel(), 0, raf.length());
ctx.write(fileRegion);
ctx.writeAndFlush(CR);
raf.close();
} else {
ctx.writeAndFlush("file not found: " + file + CR);
}
}
FileServerHandler.java 文件源码
项目:hope-tactical-equipment
阅读 27
收藏 0
点赞 0
评论 0
public void messageReceived(ChannelHandlerContext ctx, String msg)
throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("Not a file : " + file + CR);
return;
}
ctx.write(file + " " + file.length() + CR);
RandomAccessFile randomAccessFile = new RandomAccessFile(msg, "r");
FileRegion region = new DefaultFileRegion(
randomAccessFile.getChannel(), 0, randomAccessFile.length());
ctx.write(region);
ctx.writeAndFlush(CR);
randomAccessFile.close();
} else {
ctx.writeAndFlush("File not found: " + file + CR);
}
}
FileServerHandler.java 文件源码
项目:javase-study
阅读 24
收藏 0
点赞 0
评论 0
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
RandomAccessFile raf = null;
long length = -1;
try {
raf = new RandomAccessFile(msg, "r");
length = raf.length();
} catch (Exception e) {
ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
return;
} finally {
if (length < 0 && raf != null) {
raf.close();
}
}
ctx.write("OK: " + raf.length() + '\n');
if (ctx.pipeline().get(SslHandler.class) == null) {
// SSL not enabled - can use zero-copy file transfer.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));
} else {
// SSL enabled - cannot use zero-copy file transfer.
ctx.write(new ChunkedFile(raf));
}
ctx.writeAndFlush("\n");
}
FileServer.java 文件源码
项目:netty4study
阅读 34
收藏 0
点赞 0
评论 0
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("Not a file: " + file + '\n');
return;
}
ctx.write(file + " " + file.length() + '\n');
FileInputStream fis = new FileInputStream(file);
FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
ctx.write(region);
ctx.writeAndFlush("\n");
fis.close();
} else {
ctx.writeAndFlush("File not found: " + file + '\n');
}
}
FileServer.java 文件源码
项目:distributeTemplate
阅读 26
收藏 0
点赞 0
评论 0
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)
throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("Not a file: " + file + '\n');
return;
}
ctx.write(file + " " + file.length() + '\n');
FileInputStream fis = new FileInputStream(file);
FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
ctx.write(region);
ctx.writeAndFlush("\n");
fis.close();
} else {
ctx.writeAndFlush("File not found: " + file + '\n');
}
}
FileServer.java 文件源码
项目:netty-netty-5.0.0.Alpha1
阅读 23
收藏 0
点赞 0
评论 0
@Override
public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
File file = new File(msg);
if (file.exists()) {
if (!file.isFile()) {
ctx.writeAndFlush("Not a file: " + file + '\n');
return;
}
ctx.write(file + " " + file.length() + '\n');
FileInputStream fis = new FileInputStream(file);
FileRegion region = new DefaultFileRegion(fis.getChannel(), 0, file.length());
ctx.write(region);
ctx.writeAndFlush("\n");
fis.close();
} else {
ctx.writeAndFlush("File not found: " + file + '\n');
}
}
Response.java 文件源码
项目:Razor
阅读 30
收藏 0
点赞 0
评论 0
/**
* Send response immediately for a file response
*
* @param raf RandomAccessFile
*/
public void sendFile(RandomAccessFile raf, long length) {
setDate();
setPowerBy();
setResponseTime();
header(CONTENT_LENGTH, Long.toString(length));
setHttpResponse(new DefaultHttpResponse(HTTP_1_1, getStatus(), true));
// Write initial line and headers
channelCxt.write(httpResponse);
// Write content
ChannelFuture sendFileFuture;
ChannelFuture lastContentFuture;
if (false /* if has ssl handler */) {
// TODO support ssl
} else {
sendFileFuture = channelCxt.write(new DefaultFileRegion(raf.getChannel(), 0, length), channelCxt.newProgressivePromise());
lastContentFuture = channelCxt.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
sendFileFuture.addListener(new ProgressiveFutureListener(raf));
if (!keepAlive) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
flush();
}
FileSegmentManagedBuffer.java 文件源码
项目:spark_deep
阅读 24
收藏 0
点赞 0
评论 0
@Override
public Object convertToNetty() throws IOException {
if (conf.lazyFileDescriptor()) {
return new DefaultFileRegion(file, offset, length);
} else {
FileChannel fileChannel = new FileInputStream(file).getChannel();
return new DefaultFileRegion(fileChannel, offset, length);
}
}
NettyFileBody.java 文件源码
项目:megaphone
阅读 30
收藏 0
点赞 0
评论 0
@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
@SuppressWarnings("resource")
// Netty will close the ChunkedNioFile or the DefaultFileRegion
final FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
Object message = (ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy()) ? //
new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize())
: new DefaultFileRegion(fileChannel, offset, length);
channel.write(message, channel.newProgressivePromise())//
.addListener(new ProgressListener(future.getAsyncHandler(), future, false, getContentLength()));
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
AbstractEpollStreamChannel.java 文件源码
项目:netty4.0.27Learn
阅读 26
收藏 0
点赞 0
评论 0
/**
* Write a {@link DefaultFileRegion}
*
* @param region the {@link DefaultFileRegion} from which the bytes should be written
* @return amount the amount of written bytes
*/
private boolean writeFileRegion(
ChannelOutboundBuffer in, DefaultFileRegion region, int writeSpinCount) throws Exception {
final long regionCount = region.count();
if (region.transfered() >= regionCount) {
in.remove();
return true;
}
final long baseOffset = region.position();
boolean done = false;
long flushedAmount = 0;
for (int i = writeSpinCount - 1; i >= 0; i--) {
final long offset = region.transfered();
final long localFlushedAmount =
Native.sendfile(fd().intValue(), region, baseOffset, offset, regionCount - offset);
if (localFlushedAmount == 0) {
break;
}
flushedAmount += localFlushedAmount;
if (region.transfered() >= regionCount) {
done = true;
break;
}
}
if (flushedAmount > 0) {
in.progress(flushedAmount);
}
if (done) {
in.remove();
} else {
// Returned EAGAIN need to set EPOLLOUT
setFlag(Native.EPOLLOUT);
}
return done;
}
Native.java 文件源码
项目:netty4.0.27Learn
阅读 27
收藏 0
点赞 0
评论 0
public static long sendfile(
int dest, DefaultFileRegion src, long baseOffset, long offset, long length) throws IOException {
// Open the file-region as it may be created via the lazy constructor. This is needed as we directly access
// the FileChannel field directly via JNI
src.open();
long res = sendfile0(dest, src, baseOffset, offset, length);
if (res >= 0) {
return res;
}
return ioResult("sendfile", (int) res, CONNECTION_RESET_EXCEPTION_SENDFILE);
}
EchoServerHttpRequestHandler.java 文件源码
项目:examples-javafx-repos1
阅读 24
收藏 0
点赞 0
评论 0
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
if( wsURI.equalsIgnoreCase(request.getUri()) ) {
ctx.fireChannelRead(request.retain());
} else {
if( HttpHeaders.is100ContinueExpected(request) ) {
send100Continue(ctx);
}
try (
RandomAccessFile rFile = new RandomAccessFile(indexHTML, "r")
) {
HttpResponse response = new DefaultHttpResponse( request.getProtocolVersion(), HttpResponseStatus.OK );
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
boolean keepAlive = HttpHeaders.isKeepAlive(request);
if( keepAlive ) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, rFile.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response);
if( ctx.pipeline().get(SslHandler.class) == null ) {
ctx.write(new DefaultFileRegion(rFile.getChannel(), 0, rFile.length()));
} else {
ctx.write(new ChunkedNioFile(rFile.getChannel()));
}
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if( !keepAlive ) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
}
}
HttpResponse.java 文件源码
项目:blade
阅读 25
收藏 0
点赞 0
评论 0
@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;
}
OutputStreamWrapper.java 文件源码
项目:blade
阅读 25
收藏 0
点赞 0
评论 0
@Override
public void close() throws IOException {
try {
this.flush();
FileChannel file = new FileInputStream(this.file).getChannel();
long fileLength = file.size();
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength);
httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate());
httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION);
boolean keepAlive = WebContext.request().keepAlive();
if (keepAlive) {
httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE);
}
// Write the initial line and the header.
ctx.write(httpResponse);
ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise());
// Write the end marker.
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
} finally {
if(null != outputStream){
outputStream.close();
}
}
}
WireProtocol.java 文件源码
项目:fuse
阅读 27
收藏 0
点赞 0
评论 0
default void stream(FuseRequestMessage message, Path path) {
if (!message.flushed()) {
ChannelHandlerContext ctx = message.getChannelContext();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
long size = WireProtocol.getFileSize(path);
response.headers().set(CONTENT_LENGTH, size);
ctx.write(response);
try {
ctx.write(
new DefaultFileRegion(FileChannel.open(path), 0, size)
);
ChannelFuture cfuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (!isKeepAlive(message.getRequest())) {
cfuture.addListener(ChannelFutureListener.CLOSE);
}
}
catch (Exception ex) {
log.error("Error streaming file !", ex);
ctx.close();
}
}
}
NettyTransport.java 文件源码
项目:jzab
阅读 34
收藏 0
点赞 0
评论 0
void sendFile(File file) throws Exception {
long length = file.length();
LOG.debug("Got request of sending file {} of length {}.",
file, length);
Message handshake = MessageBuilder.buildFileHeader(length);
byte[] bytes = handshake.toByteArray();
// Sends HANDSHAKE first before transferring actual file data, the
// HANDSHAKE will tell the peer's channel to prepare for the file
// transferring.
channel.writeAndFlush(Unpooled.wrappedBuffer(bytes)).sync();
ChannelHandler prepender = channel.pipeline().get("frameEncoder");
// Removes length prepender, we don't need this handler for file
// transferring.
channel.pipeline().remove(prepender);
// Adds ChunkedWriteHandler for file transferring.
ChannelHandler cwh = new ChunkedWriteHandler();
channel.pipeline().addLast(cwh);
// Begins file transferring.
RandomAccessFile raf = new RandomAccessFile(file, "r");
if (channel.pipeline().get(SslHandler.class) != null) {
// Zero-Copy file transferring is not supported for ssl.
channel.writeAndFlush(new ChunkedFile(raf, 0, length, 8912));
} else {
// Use Zero-Copy file transferring in non-ssl mode.
FileRegion region = new DefaultFileRegion(raf.getChannel(), 0, length);
channel.writeAndFlush(region);
}
// Restores pipeline to original state.
channel.pipeline().remove(cwh);
channel.pipeline().addLast("frameEncoder", prepender);
}
NettyResponse.java 文件源码
项目:wildweb
阅读 31
收藏 0
点赞 0
评论 0
@Override
public void close(File file) {
if(this.request.websocket().active()) return;
LoggerFactory.getLogger(getClass()).debug("Sending file as response");
if(!file.exists()) {
status(404);
close();
return;
}
RandomAccessFile raf;
long length;
FileChannel channel;
try {
raf = new RandomAccessFile(file, "r");
length = raf.length();
channel = raf.getChannel();
} catch (IOException e) {
status(500);
close();
return;
}
header("Content-Length", String.valueOf(length));
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
header("Content-Type", mimeTypesMap.getContentType(file));
commit();
this.channelContext.writeAndFlush(new DefaultFileRegion(channel, 0, length));
this.close();
}
FileProvider.java 文件源码
项目:mopar
阅读 30
收藏 0
点赞 0
评论 0
public FileRegion serve(String path) throws IOException {
path = rewrite(path);
if (codeOnly && !path.matches("^/(jogl_\\d_\\d\\.lib|(loader|loader_gl|runescape)\\.jar|(jogl|runescape|runescape_gl)\\.pack200|unpackclass.pack)$"))
return null;
File f = new File(root, path);
if (!f.getAbsolutePath().startsWith(root.getAbsolutePath()))
return null;
if (!f.exists() || !f.isFile())
return null;
return new DefaultFileRegion(FileChannel.open(f.toPath(), StandardOpenOption.READ), 0, f.length());
}
FileOperationEncoder.java 文件源码
项目:fastdfs-spring-boot
阅读 26
收藏 0
点赞 0
评论 0
FileOperationEncoder(File file) {
long length = file.length();
this.content = new DefaultFileRegion(file, 0, length);
this.size = length;
}