


How does Java use NIO to optimize IO to implement file upload and download functions?
1 NIO的一些基础预备知识
Java中IO流类的体系中BIO与NIO:https://blog.csdn.net/ZGL_cyy/article/details/104326458
Java IO体系与NIO和BIO体系面试题 :https://blog.csdn.net/ZGL_cyy/article/details/122836368
为什么使用NIO:因为传统IO文件传输速率低,所以选择了NIO进行文件的下载操作。NIO还有一个好处就是其中零拷贝可以实现减少内存中数据的重复,减少CPU操作的效果。所以相对于传统IO,NIO有着效率高点的优势。
2 NIO为何较传统的io速度较快
就拿单个io过程来看,首先时间主要花在了用户态和内核态的转换上,其次,考虑将多个io的“合并”为一个io,这不就节省时间了吗
相应的NIO主要做了两方面的提升
1.避免了用户态和内核态的交换,直接操作内存,用户态和内核态的转换是很费时的,传统的io写入磁盘时,用户态的接口不能直接操作内存,而是通过操作系统调用内核态接口来进行io。
2.利用buffer减少io的次数,buffer化零为整”的写入方式因为大大减小了寻址/写入次数,所以就降低了硬盘的负荷。
3.IO 是基于流来读取的,而NIO则是基于块读取,面向流 的 I/O 系统一次一个字节地处理数据。一个输入流产生一个字节的数据,一个输出流消费一个字节的数据。为流式数据创建过滤器非常容易。链接几个过滤器,以便每个过滤器只负责单个复杂处理机制的一部分,这样也是相对简单的。不利的一面是,面向流的 I/O 通常相当慢。
一个 面向块 的 I/O 系统以块的形式处理数据。每一个操作都在一步中产生或者消费一个数据块。按块处理数据比按(流式的)字节处理数据要快得多。但是面向块的 I/O 缺少一些面向流的 I/O 所具有的优雅性和简单性。
4.非阻塞IO 和 异步IO的支持, 减少线程占有的栈空间,以及上下文切换
5.IO 多路复用的支持
6.Buffer 支持,所有读写操作都是基于 缓冲 来实现
7.NIO 支持 Direct Memory, 可以减少一次数据拷贝
8.Netty 零拷贝的支持
3 NIO实战上传下载
3.1 url下载文件
java NIO包提供了无缓冲情况下在两个通道之间直接传输字节的可能。
为了读来自URL的文件,需从URL流创建ReadableByteChannel :
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
从ReadableByteChannel 读取字节将被传输至FileChannel:
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME); FileChannel fileChannel = fileOutputStream.getChannel();
然后使用transferFrom方法,从ReadableByteChannel 类下载来自URL的字节传输到FileChannel:
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
transferTo() 和 transferFrom() 方法比简单使用缓存从流中读更有效。依据不同的底层操作系统,数据可以直接从文件系统缓存传输到我们的文件,而不必将任何字节复制到应用程序内存中。
在Linux和UNIX系统上,这些方法使用零拷贝技术,减少了内核模式和用户模式之间的上下文切换次数。
工具类:
/**NIO文件下载工具类 * @author olalu */ public class NioDownloadUtils { /** * @description: * @param file: 要下在文件 * @return: void */ public static void downloadDoc(File file,HttpServletResponse response) throws IOException { OutputStream outputStream = response.getOutputStream(); String contentType = Files.probeContentType(Paths.get(file.getAbsolutePath())); //设置响应头 response.setHeader("Content-Type", contentType); response.setHeader("Content-Disposition", "attachment;filename="+ new String(file.getName().getBytes("utf-8"),"ISO8859-1")); response.setContentLength((int) file.length()); //获取文件输入流 FileInputStream fileInputStream = new FileInputStream(file); //获取输出流通道 WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); FileChannel fileChannel = fileInputStream.getChannel(); //采用零拷贝的方式实现文件的下载 fileChannel.transferTo(0,fileChannel.size(),writableByteChannel); //关闭对应的资源 fileChannel.close(); outputStream.flush(); writableByteChannel.close(); } public static void downloadDoc(String path,HttpServletResponse response) throws IOException { File file = new File(path); if (!file.exists()){ throw new RuntimeException("文件不存在"); } downloadDoc(file,response); } }
3.2 通过NIO上传文件
/** * 文件上传方法 */ public static Result uploading(MultipartFile file) { //获取文件名 String realName = file.getOriginalFilename(); String newName = null; if(realName != null && realName != ""){ //获取文件后缀 String suffixName = realName.substring(realName.lastIndexOf(".")); //生成新名字 newName = UUID.randomUUID().toString().replaceAll("-", "")+suffixName; }else { return Result.fail("文件名不可为空"); } //创建流 FileInputStream fis = null; FileOutputStream fos = null; //创建通道 FileChannel inChannel = null; FileChannel outChannel = null; try { fis = (FileInputStream)file.getInputStream(); //开始上传 fos = new FileOutputStream(UPLOAD_URL+"\\"+newName); //通道间传输 inChannel = fis.getChannel(); outChannel = fos.getChannel(); //上传 inChannel.transferTo(0,inChannel.size(),outChannel); }catch (IOException e){ return Result.fail("文件上传路径错误"); }finally { //关闭资源 try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } catch (IOException e) { e.printStackTrace(); } } return Result.ok(newName); }
The above is the detailed content of How does Java use NIO to optimize IO to implement file upload and download functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
