File downloading method and device, computer device and storage medium

By utilizing a task thread pool to distribute file writing tasks during the file download process, the problems of response delay and blocking at the file receiving end are solved, thus improving file download efficiency.

CN116527653BActive Publication Date: 2025-11-18BEI DOU ZHI LIAN KE JI YOU XIAN GONG SI
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310395133.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-04-13
Publication Date
2025-11-18
Estimated Expiration
2043-04-13

AI Technical Summary

Technical Problem

In existing technologies, the file download process has a significant impact on the file receiving end's response to other requests, and may even cause the file receiving end to be blocked, especially when downloading large files, thus affecting processing efficiency.

Method used

The main thread receives fragmented data from the file sender and generates file write tasks. These tasks are then distributed to child threads using a task thread pool for file writing. The main thread is responsible for both file reading and writing, thus reducing the time and effort that file writing places on the main thread.

Benefits of technology

This reduces the impact of the file download process on the file receiver's response to other requests, avoids blocking of the file receiver during file writing, and improves the processing efficiency during the file download process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116527653B_ABST
    Figure CN116527653B_ABST
Patent Text Reader

Abstract

The application discloses a file downloading method and device, computer equipment and a storage medium. The method comprises the following steps: receiving, by a main thread, file data of a file to be downloaded sent by a file sending end; generating a file writing task corresponding to the file data according to the receiving sequence of the file data, wherein the file writing task comprises the file data and writing position information; putting the file writing task into a task thread pool; distributing the file writing task to sub-threads in the task thread pool by the task thread pool; and writing the file data into a target file according to the writing position information in the corresponding file writing task by each sub-thread, and determining that the file downloading is completed if it is determined that the file data receiving of the file to be downloaded is completed and the file data is completely written into the target file. The application executes the time-consuming file data writing process by the sub-threads in the task thread pool, so that the main thread can respond to other requests during the file writing process, and the processing efficiency of the file receiving end during the file downloading process is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a file download method, apparatus, computer equipment, and storage medium. Background Technology

[0002] File download typically involves two processes: file reading and file writing. File reading refers to the receiving end receiving file data sent by the sending end, which is then stored in memory. File writing refers to writing the file data stored in memory to the local disk, ultimately resulting in the downloaded file. To improve file transfer efficiency and stability, the sending end usually sends the file to the receiving end in chunks (data fragments). During this process, the receiving end typically uses a loop read-write approach to download the file data: reading file data chunks, writing them to the local disk, reading more file data chunks, writing them to the local disk, and so on, until all the file data is written to the local disk. However, because the process of writing file data to the local disk is relatively time-consuming, it significantly impacts the receiving end's ability to respond to other requests. For downloading large files, the receiving end may even experience blocking, greatly reducing its processing efficiency. Summary of the Invention

[0003] This application provides a file download method, apparatus, computer device, and storage medium, which can solve the technical problem in the prior art that the file download process has a significant impact on the file receiving end's response to other requests, and may even cause the file receiving end to be blocked.

[0004] In a first aspect, embodiments of this application provide a file download method, including:

[0005] Receive at least one fragment of the file to be downloaded from the file sender via the main thread;

[0006] A file writing task corresponding to each of the data segments is generated according to the receiving order of the data segments; the file writing task includes the data segments and writing position information.

[0007] The task of writing each of the aforementioned files is placed into the task thread pool;

[0008] The task of writing each file is distributed to at least one sub-thread in the task thread pool through the task thread pool;

[0009] For each of the sub-threads, the fragmented data in the file writing task is written to the target file according to the write position information in the corresponding file writing task. The target file is stored in the local disk of the file receiving end and is used to store the file data of the file to be downloaded.

[0010] If it is determined that the file data of the file to be downloaded has not been completely received, then return to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread;

[0011] If it is determined that the file data of the file to be downloaded has been received completely and written entirely into the target file, then the download of the file to be downloaded is determined to be complete.

[0012] Secondly, embodiments of this application provide a file download device, including:

[0013] The data receiving unit is used to receive at least one fragment of the file to be downloaded sent by the file sending end through the main thread;

[0014] The task generation unit is used to generate file writing tasks corresponding to each of the data segments according to the receiving order of each data segment; the file writing task includes the data segments and writing position information.

[0015] A data writing unit is used to put each of the file writing tasks into a task thread pool; distribute each of the file writing tasks to at least one sub-thread in the task thread pool; for each sub-thread, write the fragmented data in the file writing task into a target file according to the write position information in the corresponding file writing task, the target file is stored in the local disk of the file receiving end, and the target file is used to store the file data of the file to be downloaded;

[0016] The completion unit is used to, if it is determined that the file data of the file to be downloaded has not been completely received, return to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread; if it is determined that the file data of the file to be downloaded has been completely received and completely written to the target file, then determine that the download of the file to be downloaded is complete.

[0017] Thirdly, embodiments of this application also provide a computer device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the file download method described in the first aspect.

[0018] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program that, when executed by a processor, causes the processor to perform the file download method described in the first aspect.

[0019] In the process of downloading a file, this application receives at least one fragment of the file to be downloaded from the file sender via a main thread. Based on the receiving order of each fragment, it generates a write task corresponding to each fragment, including the fragment data and write position information. Then, each file write task is placed into a task thread pool, which distributes the file write tasks to at least one sub-thread within the task thread pool. This allows each sub-thread to write the fragment data from the corresponding file write task to a target file stored on the local disk of the file receiver, according to the write position information in the corresponding file write task. In some technologies, a single-threaded loop is used for reading and writing fragmented data, causing the main thread to handle the entire file download process, including file reading and writing, which affects the file receiver's response to other commands. In contrast, this application delegates the time-consuming file data writing process to a child thread in the task thread pool. This allows the main thread to respond to other processing requests during file writing, reducing the impact of the file download process, especially the download of large files, on the file receiver's response to other requests. It also avoids the file receiver being blocked (unable to respond to other processing requests) during file writing, thus improving the processing efficiency of the file receiver during file download. Attached Figure Description

[0020] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0021] Figure 1 This is a first flowchart illustrating the file download method provided in an embodiment of this application;

[0022] Figure 2 A schematic diagram illustrating the file writing task distribution process of the file download method provided in this application embodiment;

[0023] Figure 3 Another schematic diagram of the file download method provided in the embodiments of this application;

[0024] Figure 4 Another schematic diagram of the file download method provided in the embodiments of this application;

[0025] Figure 5 A schematic block diagram of a file download device provided in an embodiment of this application;

[0026] Figure 6 Another schematic block diagram of the file download device provided in the embodiments of this application;

[0027] Figure 7 A schematic block diagram of a computer device provided in an embodiment of this application. Detailed Implementation

[0028] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0029] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.

[0030] It should also be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the scope of the application. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.

[0031] It should also be further understood that the term “and / or” as used in this application specification and the appended claims means any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.

[0032] This application provides a file download method, apparatus, device, and storage medium, which can reduce the impact of the file download process on the file receiving end's response to other requests, avoid blocking of the file receiving end during file writing, and improve the processing efficiency of the file receiving end during the file download process. The executing entity of this file download method can be the file download apparatus provided in the embodiments of this application, or a computer device integrating the file download apparatus provided in the embodiments of this application. The file download apparatus can be implemented in hardware or software, and the computer device can be a terminal or a server. The terminal can be a vehicle-mounted computer, smartphone, tablet computer, PDA, or laptop computer, etc.

[0033] Please see Figure 1 , Figure 1 This is a flowchart illustrating a file download method provided in an embodiment of this application. The file download method is executed by a file receiving end. In some embodiments, the file receiving end can be an in-vehicle computer. The file download method specifically includes the following steps S101 to S107.

[0034] Step S101: Receive at least one fragment of the file to be downloaded from the file sender via the main thread.

[0035] The aforementioned file sending end is used to send the file data of the file to be downloaded. Specifically, it can be a terminal or a server. The terminal can be a smartphone, tablet computer, PDA, laptop computer, smart wearable device, etc. In some embodiments, the file receiving end is an in-vehicle computer, in which case the terminal is a terminal that can communicate and connect with the in-vehicle computer.

[0036] In this embodiment, when a file needs to be downloaded, the file receiving end can send a file download instruction for the file to be downloaded to the file sending end. After receiving the file download instruction, the file sending end can send the fragmented data of the file to be downloaded to the file receiving end based on the communication protocol supported by the file sending end and the file receiving end. The file receiving end then receives the fragmented data sent by the file sending end through the main thread.

[0037] The aforementioned communication protocol may be FTP (File Transfer Protocol), and in some embodiments, the file transfer protocol may also be IAP2 (iPod Accessory Protocol 2). This application does not impose any restrictions on this.

[0038] Specifically, before sending the fragmented data of the file to be downloaded, the file sending end can send a start sending command to the file receiving end, instructing it to begin sending the fragmented data of the file to be downloaded. The file sending end can also send a data fragment sending reminder command to the file receiving end before sending each fragment, such as "Sending the first fragment now." During the file data sending process, the file sending end can also send file sending status commands to the file receiving end, such as pause sending commands and sending completion commands. The file receiving end can then determine the current file sending status based on the file sending status commands sent by the file sending end. In some embodiments, before sending the fragmented data of the file to be downloaded, the file sending end can also send the total file size of the file to be downloaded. The file receiving end can then determine the fragmented data receiving progress based on the received total file size (first file size). For example, in one embodiment, the file receiving end can determine whether all file data of the file to be downloaded has been received completely based on the total file size and the size of the received fragmented data.

[0039] The fragmented data received by the file sending end can be a single fragment or multiple fragments. These multiple fragments can be obtained by the file receiving end continuously receiving the fragmented data sent by the file sending end.

[0040] Step S102: Generate file writing tasks corresponding to each of the data segments according to the receiving order of each data segment.

[0041] The aforementioned file write task instructs a sub-thread to write the fragmented data from the task to the target file. Each file write task includes the fragmented data and write position information. Specifically, the write position information in the file write task indicates the write position of the fragmented data in the target file obtained after downloading, so that the final target file is identical to the file to be downloaded. In some specific embodiments, the write position information may include the storage path of the target file and the starting offset position of the fragmented data in the target file. This write position information instructs the sub-thread to write the fragmented data starting from the starting offset position in the target file corresponding to the storage path.

[0042] In this embodiment, since the file data of the file to be downloaded is sent to the file receiving end in the form of fragmented data, the file receiving end needs to combine the fragmented data in an orderly manner to obtain the complete content of the file to be downloaded. Therefore, after receiving at least one fragment of the file to be downloaded sent by the file sending end, the write position information corresponding to the fragmented data can be determined according to the receiving order of each fragmented data, and then a file writing task including the fragmented data and the write position information corresponding to the fragmented data can be generated.

[0043] Specifically, in some implementations, the main thread can determine the write position information corresponding to each data segment based on the data size of each received data segment and the receiving order of each data segment. For example, for the first data segment, the write position information in its corresponding file write task can indicate that the data segment should be written starting at an offset of 0 bits in the target file. For the second data segment, the write position information in its corresponding file write task can indicate that the second data segment should be written starting at an offset of the data size (f1) + 1 bit in the target file. For the third data segment, the write position information in its corresponding file write task can indicate that the third data segment should be written starting at an offset of the sum of the data sizes of the first and second data segments (f1 + f2) + 1 bit in the target file, and so on. This will not be elaborated further here.

[0044] The data sizes of the aforementioned data segments may be the same or different, and this application does not impose any restrictions on this.

[0045] Step S103: Place each of the file writing tasks into a task thread pool, and distribute each of the file writing tasks to at least one sub-thread in the task thread pool.

[0046] In this embodiment, the task thread pool is used to distribute file writing tasks to child threads in the task thread pool. The task thread pool is a thread management technology that uses the pooling technology concept. It can reuse threads, conveniently manage threads and tasks, and decouple thread creation and task execution. Furthermore, by creating a thread pool to manage threads, the resource consumption caused by frequent thread creation and destruction can be reduced.

[0047] The task thread pool can contain one or more child threads. After receiving a file write task, the task thread pool stores the file write task in a task queue and then distributes the file write task to one or more idle child threads in the task thread pool through the task queue. After each child thread receives a file write task, it can then write fragmented data according to the file write task.

[0048] For example, such as Figure 2 As shown, after receiving the fragmented data (solid quadrilaterals in the diagram), the main thread generates file write tasks corresponding to the fragmented data (solid pentagons in the diagram) and places the file write tasks into the task queue of the task flow pool, such as... Figure 2As shown, there are 3 child threads (idle child threads, solid triangles in the figure) in the task flow pool. The task queue first distributes the file write tasks in the queue to child thread 1. After receiving the file write task, child thread 1 starts to process the file write task (at this time, child thread 1 is occupied, hollow triangles in the figure). Since there are still 2 child threads (idle child threads) in the task thread pool, the task queue can continue to distribute file write tasks.

[0049] It should be noted that in the task thread pool, child threads can repeatedly accept file write tasks. That is, after each child thread completes the received file write task, it becomes an idle child thread. The task queue can continue to distribute file write tasks stored in the queue to the idle child thread, and the idle child thread then changes to the occupied state, that is, it performs file write task processing.

[0050] Step S104: For each sub-thread, write the fragmented data in the file writing task to the target file according to the write position information in the corresponding file writing task.

[0051] In this embodiment, after receiving a file write task, each sub-thread can write the fragmented data from the file write task to the target file stored on the local disk of the file receiving end, based on the write position information in the received file write task. Specifically, when there is only one sub-thread, the operation of "receiving file write task - executing file write task" can be executed cyclically by the sub-thread. When there are multiple sub-threads, multiple sub-threads can write to the target file simultaneously, thereby improving the writing efficiency of the file to be downloaded.

[0052] The target file can be a file located in a preset storage path. The filename of the target file can be the same as the file to be downloaded, or it can be named according to the naming rules of the file receiving end.

[0053] In one embodiment, to further reduce the time spent writing fragmented data to the target file and avoid repeated opening and closing of the target file, the target file can be opened by the first sub-thread in the task thread pool that writes the fragmented data of the file writing task to the target file according to the write position information in the corresponding file writing task (i.e., the first sub-thread). Subsequently, subsequent sub-threads, i.e., the sub-threads in the task thread pool that do not write the fragmented data of the file writing task to the target file according to the write position information in the corresponding file writing task (i.e., the second sub-thread), can write the fragmented data without opening the target file. After this, the target file can be closed by the last sub-thread that writes the fragmented data to the target file. Specifically, each sub-thread that has completed writing the fragmented data can determine whether the file to be downloaded has been completely written to the target file. If the file to be downloaded has been completely written to the target file, then this sub-thread is the last writing sub-thread, and the target file is closed by the last writing sub-thread. At this point, the download of the target file is complete.

[0054] For example, specifically, such as Figure 3 As shown, if there are two child threads in the task thread pool for writing fragmented data, and if child thread 1 is the first child thread (the first child thread) that writes the fragmented data (fragmented data d1 in the figure) in the file writing task (file writing task t1 in the figure) to the target file according to the write position information in the corresponding file writing task t1, then the action performed by child thread 1 for this file writing task t1 is as follows:

[0055] S11. Open the target file.

[0056] S12. Write the fragment data d1 into the target file according to the write position information.

[0057] S13. Determine whether the file to be downloaded has been completely written into the target file.

[0058] S14. If the file to be downloaded has been completely written to the target file, then the target file is closed through the sub-thread.

[0059] Since the file to be downloaded has not been completely written to the target file, sub-thread 1 does not need to perform the operation of closing the target file at this time.

[0060] Child thread 2 receives and executes file write task t2 after child thread 1. Therefore, child thread 2 is the second child thread. The actions performed by child thread 2 for file write task t2 are as follows:

[0061] S21. Write the fragment data d2 into the target file according to the write position information.

[0062] S22. Determine whether the file to be downloaded has been completely written into the target file.

[0063] S23. If the file to be downloaded has been completely written to the target file, then the target file is closed through the sub-thread.

[0064] Since the file to be downloaded has not been completely written to the target file, sub-thread 2 does not need to perform the operation of closing the target file at this time.

[0065] After child thread 1 completes file write task t1, it receives file write task t3. At this point, child thread 1 is also the second child thread. The action performed by child thread 1 for file write task t3 is as follows:

[0066] S31. Write the fragment data d3 into the target file according to the write position information.

[0067] S32. Determine whether the file to be downloaded has been completely written into the target file.

[0068] S33. If the file to be downloaded has been completely written to the target file, then the target file is closed through the sub-thread.

[0069] Since the file to be downloaded has not been completely written to the target file, sub-thread 1 does not need to perform the operation of closing the target file at this time.

[0070] After child thread 2 completes file write task t2, it receives file write task t4. If file write task t4 is the last file write task for the file to be downloaded, the action performed by child thread 2 is as follows:

[0071] S41. Write the fragment data d4 into the target file according to the write position information.

[0072] S42. Determine whether the file to be downloaded has been completely written into the target file.

[0073] S43. If the file to be downloaded has been completely written to the target file, then the target file is closed through the sub-thread.

[0074] At this point, during the judgment process in step S42 above, sub-thread 2 determines that the file to be downloaded has been completely written to the target file. Therefore, it executes the operation of closing the target file in step S43. Thus, the process of writing the file data of the file to be downloaded is completed, that is, the download of the file to be downloaded is completed.

[0075] In some embodiments of this application, after closing the target file, the child thread can also send file writing completion information to the main thread.

[0076] In one specific implementation, during the process of determining whether the file to be downloaded has been completely written into the target file (i.e., steps S13, S22, S32, and S42), it can be determined whether the file size of the file to be downloaded (i.e., the first file size) is the same as the file size of the target file (i.e., the second file size). If the file size of the file to be downloaded is the same as the file size of the target file, it can be determined that the file to be downloaded has been completely written into the target file. If the file size of the file to be downloaded is not the same as the file size of the target file, it can be determined that the file to be downloaded has not been completely written into the target file.

[0077] In this embodiment, the file size of the target file is the same as the file to be downloaded, which means that the file data of the file to be downloaded has been completely written into the target file, and the target file can be closed.

[0078] In one embodiment, during the execution of a file writing task, after determining that the file to be downloaded has not been completely written to the target file, the sub-thread can further determine whether the current download time of the file to be downloaded is greater than a preset download time threshold. If the current download time of the file to be downloaded is greater than the preset download time threshold, the sub-thread can send download timeout information to the main thread.

[0079] The preset download duration threshold can be determined according to the actual situation, and this application does not impose any restrictions on it.

[0080] In this embodiment, the current download time is the duration from the start of downloading the file to the current judgment node of the child thread. Since the file to be downloaded has not been completed, if the current download time exceeds the preset download time threshold, an error is very likely to have occurred during the download process. Therefore, the child thread can send a download timeout message to the main thread to remind the main thread that the download of the file to be downloaded has timed out. After receiving the download timeout message, the main thread can promptly detect the reason for the download timeout, determine whether there is a download error, and restart the download of the file to be downloaded when it is determined that there is a download error, that is, re-execute steps S101 to S107.

[0081] In this embodiment, by setting a download time threshold, it is beneficial to detect errors that occur during the download process in a timely manner and to re-download, thereby reducing the download time of the file to be downloaded.

[0082] Step S105: Determine whether the file data of the file to be downloaded has been completely received and completely written to the target file. If the file data of the file to be downloaded has been completely received and completely written to the target file, then proceed to step S107 below; if the file data of the file to be downloaded has not been completely received, then proceed to step S106 below.

[0083] In the specific implementation process, we can first determine whether the file data of the file to be downloaded has been completely received, that is, whether the file receiving end has received all the file data of the file to be downloaded. If the file receiving end has received all the file data of the file to be downloaded, we can further determine whether all the file data of the file to be downloaded has been completely written to the target file.

[0084] Specifically, in some implementations, the reception of file data for the file to be downloaded can be determined based on whether a sending completion instruction is received from the file receiving end. In one embodiment, to ensure that the file receiving end receives all the fragmented data sent by the file sending end, during the process of determining whether the file data for the file to be downloaded has been received, the main thread can also first obtain the file size (first file size) of the file to be downloaded. For example, the file sending end can send the file size of the file to be downloaded to the file receiving end before sending the fragmented data of the file to be downloaded to the file receiving end. Then, after each fragmented data received from the file sending end, the file receiving end can calculate the data size of the received fragmented data and accumulate it to determine the total data size of all received fragmented data. If the total data size of the received fragmented data for the file to be downloaded has reached the file size (first file size) of the file to be downloaded, it can be considered that the file data for the file to be downloaded has been received. If the total data size of the received fragmented data for the file to be downloaded has not reached the file size (first file size) of the file to be downloaded, it can be considered that the file data for the file to be downloaded has not been received.

[0085] In this embodiment, by counting the size of the received fragmented data at the file receiving end, the situation where the file sending end has completed sending all the file data but the file receiving end has not received all the file data can be avoided due to data loss during the file sending process, thereby improving the reliability of the file receiving process.

[0086] Regarding whether the file data of the file to be downloaded has been completely written to the target file, in specific implementations, the main thread can determine whether the file data of the file to be downloaded has been completely written to the target file based on whether it receives the file write completion information sent by the child thread. In some implementations, the main thread can also detect whether the file data of the file to be downloaded has been completely written to the target file. Specifically, the main thread can determine whether the file data of the file to be downloaded has been completely written to the target file by detecting whether all file write tasks for the file to be downloaded in the task thread pool have been completed. If the main thread has received the file write completion information sent by the child thread, or if the main thread detects that all file write tasks for the file to be downloaded in the task thread pool have been completed, then the download of the file to be downloaded can be considered complete.

[0087] Step S106: Determine that the file data of the file to be downloaded has not been completely received, and return to step S101.

[0088] In this embodiment, if the file receiving end does not receive all the file data of the file to be downloaded, it can be considered that the file data of the file to be downloaded has not been received completely, and the process of executing steps S101 to S105 above can be returned.

[0089] Step S107: Confirm that the download of the file to be downloaded is complete.

[0090] In this embodiment, if the file receiving end has received all the file data of the file to be downloaded and all the file data has been completely written into the target file, then it can be determined that the download of the file to be downloaded is complete.

[0091] In the specific implementation process, if it is determined that the file data of the file to be downloaded has been received, but the file data of the file to be downloaded has not been completely written to the target file, it can be considered that the file to be downloaded has not been completely downloaded, and the file data of the file to be downloaded can be checked again after a preset time interval to see if the file data of the file to be downloaded has been completely written to the target file.

[0092] In summary, during the file download process, this application receives at least one fragment of the file to be downloaded from the file sender via a main thread. Based on the receiving order of each fragment, it generates file write tasks corresponding to each fragment, including the fragment data and write position information. These file write tasks are then placed into a task thread pool, which distributes them to at least one sub-thread. Each sub-thread writes the fragment data from its corresponding file write task to a target file stored on the local disk of the file receiver, according to the write position information within that task. In existing technologies, a single-threaded loop is used for reading and writing fragmented data, causing the main thread to handle the entire file download process, including file reading and writing. This affects the file receiver's response to other commands. In contrast, this application delegates the time-consuming file data writing process to a child thread in the task thread pool. This allows the main thread to respond to other processing requests during file writing, reducing the impact of the file download process, especially the download of large files, on the file receiver's response to other requests. It also avoids the file receiver being blocked (unable to respond to other processing requests) during file writing, thus improving the processing efficiency of the file receiver during file download.

[0093] In practical implementation, it is possible that the file data writing process for the file to be downloaded has been completed, but the actual target file obtained is abnormal, or the target file and the file to be downloaded are inconsistent. Based on this, in one embodiment, such as Figure 4 As shown in the figure, after step S107 above, the file download method provided in this application may further include the following steps S108 to S109.

[0094] Step S108: Determine whether the file data of the file to be downloaded has been successfully written to the target file. If the file data of the file to be downloaded has been successfully written to the target file, then execute the following step S110. If the file data of the file to be downloaded has not been successfully written to the target file, then execute the following step S109.

[0095] In this embodiment, after determining that the file to be downloaded has been downloaded (for example, the main thread detects that all file writing tasks for the file to be downloaded in the task thread pool have been completed), it can be further verified whether the file data of the file to be downloaded has been successfully written to the target file.

[0096] For example, in some implementations, it can be detected whether the file to be downloaded can be opened normally. Optionally, it can also be determined whether the size of the obtained target file is the same as the size of the file to be downloaded. In some implementations, to avoid errors in the location of writing fragmented data to the target file, resulting in the target file and the file to be downloaded having the same file data (same file size) but different file data locations, the main thread can also verify whether the locally stored target file is completely identical to the file to be downloaded. Specifically, the file sender can send the message digest of the file to be downloaded, such as the MD5 (Message-digest Algorithm 5) value, to the file receiver. Then, after determining that the file to be downloaded has been downloaded, the main thread calculates the MD5 value of the target file and determines whether the MD5 value of the target file is consistent with the MD5 value of the file to be downloaded. If the MD5 value of the target file is consistent with the MD5 value of the file to be downloaded, it can be considered that the file data of the file to be downloaded has been successfully written to the target file.

[0097] Step S109: Send a retransmission command for the file to be downloaded to the file sending end, and return to execute the above step S101.

[0098] In this embodiment, if the file data of the file to be downloaded is not successfully written to the target file, a retransmission instruction for the file to be downloaded can be sent to the file sending end, and then the steps S101 to S107 above will be re-executed, that is, the process of receiving the fragmented data of the file to be downloaded and writing the fragmented data to the target file will be re-performed.

[0099] Step S110: Determine that the file data of the file to be downloaded has been successfully written to the target file.

[0100] In this embodiment, by verifying whether the file data of the file to be downloaded has been successfully written to the target file, the successful download of the file can be ensured.

[0101] This application also provides a file download apparatus, which is used to perform the steps in any of the embodiments of the foregoing file download method. Specifically, please refer to... Figure 5 , Figure 5 This illustration shows a structural diagram of a file download device 500 provided in an embodiment of this application. The file download device 500 specifically includes a data receiving unit 501, a task generation unit 502, a data writing unit 503, and a completion unit 504.

[0102] The data receiving unit 501 is used to receive at least one fragment of the file to be downloaded sent by the file sending end through the main thread;

[0103] The task generation unit 502 is used to generate file writing tasks corresponding to each of the fragment data according to the receiving order of each fragment data; the file writing task includes the fragment data and writing position information.

[0104] The data writing unit 503 is used to put each of the file writing tasks into a task thread pool; distribute each of the file writing tasks to at least one sub-thread in the task thread pool; and for each sub-thread, write the fragmented data in the file writing task into a target file according to the write position information in the corresponding file writing task. The target file is stored in the local disk of the file receiving end, and the target file is used to store the file data of the file to be downloaded.

[0105] The completion unit 504 is used to, if it is determined that the file data of the file to be downloaded has not been completely received, return to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread; if it is determined that the file data of the file to be downloaded has been completely received and completely written to the target file, then determine that the download of the file to be downloaded is complete.

[0106] In some embodiments of this application, such as Figure 6 As shown, the file download device 500 further includes an acquisition unit 505, specifically used to acquire the first file size of the file to be downloaded through the main thread; the file download device further includes a judgment unit 506, specifically used to determine through the main thread whether the total data size of the received fragment data of the file to be downloaded has reached the first file size; if the total data size of the received fragment data of the file to be downloaded has reached the first file size, then it is determined that the file data of the file to be downloaded has been received; if the total data size of the received fragment data of the file to be downloaded has not reached the first file size, then it is determined that the file data of the file to be downloaded has not been received.

[0107] In some embodiments of this application, the judgment unit 506 may also be specifically used to determine whether the file data of the file to be downloaded has been successfully written to the target file; the completion unit 504 may also be specifically used to send a retransmission instruction for the file to be downloaded to the file sending end if the file data of the file to be downloaded has not been successfully written to the target file, and return to execute the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread.

[0108] In some embodiments of this application, the first sub-thread in the task thread pool that writes the fragmented data of the file writing task to the target file according to the write position information in the corresponding file writing task is a first sub-thread, and a sub-thread in the task thread pool that is not the first to write the fragmented data of the file writing task to the target file according to the write position information in the corresponding file writing task is a second sub-thread. The data writing unit 503 can also be specifically used to: if the sub-thread is the first sub-thread, open the target file through the first sub-thread; write the fragmented data to the target file according to the write position information; if the sub-thread is the second sub-thread, write the fragmented data to the target file according to the write position information; the judging unit 506 can also be specifically used to: judge whether the file to be downloaded has been completely written to the target file through the sub-thread; the file download device further includes a closing unit, specifically used to: if the file to be downloaded has been completely written to the target file, close the target file through the sub-thread.

