简体   繁体   English

Java NIO Servlet to File

[英]Java NIO Servlet to File

Is there a way (without buffering the whole Inputstream) to take the HttpServletRequest from a Java Servlet and write it out to a file using all NIO? 有没有办法(没有缓冲整个Inputstream)从Java Servlet获取HttpServletRequest并使用所有NIO将其写入文件? Is it even worth trying? 它甚至值得尝试吗? Will it be any faster reading from a normal java.io stream and writing to a java.nio Channel or do they both really need to be pure NIO to see a benefit? 从普通的java.io流中读取并写入java.nio频道是否会更快?或者它们是否真的需要纯粹的NIO才能看到好处? Thanks. 谢谢。

EDIT: 编辑:

So I just did a quick and dirty benchmark, reading a file from one disk and writing to a different disk (so I'm actually testing the code and not the disk). 所以我只是做了一个快速而又脏的基准测试,从一个磁盘读取文件并写入另一个磁盘(所以我实际上是测试代码而不是磁盘)。

Averages:
InputStream -> OutputStream : 321 ms.
FileChannel -> FileChannel  :   3 ms.
InputStream -> FileChannel  : 600 ms.

I actually got worse performance trying to use a hybrid java.io -> java.nio. 实际上我尝试使用混合java.io - > java.nio时性能更差。 The nio->nio was faster by A LOT, but I'm stuck with the Servlet InputStream. nio-> nio速度快了很多,但我坚持使用Servlet InputStream。

The primary benefit of a pure NIO solution is that you may be able to avoid copying data from kernel to user and back to kernel space. 纯NIO解决方案的主要好处是您可以避免将数据从内核复制到用户并返回到内核空间。 When you use a transferTo() or transferFrom() operation, this overhead can be avoided and transfers between channels can be very fast (depending on the underlying implementation). 当您使用transferTo()transferFrom()操作时,可以避免此开销,并且通道之间的传输可以非常快(取决于底层实现)。

However, the Servlet API doesn't allow you to access a source Channel ; 但是,Servlet API不允许您访问源Channel ; by the time your servlet sees the data, they're in user space. 当您的servlet看到数据时,它们就在用户空间中。 So I wouldn't expect a performance boost from writing to a Channel . 因此,我不希望通过写入Channel来提升性能。

HttpServletRequest为您提供常规的“拉”输入流 - 不要看NIO如何在这里提供帮助。

you can't seriously compare performance with tests as short as milliseconds. 你不能认真地将性能与毫秒级的测试进行比较。

disks won't spin any faster because of choice of API. 由于API的选择,磁盘不会旋转得更快。 what happened in FileChannel->FileChannel is probably that the call returned before the writes actually committed to the disk. 在FileChannel-> FileChannel中发生的事情可能是在写入实际提交给磁盘之前返回的调用。

nio could save some cpu/memory usage. nio可以节省一些CPU /内存使用量。 but not much in your situation. 但在你的情况下并不多。 for saving files uploaded by users, usually it's in the format of multipart/form-data, and server must read the stream byte-by-byte to parse the input and extract file content, it cannot just directly dump raw stream to file. 为了保存用户上传的文件,通常它的格式为multipart / form-data,而服务器必须逐字节读取流来解析输入并提取文件内容,它不能直接将原始流转储到文件中。

I think you're worrying about nothing. 我觉得你什么都不担心。 The average network speed (~1MB/s) can't catch up the average harddisk speed (~100MB/s) by far (even with the most optimistic averages). 到目前为止,平均网络速度(~1MB / s)无法赶上平均硬盘速度(~100MB / s)(即使是最乐观的平均值)。 The filechannels won't give you any benefits. 文件渠道不会给您带来任何好处。 The "legacy" java.io is more than sufficient. “遗留” java.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM