


How does Java use NIO to optimize IO to implement file upload and download functions?
May 12, 2023 pm 09:31 PM1 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:因為傳統(tǒng)IO文件傳輸速率低,所以選擇了NIO進行文件的下載操作。NIO還有一個好處就是其中零拷貝可以實現(xiàn)減少內存中數(shù)據(jù)的重復,減少CPU操作的效果。所以相對于傳統(tǒng)IO,NIO有著效率高點的優(yōu)勢。
2 NIO為何較傳統(tǒng)的io速度較快
就拿單個io過程來看,首先時間主要花在了用戶態(tài)和內核態(tài)的轉換上,其次,考慮將多個io的“合并”為一個io,這不就節(jié)省時間了嗎
相應的NIO主要做了兩方面的提升
1.避免了用戶態(tài)和內核態(tài)的交換,直接操作內存,用戶態(tài)和內核態(tài)的轉換是很費時的,傳統(tǒng)的io寫入磁盤時,用戶態(tài)的接口不能直接操作內存,而是通過操作系統(tǒng)調用內核態(tài)接口來進行io。
2.利用buffer減少io的次數(shù),buffer化零為整”的寫入方式因為大大減小了尋址/寫入次數(shù),所以就降低了硬盤的負荷。
3.IO 是基于流來讀取的,而NIO則是基于塊讀取,面向流 的 I/O 系統(tǒng)一次一個字節(jié)地處理數(shù)據(jù)。一個輸入流產生一個字節(jié)的數(shù)據(jù),一個輸出流消費一個字節(jié)的數(shù)據(jù)。為流式數(shù)據(jù)創(chuàng)建過濾器非常容易。鏈接幾個過濾器,以便每個過濾器只負責單個復雜處理機制的一部分,這樣也是相對簡單的。不利的一面是,面向流的 I/O 通常相當慢。
一個 面向塊 的 I/O 系統(tǒng)以塊的形式處理數(shù)據(jù)。每一個操作都在一步中產生或者消費一個數(shù)據(jù)塊。按塊處理數(shù)據(jù)比按(流式的)字節(jié)處理數(shù)據(jù)要快得多。但是面向塊的 I/O 缺少一些面向流的 I/O 所具有的優(yōu)雅性和簡單性。
4.非阻塞IO 和 異步IO的支持, 減少線程占有的??臻g,以及上下文切換
5.IO 多路復用的支持
6.Buffer 支持,所有讀寫操作都是基于 緩沖 來實現(xiàn)
7.NIO 支持 Direct Memory, 可以減少一次數(shù)據(jù)拷貝
8.Netty 零拷貝的支持
3 NIO實戰(zhàn)上傳下載
3.1 url下載文件
java NIO包提供了無緩沖情況下在兩個通道之間直接傳輸字節(jié)的可能。
為了讀來自URL的文件,需從URL流創(chuàng)建ReadableByteChannel :
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
從ReadableByteChannel 讀取字節(jié)將被傳輸至FileChannel:
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME); FileChannel fileChannel = fileOutputStream.getChannel();
然后使用transferFrom方法,從ReadableByteChannel 類下載來自URL的字節(jié)傳輸?shù)紽ileChannel:
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
transferTo() 和 transferFrom() 方法比簡單使用緩存從流中讀更有效。依據(jù)不同的底層操作系統(tǒng),數(shù)據(jù)可以直接從文件系統(tǒng)緩存?zhèn)鬏數(shù)轿覀兊奈募?,而不必將任何字?jié)復制到應用程序內存中。
在Linux和UNIX系統(tǒng)上,這些方法使用零拷貝技術,減少了內核模式和用戶模式之間的上下文切換次數(shù)。
工具類:
/**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(); //采用零拷貝的方式實現(xiàn)文件的下載 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("文件名不可為空"); } //創(chuàng)建流 FileInputStream fis = null; FileOutputStream fos = null; //創(chuàng)建通道 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

Undress AI Tool
Undress images for free

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

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

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

Java's class loading mechanism is implemented through ClassLoader, and its core workflow is divided into three stages: loading, linking and initialization. During the loading phase, ClassLoader dynamically reads the bytecode of the class and creates Class objects; links include verifying the correctness of the class, allocating memory to static variables, and parsing symbol references; initialization performs static code blocks and static variable assignments. Class loading adopts the parent delegation model, and prioritizes the parent class loader to find classes, and try Bootstrap, Extension, and ApplicationClassLoader in turn to ensure that the core class library is safe and avoids duplicate loading. Developers can customize ClassLoader, such as URLClassL

Java supports asynchronous programming including the use of CompletableFuture, responsive streams (such as ProjectReactor), and virtual threads in Java19. 1.CompletableFuture improves code readability and maintenance through chain calls, and supports task orchestration and exception handling; 2. ProjectReactor provides Mono and Flux types to implement responsive programming, with backpressure mechanism and rich operators; 3. Virtual threads reduce concurrency costs, are suitable for I/O-intensive tasks, and are lighter and easier to expand than traditional platform threads. Each method has applicable scenarios, and appropriate tools should be selected according to your needs and mixed models should be avoided to maintain simplicity

JavaNIO is a new IOAPI introduced by Java 1.4. 1) is aimed at buffers and channels, 2) contains Buffer, Channel and Selector core components, 3) supports non-blocking mode, and 4) handles concurrent connections more efficiently than traditional IO. Its advantages are reflected in: 1) Non-blocking IO reduces thread overhead, 2) Buffer improves data transmission efficiency, 3) Selector realizes multiplexing, and 4) Memory mapping speeds up file reading and writing. Note when using: 1) The flip/clear operation of the Buffer is easy to be confused, 2) Incomplete data needs to be processed manually without blocking, 3) Selector registration must be canceled in time, 4) NIO is not suitable for all scenarios.

In Java, enums are suitable for representing fixed constant sets. Best practices include: 1. Use enum to represent fixed state or options to improve type safety and readability; 2. Add properties and methods to enums to enhance flexibility, such as defining fields, constructors, helper methods, etc.; 3. Use EnumMap and EnumSet to improve performance and type safety because they are more efficient based on arrays; 4. Avoid abuse of enums, such as dynamic values, frequent changes or complex logic scenarios, which should be replaced by other methods. Correct use of enum can improve code quality and reduce errors, but you need to pay attention to its applicable boundaries.

Singleton design pattern in Java ensures that a class has only one instance and provides a global access point through private constructors and static methods, which is suitable for controlling access to shared resources. Implementation methods include: 1. Lazy loading, that is, the instance is created only when the first request is requested, which is suitable for situations where resource consumption is high and not necessarily required; 2. Thread-safe processing, ensuring that only one instance is created in a multi-threaded environment through synchronization methods or double check locking, and reducing performance impact; 3. Hungry loading, which directly initializes the instance during class loading, is suitable for lightweight objects or scenarios that can be initialized in advance; 4. Enumeration implementation, using Java enumeration to naturally support serialization, thread safety and prevent reflective attacks, is a recommended concise and reliable method. Different implementation methods can be selected according to specific needs

Anonymous internal classes are used in Java to create subclasses or implement interfaces on the fly, and are often used to override methods to achieve specific purposes, such as event handling in GUI applications. Its syntax form is a new interface or class that directly defines the class body, and requires that the accessed local variables must be final or equivalent immutable. Although they are convenient, they should not be overused. Especially when the logic is complex, they can be replaced by Java8's Lambda expressions.

String is immutable, StringBuilder is mutable and non-thread-safe, StringBuffer is mutable and thread-safe. 1. Once the content of String is created cannot be modified, it is suitable for a small amount of splicing; 2. StringBuilder is suitable for frequent splicing of single threads, and has high performance; 3. StringBuffer is suitable for multi-threaded shared scenarios, but has a slightly lower performance; 4. Reasonably set the initial capacity and avoid using String splicing in loops can improve performance.

Use the installation media to enter the recovery environment; 2. Run the bootrec command to repair the boot records; 3. Check for disk errors and repair system files; 4. Disable automatic repair as a temporary means. The Windows automatic repair loop is usually caused by system files corruption, hard disk errors or boot configuration abnormalities. The solution includes troubleshooting by installing the USB flash drive into the recovery environment, using bootrec to repair MBR and BCD, running chkdsk and DISM/sfc to repair disk and system files. If it is invalid, the automatic repair function can be temporarily disabled, but the root cause needs to be checked later to ensure that the hard disk and boot structure are normal.