[0109] In some embodiments of this application, the judgment unit 506 may also be specifically used to determine, through the sub-thread, whether the current download time of the file to be downloaded is greater than a preset download time threshold if the file to be downloaded has not been completely written to the target file; the file download device may also include a sending unit, used to send download timeout information to the main thread through the sub-thread if the current download time of the file to be downloaded is greater than the preset download time threshold.

[0110] In some embodiments of this application, the determination unit 506 may also be specifically used to determine whether the first file size of the file to be downloaded is the same as the second file size of the target file; if the first file size of the file to be downloaded is the same as the second file size of the target file, then it is determined that the file to be downloaded has been completely written into the target file; if the first file size of the file to be downloaded is not the same as the second file size of the target file, then it is determined that the file to be downloaded has not been completely written into the target file.

[0111] It should be noted that those skilled in the art can clearly understand that the specific implementation process of the above-mentioned file download device and each unit can be referred to the corresponding description in the foregoing method embodiments. For the sake of convenience and brevity, it will not be repeated here.

[0112] The aforementioned file download device can be implemented as a computer program, which can, for example... Figure 7 It runs on the computer equipment shown.

[0113] Please see Figure 7 , Figure 7 This is a schematic block diagram of a computer device provided in an embodiment of this application. The computer device 700 can be a smartphone, tablet computer, personal computer, smart wearable device, server, or other terminal device. See also... Figure 7 The computer device 700 includes a processor 702, a memory, and a network interface 705 connected via a device bus 701, wherein the memory may include a storage medium 703 and internal memory 704.

[0114] The storage medium 703 may store an operating system 7031 and a computer program 7032. When the computer program 7032 is executed, it causes the processor 702 to execute a file download method.

[0115] The processor 702 provides computing and control capabilities to support the operation of the entire computer device 700.

[0116] The internal memory 704 provides an environment for the execution of the computer program 7032 in the storage medium 703. When the computer program 7032 is executed by the processor 702, the processor 702 can execute the file download method.

[0117] This network interface 705 is used for network communication, such as providing data transmission. Those skilled in the art will understand that... Figure 7 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device 700 to which the present application is applied. The specific computer device 700 may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0118] The processor 702 is used to run the computer program 7032 stored in the memory to implement the file download method disclosed in the embodiments of this application.

[0119] Those skilled in the art will understand that Figure 7 The embodiments of the computer device shown do not constitute a limitation on the specific configuration of the computer device. In other embodiments, the computer device may include more or fewer components than illustrated, or combine certain components, or have different component arrangements. For example, in some embodiments, the computer device may include only memory and a processor. In such embodiments, the structure and function of the memory and processor are different from those shown. Figure 7 The embodiments shown are consistent and will not be described again here.

[0120] It should be understood that in the embodiments of this application, the processor 702 may be a central processing unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.

[0121] In another embodiment of this application, a computer-readable storage medium is provided. This computer-readable storage medium may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the file download method disclosed in the embodiments of this application.

[0122] Those skilled in the art will readily understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0123] In the embodiments provided in this application, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Units with the same function may be grouped into one unit. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or units, or it may be an electrical, mechanical, or other form of connection.

[0124] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of the embodiments of this application, depending on actual needs.

[0125] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0126] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a backend server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), magnetic disks, or optical disks.

[0127] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in this application, and these modifications or substitutions should all be covered within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A file download method, characterized in that, include: Receive at least one fragment of the file to be downloaded from the file sender via the main thread; A file writing task corresponding to each of the data segments is generated according to the receiving order of the data segments; the file writing task includes the data segments and writing position information. The task of writing each of the aforementioned files is placed into the task thread pool; The task thread pool distributes each file writing task to at least one sub-thread in the task thread pool. The task thread pool contains one or more sub-threads. After receiving a file writing task, the task thread pool stores the file writing task in a task queue and distributes the file writing task to one or more idle sub-threads in the task thread pool through the task queue. After each sub-thread receives a file writing task, it writes the fragmented data according to the file writing task. For each of the sub-threads, the segmented data in the file writing task is written to the target file according to the write position information in the corresponding file writing task. The target file is stored in the local disk of the file receiving end and is used to store the file data of the file to be downloaded. If it is determined that the file data of the file to be downloaded has not been completely received, then return to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread; If it is determined that the file data of the file to be downloaded has been received completely and written entirely to the target file, then it is determined that the download of the file to be downloaded is complete. The first sub-thread in the task thread pool that writes the fragmented data of the file writing task to the target file according to the write position information of the corresponding file writing task is the first sub-thread; the second sub-thread in the task thread pool that does not write the fragmented data of the file writing task to the target file according to the write position information of the corresponding file writing task is the second sub-thread; the step of writing the fragmented data of the file writing task to the target file according to the write position information of the corresponding file writing task includes: If the sub-thread is the first sub-thread, then the target file is opened through the first sub-thread; The fragmented data is written to the target file according to the write location information; If the sub-thread is the second sub-thread, then the fragmented data is written to the target file according to the write position information; After writing the fragmented data in the file writing task to the target file according to the write position information in the corresponding file writing task, the method further includes: The sub-thread determines whether the file to be downloaded has been completely written to the target file. If the file to be downloaded has been completely written to the target file, the target file is closed by the sub-thread; the closing of the target file is performed by the last sub-thread that wrote the fragmented data to the target file.

2. The method according to claim 1, characterized in that, Before receiving at least one fragment of the file to be downloaded sent by the file sender via the main thread, the method further includes: The main thread obtains the first file size of the file to be downloaded. If it is determined that the file data of the file to be downloaded has not been completely received, the method further includes: The main thread determines whether the total size of the received data segments of the file to be downloaded has reached the size of the first file. If the total data size of the received fragments of the file to be downloaded has reached the first file size, then it is determined that the file data reception of the file to be downloaded is complete. If the total size of the received fragments of data for the file to be downloaded does not reach the first file size, then it is determined that the file data of the file to be downloaded has not been completely received.

3. The method according to claim 1, characterized in that, After determining that the file to be downloaded has been downloaded completely, the method further includes: Determine whether the file data of the file to be downloaded has been successfully written to the target file; If the file data of the file to be downloaded is not successfully written to the target file, a retransmission instruction for the file to be downloaded is sent to the file sending end, and the process returns to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread.

4. The method according to claim 1, characterized in that, After determining whether the file to be downloaded has been completely written to the target file via the sub-thread, the method further includes: If the file to be downloaded is not completely written to the target file, the sub-thread determines whether the current download time of the file to be downloaded is greater than a preset download time threshold. If the current download time of the file to be downloaded is greater than the preset download time threshold, then the sub-thread sends a download timeout message to the main thread.

5. The method according to claim 1, characterized in that, The step of determining whether the file to be downloaded has been completely written to the target file through the sub-thread includes: Determine whether the first file size of the file to be downloaded is the same as the second file size of the target file; If the first file size of the file to be downloaded is the same as the second file size of the target file, then it is determined that the file to be downloaded has been completely written into the target file; If the first file size of the file to be downloaded is not the same as the second file size of the target file, then it is determined that the file to be downloaded has not been completely written into the target file.

6. The method according to any one of claims 1-5, characterized in that, The file receiving end is an in-vehicle computer, and the process of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread includes: The main thread receives at least one fragment of the file to be downloaded, sent by the file sender based on the IAP2 protocol.

7. A file download device, characterized in that, The device includes: The data receiving unit is used to receive at least one fragment of the file to be downloaded sent by the file sending end through the main thread; The task generation unit is used to generate file writing tasks corresponding to each of the data segments according to the receiving order of each data segment; the file writing task includes the data segments and writing position information. A data writing unit is used to place each file writing task into a task thread pool; distribute each file writing task to at least one sub-thread in the task thread pool; for each sub-thread, write the fragmented data in the file writing task to a target file according to the write position information in the corresponding file writing task, the target file is stored in the local disk of the file receiving end, and the target file is used to store the file data of the file to be downloaded; the task thread pool has one or more sub-threads, after the task thread pool receives a file writing task, it stores the file writing task in a task queue, and distributes the file writing task to one or more idle sub-threads in the task thread pool through the task queue, and each sub-thread writes the fragmented data according to the file writing task after receiving a file writing task; The completion unit is used to, if it is determined that the file data of the file to be downloaded has not been completely received, return to the step of receiving at least one fragment of the file to be downloaded sent by the file sending end through the main thread; if it is determined that the file data of the file to be downloaded has been completely received and completely written to the target file, then determine that the download of the file to be downloaded is complete. The first sub-thread in the task thread pool to write the fragmented data of the file writing task to the target file according to the write position information of the corresponding file writing task is the first sub-thread; the second sub-thread in the task thread pool that is not the first to write the fragmented data of the file writing task to the target file according to the write position information of the corresponding file writing task is the second sub-thread; the data writing unit is specifically used for: If the sub-thread is the first sub-thread, then the target file is opened through the first sub-thread; The fragmented data is written to the target file according to the write location information; If the sub-thread is the second sub-thread, then the fragmented data is written to the target file according to the write position information; After writing the fragmented data in the file writing task to the target file according to the write position information in the corresponding file writing task, the method further includes: The sub-thread determines whether the file to be downloaded has been completely written to the target file. If the file to be downloaded has been completely written to the target file, the target file is closed by the sub-thread; the closing of the target file is performed by the last sub-thread that wrote the fragmented data to the target file.

8. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method as described in any one of claims 1 to 6.

9. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method and system for merging data in server

    CN103678699A

  • Data exporting method and device, computer equipment and storage medium

    CN113609226A