File transmission method, device, electronic device and medium
By verifying and processing the hash calculation status of the fragments, the problem of obtaining the complete hash value in the fragmented parallel upload is solved, and the complete hash value of the file is quickly and synchronously obtained in the fragmented parallel upload, improving the file transfer. efficiency.
Patent Information
- Application Number
- CN202211289037.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-20
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2042-10-20
AI Technical Summary
During the file upload process, especially the fragmented parallel upload of large files, it becomes difficult to obtain the complete hash value of the file, because the upload order of the fragments is inconsistent with the original order, making it difficult for the server to simultaneously calculate the complete hash of the entire file. value.
By obtaining and verifying the hash calculation status of each fragment, it is determined whether the fragment is uploaded successfully and has a contextual relationship, and the complete hash value is derived from the hash calculation status of the last fragment to achieve parallel uploading of fragments compatible with complete hash. Obtaining the expected value.
It realizes the rapid acquisition of the complete hash value of the file in the case of fragmented parallel upload, improves the file transmission efficiency, meets the needs of synchronous calculation, and avoids waste of resources and delayed calculation.
Smart Images

Figure CN115665131B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a file transmission method, a file transmission device, an electronic device, and a computer-readable storage medium. Background Art
[0002] For file uploads, especially large files, the more common approach is to upload them in pieces in parallel. At the same time, in file management systems, hash values are often used to verify the integrity and credibility of files, and as fingerprints for identifying files. In many applications, the complete hash value of a file is required, and recalculating it generally takes a long time or more resources.
[0003] The complete hash value of a file needs to be generated by processing the file content using a hash algorithm. Generally, the hash value calculation process requires that the file content is input sequentially and continuously. The characteristics of the hash value determine that when the file content is fragmented, the hash value calculated for each fragment cannot derive the complete hash value of the entire file. This makes it very difficult to synchronously calculate the complete hash value of the entire file in the application scenario of file upload if the file is allowed to be uploaded in fragments and each fragment is allowed to be uploaded in parallel. This is because, in the parallel upload of fragments, the order of the fragments arriving at the server is not necessarily the same as the original order; and the upload of different fragments also occurs in the context of different requests, and the server cannot directly connect the hash calculation status of different requests. Summary of the invention
[0004] In view of the above problems, the embodiments of the present disclosure are proposed to provide a file transmission method and a corresponding file transmission device, an electronic device, and a computer-readable storage medium that overcome the above problems or at least partially solve the above problems.
[0005] The present disclosure embodiment discloses a file transmission method, which is applied to a server, and the method includes:
[0006] Obtaining each fragment of the same file uploaded in parallel by the client, and obtaining a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each fragment;
[0007] Verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status respectively corresponding to each of the slices;
[0008] When it is determined that all the fragments are uploaded successfully, using the first hash calculation state and the second hash calculation state respectively corresponding to each of the fragments to verify whether there is a contextual relationship between the fragments;
[0009] When it is determined that all the fragments have a contextual relationship, a complete hash value for the file is derived from a second hash calculation state corresponding to the last fragment of the file.
[0010] The present disclosure also discloses another file transmission method, which is applied to a client and includes:
[0011] Obtain a file to be transmitted, and divide the file into multiple fragments;
[0012] Perform hash calculations on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards;
[0013] The slices are uploaded to the server in parallel, and the first hash calculation state and the second hash calculation state corresponding to the slices are uploaded to the server, so that the server executes the above-mentioned file transfer method.
[0014] The present disclosure also discloses a file transmission device, which is applied to a server. The device includes:
[0015] An acquisition module, used to acquire each fragment of the same file uploaded in parallel by the client, and acquire a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each of the fragments;
[0016] A first verification module, used to verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status respectively corresponding to each of the slices;
[0017] A second verification module is used to verify whether there is a contextual relationship between the fragments by using the first hash calculation state and the second hash calculation state respectively corresponding to the fragments when it is determined that the fragments are uploaded successfully;
[0018] The export module is used to export the complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file when it is determined that each fragment has a contextual relationship.
[0019] The present disclosure also discloses another file transmission device, which is applied to a client, and the device includes:
[0020] An acquisition and segmentation module, used to acquire a file to be transmitted and segment the file into multiple segments;
[0021] A hash calculation module, used to perform hash calculation on each of the shards, to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards;
[0022] An upload module is used to upload each of the fragments to the server in parallel, and to upload the first hash calculation state and the second hash calculation state corresponding to each of the fragments to the server, so that the server uses the first hash calculation state and the second hash calculation state corresponding to each of the fragments to verify whether each of the fragments is successfully uploaded, and when it is determined that each of the fragments is successfully uploaded, use the first hash calculation state and the second hash calculation state corresponding to each of the fragments to verify whether there is a contextual relationship between each of the fragments, and when it is determined that each of the fragments has a contextual relationship, derive a complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file.
[0023] The embodiment of the present disclosure also discloses an electronic device, including: a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the above-mentioned file transmission method when executed by the processor.
[0024] The embodiment of the present disclosure further discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the file transmission method described above is implemented.
[0025] The disclosed embodiments include the following advantages:
[0026] In the disclosed embodiment, each fragment of the same file can be uploaded to the server in parallel, and the first hash calculation state before hash calculation and the second hash calculation state after hash calculation corresponding to each fragment can be obtained. When the first hash calculation state and the second hash calculation state corresponding to each fragment are used to verify that each fragment is successfully uploaded and each fragment has a contextual relationship, the complete hash value of the file can be derived from the second hash calculation state corresponding to the last fragment of the determined file. By adopting the above method, the first hash calculation state and the second hash calculation state corresponding to each fragment are used to verify whether each fragment is successfully uploaded and whether each fragment has a contextual relationship. In the case of successful verification, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last fragment of the file based on the hash function calculation characteristics and the determined contextual relationship between each fragment, so as to achieve compatible fragment parallel upload and obtain the complete hash value of the entire file, and meet the demand for quickly obtaining the complete hash value of the file while ensuring the efficiency of file transmission through fragment parallel upload. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 is a flowchart of a file transmission method provided by an embodiment of the present disclosure;
[0028] Figure 2 is a flowchart of another file transmission method provided by an embodiment of the present disclosure;
[0029] Figure 3 It is a schematic diagram of a process of performing hash calculation on a shard in a server according to an embodiment of the present disclosure;
[0030] Figure 4 It is a schematic diagram of a process of verifying the contextual relationship of each shard and outputting a complete hash value of a file in a server according to an embodiment of the present disclosure;
[0031] Figure 5 is a flowchart of another method for transferring files provided by an embodiment of the present disclosure;
[0032] Figure 6 It is a schematic diagram of a process of performing hash calculation on a shard in a client in an embodiment of the present disclosure;
[0033] Figure 7 is a structural diagram of a file transmission system provided by an embodiment of the present disclosure;
[0034] Figure 8 is a structural block diagram of a file transmission device provided by an embodiment of the present disclosure;
[0035] Fig. 9It is a structural block diagram of another file transmission device provided by an embodiment of the present disclosure. DETAILED DESCRIPTION
[0036] In order to make the above-mentioned purposes, features and advantages of the present disclosure more obvious and easy to understand, the present disclosure is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field belong to the scope of protection of the present disclosure.
[0037] Sharding means dividing a file into multiple segments. Splitting a file into segments and uploading them separately can increase connection concurrency and ultimately increase upload speed.
[0038] Multi-part parallel upload means dividing a file into multiple parts. Multiple parts can initiate upload requests at the same time at each moment. Multi-part parallel upload is to improve transmission efficiency.
[0039] The hash value refers to the calculation result of the hash function.
[0040] The hash calculation state refers to the internal state of the hash function, that is, the set of internal variables of the hash function. The hash calculation state evolves with the input of data, and the final hash calculation result is also derived from the hash calculation state.
[0041] The existing file upload solutions are as follows:
[0042] Solution 1 is to wait for the file to be fully uploaded and then re-read the file to calculate the complete hash value. This process can be implemented asynchronously, thereby reducing the request consumption time. That is, after waiting for each shard to be uploaded, an asynchronous process is triggered to read each shard sequentially, so that the entire file bytes can be read sequentially and the complete hash value of the file can be calculated.
[0043] Solution 2 is to calculate the hash value of each shard instead of the complete hash value of the file. Because the bytes in each shard are uploaded sequentially during the upload process of each shard, the process of calculating the hash value of the shard can be completed during the upload process of the shard.
[0044] Solution 3 is to upload the shards sequentially. When uploading shards sequentially, the hash calculation state of the current shard can be evolved based on the hash calculation state of the previous shard and the byte stream of the current shard, and finally the complete hash value of the file is calculated.
[0045] The above file upload method has the following shortcomings:
[0046] Solution 1 can achieve the intended goal, but this approach still wastes server processing resources, and the complete hash value of the file is calculated with a delay, which cannot meet business scenarios that require immediate access to the complete hash value of the file.
[0047] Solution 2 is a method that sacrifices business. Because the hash value of each shard cannot derive the complete hash value of the entire file, this method cannot meet the requirements when the complete hash value of the file is required. This method is only applicable to scenarios where the integrity of the file during the upload process is verified.
[0048] Solution 3 requires sequential uploading of fragments, so the uploading process can only be performed in a single thread and single connection, which is easily restricted by the transmission speed in the existing network environment.
[0049] Based on this, the present disclosure relates to realizing synchronous calculation of the complete hash value of the entire file while realizing parallel upload of the file in segments, and is compatible with parallel upload of the segments and fast acquisition of the complete hash value of the file.
[0050] In the disclosed embodiment, each fragment of the same file can be uploaded to the server in parallel, and the first hash calculation state before hash calculation and the second hash calculation state after hash calculation corresponding to each fragment can be obtained. When the first hash calculation state and the second hash calculation state corresponding to each fragment are used to verify that each fragment is successfully uploaded and each fragment has a contextual relationship, the complete hash value of the file can be derived from the second hash calculation state corresponding to the last fragment of the determined file. By adopting the above method, the first hash calculation state and the second hash calculation state corresponding to each fragment are used to verify whether each fragment is successfully uploaded and whether each fragment has a contextual relationship. In the case of successful verification, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last fragment of the file based on the hash function calculation characteristics and the determined contextual relationship between each fragment, so as to achieve compatible fragment parallel upload and obtain the complete hash value of the entire file, and meet the demand for quickly obtaining the complete hash value of the file while ensuring the efficiency of file transmission through fragment parallel upload.
[0051] Reference Figure 1 , shows a flowchart of a file transmission method provided by an embodiment of the present disclosure, which is applied to a server and may specifically include the following steps:
[0052] Step 101, obtaining each fragment of the same file uploaded in parallel by the client, and obtaining a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each fragment.
[0053] A file transmission method provided by an embodiment of the present disclosure can be applied to a server, and the server can interact with a client.
[0054] After the file to be transmitted is obtained in the client, the file can be divided into multiple fragments. The client can perform a hash calculation process while uploading the fragments, and perform hash calculations on each fragment separately. For any of the fragments, the first hash calculation state of the fragment before the hash calculation can be recorded, and the second hash calculation state of the fragment after the hash calculation can be recorded, so that the first hash calculation state before the hash calculation and the second hash calculation state after the hash calculation corresponding to each fragment can be obtained.
[0055] When the client uploads each shard in parallel, it can simultaneously upload the first hash calculation state before hash calculation and the second hash calculation state after hash calculation corresponding to each shard to the server. As an example, for any shard, the client uploads the hash calculation state before and after hash calculation of the shard while uploading the byte data in the shard.
[0056] The server may obtain the various fragments of the same file uploaded in parallel by the client, and obtain the first hash calculation state before the hash calculation and the second hash calculation state after the hash calculation corresponding to each fragment.
[0057] Step 102: Use the first hash calculation status and the second hash calculation status corresponding to each of the slices to verify whether each of the slices is uploaded successfully.
[0058] In the disclosed embodiment, the first hash calculation state and the second hash calculation state corresponding to each shard can be used to verify whether each shard is uploaded successfully. For any shard, the first hash calculation state and the second hash calculation state corresponding to the shard can be used to verify whether the shard is uploaded successfully.
[0059] Step 103: When it is determined that all the fragments are uploaded successfully, the first hash calculation state and the second hash calculation state respectively corresponding to each of the fragments are used to verify whether there is a contextual relationship between the fragments.
[0060] When it is determined that all the segments have been uploaded successfully, the first hash calculation state and the second hash calculation state corresponding to each segment can be used to verify whether there is a contextual relationship between the segments. The contextual relationship refers to the connection between the two segments in the file, for example, one segment is the previous content of another segment, or one segment is the following content of another segment. By splicing the segments according to the contextual relationship of each segment, the complete original file can be obtained.
[0061] Step 104: When it is determined that all the fragments have a contextual relationship, derive a complete hash value for the file from a second hash calculation state corresponding to the last fragment of the file.
[0062] When it is determined that each fragment has a contextual relationship, the last fragment of the file can be determined from the multiple fragments, and the complete hash value for the file can be derived from the second hash calculation state corresponding to the last fragment. The complete hash value of the file refers to the calculation result obtained by inputting the file content into the hash function in sequence and continuously, and in the embodiment of the present disclosure, the complete hash value of the file can be obtained without inputting the complete file content of the entire file into the hash function in sequence and continuously, which can save the calculation time and calculation resources for calculating the complete hash value of the file.
[0063] As an example, suppose a file includes four fragments, namely fragment A, fragment B, fragment C and fragment D. There is a context relationship between each fragment. Fragment A is the previous content of fragment B, fragment B is the previous content of fragment C, and fragment C is the previous content of fragment D. Then it can be determined that fragment D is the last fragment of the file, and the complete hash value for the file can be derived from the second hash calculation state corresponding to fragment D.
[0064] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0065] Reference Figure 2 , shows a flowchart of another file transmission method provided by an embodiment of the present disclosure, which is applied to a server and may specifically include the following steps:
[0066] Step 201, obtaining each fragment of the same file uploaded in parallel by the client, and obtaining a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each fragment.
[0067] In the embodiment of the present disclosure, each shard has a corresponding first hash calculation state before hash calculation and a second hash calculation state after hash calculation. When the server obtains the byte data corresponding to each shard, it obtains the first hash calculation state and the second hash calculation state corresponding to each shard.
[0068] Step 202: Use the first hash calculation status and the second hash calculation status corresponding to each of the slices to verify whether each of the slices is uploaded successfully.
[0069] In an optional embodiment of the present disclosure, step 202 uses the first hash calculation status and the second hash calculation status corresponding to each of the slices to verify whether each of the slices is uploaded successfully, which may specifically include the following sub-steps:
[0070] Sub-step S11, for any of the shards, set the current internal state of the hash function of the server to the first hash calculation state corresponding to the shard, call the Update method in the hash function to receive the byte data corresponding to the shard for hash calculation, and determine the current internal state of the hash function after the calculation is completed as the third hash calculation state corresponding to the shard. If the second hash calculation state corresponding to the shard is consistent with the third hash calculation state, it is determined that the shard has been uploaded successfully.
[0071] The server can perform a hash calculation process while receiving the shards, and perform hash calculation on each shard separately. That is, in the embodiment of the present disclosure, for any shard, at least one hash calculation needs to be performed on the client and the server respectively.
[0072] When the server obtains the various segments of the same file uploaded in parallel, and obtains the first hash calculation state and the second hash calculation state corresponding to each segment, for any segment, the current internal state of the hash function preset in the server can be reset to the first hash calculation state declared by the client before the hash calculation is performed on the segment, and the byte data corresponding to the segment is put into the hash function for hash calculation, and the current internal state of the hash function after the calculation is completed is determined as the third hash calculation state corresponding to the segment, and it is verified whether the third hash calculation state is consistent with the second hash calculation state declared by the client after the hash calculation is performed on the segment. If the verification is consistent, it can be determined that the segment has been uploaded successfully; if the verification is inconsistent, it can be determined that the upload of the segment has failed.
[0073] There can be one or more hash functions preset in the server. When the hash function is running, the internal state of the hash function is limited and can be represented by a byte array S.
[0074] The calculation process of the hash function can be broken down into three methods: Init, Update, and Finalize. Their specific functions include:
[0075] Init is used to initialize the internal state of the hash function;
[0076] Update is used to accept data, perform hash calculations, and update the internal state of the hash function;
[0077] Finalize is used to derive the hash calculation result from the internal state of the hash function.
[0078] The specific implementation of these three methods is different for different hash functions. In practice, open source implementations are generally used directly. Generally, open source implementations of hash functions do not directly expose the internal state S, but in fact, as long as it is simply modified, its internal state S can be read and written. For example, for MD5 (Message Digest Algorithm MD5, message digest algorithm), its internal state is represented by 4 32-bit integers. Generally, open source implementations will define it as 4 integer variables internally, and additional methods can be added to read and write it.
[0079] After the hash function is decomposed, the synchronous calculation of the hash value in the shard parallel upload can be achieved by maintaining the internal state of the hash function. The decomposition of the hash function and the reading and writing of the internal state are the basis of the embodiments of the present disclosure.
[0080] Therefore, the byte data corresponding to the shard is put into the hash function for hash calculation, and the current internal state of the hash function after the calculation is completed is determined as the third hash calculation state corresponding to the shard. Specifically, the Update method in the hash function can be called to receive the byte data corresponding to the shard for hash calculation, and the current internal state of the hash function after the calculation is determined as the third hash calculation state corresponding to the shard.
[0081] Reference Figure 3 FIG. 1 is a schematic diagram of a process of performing hash calculation on a shard in a server according to an embodiment of the present disclosure. The specific process includes:
[0082] 1. Receive the client request, obtain the byte data of each shard, and the internal state of the hash function before the shard is hashed, i.e., the first hash calculation state, and the internal state of the hash function after the shard is hashed, i.e., the second hash calculation state. For the kth shard, the first hash calculation state corresponding to the shard is S ckS , the corresponding second hash calculation state is S ckE .
[0083] 2. When performing hash calculation on the kth shard, the internal state S of the hash function in the server can be set to the first hash calculation state S of the shard. ckS , and call the Update method to receive the byte data of the kth shard for hash calculation. At the same time, the byte data corresponding to the shard can be written to the file system, and the internal state of the server after the calculation is completed is used as the third hash calculation state corresponding to the shard, and S kE express.
[0084] 3. Compare S ckE and S kE The value of .
[0085] 4. If S ckE and S kE If they are not equal, the multipart upload is considered to have failed.
[0086] 5. If S ckE and S kE If they are equal, it means that the hash calculation process of the client and the server is consistent, and the upload of the fragment is considered successful. At this time, S ckS and S ckE Recorded in the database.
[0087] Step 203, when it is determined that all the fragments are uploaded successfully, traverse the fragments in order. For the next fragment, if the first hash calculation state corresponding to the fragment is consistent with the second hash calculation state corresponding to the previous fragment, it is determined that there is a contextual relationship between the fragment and the previous fragment.
[0088] Only when it is confirmed that all shards have been uploaded successfully, the first hash calculation state and the second hash calculation state corresponding to each shard can be used to verify whether there is a contextual relationship between the shards; as long as one of the shards fails to be uploaded, the contextual relationship verification operation cannot be performed.
[0089] In an optional embodiment of the present disclosure, each shard has a corresponding shard identifier, and each shard identifier has a corresponding identifier sequence. Each shard can be traversed in the identifier sequence, and for the next shard, if the first hash calculation state corresponding to the shard is consistent with the second hash calculation state corresponding to the previous shard, it is determined that the shard has a contextual relationship with the previous shard, and the shard is the content of the previous shard; otherwise, it is determined that the shard has no contextual relationship with the previous shard.
[0090] In an optional embodiment of the present disclosure, before step 203, the following steps may be specifically included:
[0091] Call the Init method in the hash function to initialize the current internal state of the hash function to the initial state; for one of the shards, if the first hash calculation state corresponding to the shard is consistent with the initial state, determine that the shard is the first shard of the file.
[0092] The Init method in the hash function can initialize the internal state of the hash function to the initial state designed by the algorithm.
[0093] In the disclosed embodiment, the server may call the Init method in the hash function to initialize the current internal state of the hash function to the initial state, and then for the shard whose identification order is the first order, if the first hash calculation state corresponding to the shard is consistent with the initial state after initialization, the shard is determined to be the first shard of the original file.
[0094] Step 204: When it is determined that all the fragments have a contextual relationship, a complete hash value for the file is derived from a second hash calculation state corresponding to the last fragment of the file.
[0095] In the embodiment of the present disclosure, when it is determined that each shard has a contextual relationship, the complete hash value for the file can be derived from the second hash calculation state corresponding to the last shard of the file. The complete hash value of the file can be obtained without inputting the complete file content of the entire file into the hash function in sequence, which can save computing time and computing resources for calculating the complete hash value of the file.
[0096] In an optional embodiment of the present disclosure, step 204 derives a complete hash value for the file from the second hash calculation state corresponding to the last shard of the file, which may specifically include the following sub-steps:
[0097] Sub-step S21, calling the Finalize method in the hash function to derive a complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file, so as to determine that the file is uploaded successfully.
[0098] In an embodiment of the present disclosure, the server can call the Finalize method in the hash function to derive a complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file. At this time, the file can be marked as uploaded successfully. Furthermore, a message indicating that the file has been uploaded successfully can be transmitted to the client to form a closed loop of file transfer.
[0099] Reference Figure 4 FIG. 1 is a flow chart of verifying the contextual relationship of each shard and outputting the complete hash value of the file in a server according to an embodiment of the present disclosure. The specific flow includes:
[0100] 1. Call the Init method of the hash function and set the internal state of the hash function of the server to the initial state S 1S .
[0101] 2. Compare S 1S The first hash calculation state S corresponding to the shard whose identification order is the first order c1S .
[0102] 3. If S 1S and S c1S If they are not equal, the file upload is considered to have failed.
[0103] 4. If S 1S and S c1S If they are equal, then set k=2.
[0104] 5. Compare the first hash calculation state S of the kth shard ckS and the second hash calculation state S of the previous shard c(k-1)E .
[0105] 6. If S ckS and S c(k-1)E If they are not equal, the file upload is considered to have failed.
[0106] 7. If S ckS and S c(k-1)E If they are equal, continue processing the next shard.
[0107] 8. Let the total number of shards be k c , compare k and k c .
[0108] 9. If k <k c , then set k=k+1 and return to step 5.
[0109] 10. If k>=k c , then set the internal state of the hash function to S ckE (k=k c ), at this time, the Finalize method of the hash function is called, and the obtained hash value is the complete hash value of the entire file, and the file upload is marked successful.
[0110] Among them, the failure of file upload is generally caused by incorrect implementation of the hash algorithm or unexpected operation of the client. Generally, upload based on TCP (Transmission Control Protocol) has provided quality assurance and can basically exclude transmission errors. In the case of upload failure, the client can be asked to correct and retransmit.
[0111] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0112] In the disclosed embodiments, the hash calculation process of each shard can be parallelized by disassembling the initialization and calculation process of the hash function; the connection of the hash function context of each shard is achieved through the cooperation of the client; and the hash calculation context is verified by the server to avoid problems in the client hash calculation implementation or hash calculation cheating.
[0113] In the present disclosure, by decomposing the hash calculation process and letting the client and server cooperate in the hash calculation, the synchronous calculation of the hash value in the sharded parallel upload is realized. The calculated hash value is verified by the server and is reliable; and the calculation process is synchronous and does not require backtracking. In this way, the dual needs of the upload efficiency of the file management system and the hash extraction business are met.
[0114] The above embodiment describes a file transmission method provided by the present disclosure from the perspective of a server, and the following embodiment describes a file transmission method provided by the present disclosure from the perspective of a client. It should be noted that, in order to avoid repetition, the various embodiments and the features in the various embodiments can refer to each other, and in addition, the various embodiments and the features in the various embodiments can be combined with each other in the absence of conflict.
[0115] Reference Figure 5 , shows a flowchart of the steps of another file transmission method provided by an embodiment of the present disclosure, which is applied to a client and may specifically include the following steps:
[0116] Step 501: obtain a file to be transmitted, and divide the file into multiple segments.
[0117] The client obtains the file to be transmitted and can divide the file into multiple slices. In an example, the file can be evenly divided into multiple slices. The embodiment of the present disclosure does not specifically limit the file division method.
[0118] Step 502: Perform hash calculations on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards.
[0119] In the embodiment of the present disclosure, hash calculation may be performed on each shard respectively to obtain a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each shard.
[0120] In an optional embodiment of the present disclosure, step 502 performs hash calculation on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards, which may specifically include the following sub-steps:
[0121] Sub-step S31, scan each of the shards in sequence, for any of the shards, determine the current internal state of the hash function before hash calculation for the shard as the first hash calculation state corresponding to the shard, call the Update method in the hash function to receive the byte data corresponding to the shard for hash calculation, and determine the current internal state of the hash function after the calculation is completed as the second hash calculation state corresponding to the shard.
[0122] In the embodiment of the present disclosure, each shard may be scanned in sequence to determine the first hash calculation state and the second hash calculation state corresponding to each shard.
[0123] Step 503, upload each of the fragments to the server in parallel, and upload the first hash calculation state and the second hash calculation state corresponding to each of the fragments to the server, so that the server uses the first hash calculation state and the second hash calculation state corresponding to each of the fragments to verify whether each of the fragments is successfully uploaded, and when it is determined that each of the fragments is successfully uploaded, use the first hash calculation state and the second hash calculation state corresponding to each of the fragments to verify whether there is a contextual relationship between each of the fragments, and when it is determined that each of the fragments has a contextual relationship, derive a complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file.
[0124] Each fragment is uploaded to the server in parallel. The specific process of processing the fragments in the server can refer to the above embodiment, which will not be repeated here.
[0125] Reference Figure 6 FIG. 1 is a schematic diagram of a process of performing hash calculation on a shard in a client according to an embodiment of the present disclosure, and the specific process includes:
[0126] 1. Call the Init method of the hash function and set the internal state of the client hash function to the initial state.
[0127] 2. The client initializes the loop variable k = 1. The client scans the shards in order, from the first shard to the last shard. Let the total number of shards be k. c .
[0128] 3. The client reads the kth shard and first reads the internal state of the hash function at this time as the first hash calculation state S ckS Then call the Update function to put the byte data of the kth shard into the hash calculation. After the calculation is completed, read the internal state of the hash function as the second hash calculation state S ckE .
[0129] 4. Initiate the upload of the fragment bytes of the kth fragment, and upload the corresponding S of the fragment at the same time ckS and S ckE Each segment is uploaded in parallel, and there is no need to wait for the previous segment to be uploaded before uploading the next segment. The process of uploading segment byte data is the same as uploading the corresponding S ckS and S ckE The process can be asynchronous.
[0130] 5. Determine whether k is equal to k c .
[0131] 6. If k <k c , indicating that there are still segments that have not been uploaded, set k = k + 1, and return to step 3 to initiate the upload of the next segment.
[0132] 7. If k>=k c , indicating that all segments have been uploaded. At this time, you need to wait until all segments are uploaded. This is a blocking operation, and step 8 can only be executed after all segments are uploaded.
[0133] 8. Call the server to output the complete hash value of the file and obtain the final processing result, which is the complete hash value of the file, indicating that the file upload is complete.
[0134] It should be noted that the design of the client processing process implicitly assumes that the speed at which the CPU (central processing unit) processes file content is much faster than the disk read speed, and the disk read speed is much faster than the network transmission speed.
[0135] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0136] Reference Figure 7 , shows a schematic diagram of the structure of a file transmission system provided by an embodiment of the present disclosure, the file transmission system 700 includes a server and a client, in the scenario of uploading files, the client is the party submitting the file, and the server is the party receiving the file. The server includes an acquisition module 7001, a first verification module 7002, a second verification module 7003 and an export module 7004; the client includes an acquisition and segmentation module 7005, a hash calculation module 7006 and an upload module 7007.
[0137] The operation of each module in the file transfer system is as follows:
[0138] The acquisition and segmentation module 7005 is used to acquire the file to be transmitted and segment the file into multiple segments;
[0139] A hash calculation module 7006 is used to perform hash calculation on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards;
[0140] The uploading module 7007 is used to upload the slices to the server in parallel, and upload the first hash calculation state and the second hash calculation state corresponding to the slices to the server;
[0141] The acquisition module 7001 is used to acquire the fragments of the same file uploaded in parallel by the client, and acquire the first hash calculation state before hash calculation and the second hash calculation state after hash calculation corresponding to each of the fragments;
[0142] The first verification module 7002 is used to verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status corresponding to each of the slices;
[0143] The second verification module 7003 is used to verify whether there is a contextual relationship between the fragments by using the first hash calculation state and the second hash calculation state respectively corresponding to the fragments when it is determined that the fragments are uploaded successfully;
[0144] The export module 7004 is used to export the complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file when it is determined that all the fragments have a contextual relationship.
[0145] It should be noted that, in order to avoid repetition, only a brief description of the system embodiment is given here, and the specific implementation process of the system can refer to the process described from the perspective of the server or the client.
[0146] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0147] It should be noted that, for the method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present disclosure are not limited by the order of the actions described, because according to the embodiments of the present disclosure, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present disclosure.
[0148] Reference Figure 8 , shows a structural block diagram of a file transmission device provided by an embodiment of the present disclosure, which is applied to a server and may specifically include the following modules:
[0149] The acquisition module 801 is used to acquire the fragments of the same file uploaded in parallel by the client, and acquire the first hash calculation state before hash calculation and the second hash calculation state after hash calculation corresponding to each of the fragments;
[0150] A first verification module 802 is used to verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status corresponding to each of the slices;
[0151] The second verification module 803 is used to verify whether there is a contextual relationship between the fragments by using the first hash calculation state and the second hash calculation state respectively corresponding to the fragments when it is determined that the fragments are uploaded successfully;
[0152] The export module 804 is used to export the complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file when it is determined that all the fragments have a contextual relationship.
[0153] In the embodiment of the present disclosure, the first verification module includes:
[0154] The first verification submodule is used to set the current internal state of the hash function of the server to the first hash calculation state corresponding to the slice for any of the slices, and call the Update method in the hash function to receive the byte data corresponding to the slice for hash calculation, and determine the current internal state of the hash function after the calculation is completed as the third hash calculation state corresponding to the slice; if the second hash calculation state corresponding to the slice is consistent with the third hash calculation state, it is determined that the slice has been uploaded successfully.
[0155] In the embodiment of the present disclosure, the second verification module includes:
[0156] The second verification submodule is used to traverse each of the shards in order. For the next shard, if the first hash calculation state corresponding to the shard is consistent with the second hash calculation state corresponding to the previous shard, it is determined that there is a contextual relationship between the shard and the previous shard.
[0157] In the embodiment of the present disclosure, the second verification module further includes:
[0158] An initialization submodule, used to call the Init method in the hash function to initialize the current internal state of the hash function to an initial state;
[0159] The third verification submodule is used to determine, for one of the fragments, that the fragment is the first fragment of the file if the first hash calculation state corresponding to the fragment is consistent with the initial state.
[0160] In the embodiment of the present disclosure, the export module includes:
[0161] The export submodule is used to call the Finalize method in the hash function to export the complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file to determine that the file is uploaded successfully.
[0162] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0163] Reference Fig. 9 , shows a structural block diagram of another file transmission device provided by an embodiment of the present disclosure, which is applied to a client and may specifically include the following modules:
[0164] The acquisition and segmentation module 901 is used to acquire the file to be transmitted and segment the file into multiple segments;
[0165] A hash calculation module 902 is used to perform hash calculation on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards;
[0166] The upload module 903 is used to upload each of the fragments to the server in parallel, and upload the first hash calculation state and the second hash calculation state corresponding to each of the fragments to the server, so that the server uses the first hash calculation state and the second hash calculation state corresponding to each of the fragments to verify whether each of the fragments is successfully uploaded, and when it is determined that each of the fragments is successfully uploaded, the first hash calculation state and the second hash calculation state corresponding to each of the fragments are used to verify whether there is a context relationship between each of the fragments, and when it is determined that each of the fragments has a context relationship, a complete hash value for the file is derived from the second hash calculation state corresponding to the last fragment of the file.
[0167] In the embodiment of the present disclosure, the hash calculation module includes:
[0168] The hash calculation submodule is used to scan each of the shards in sequence. For any shard, the current internal state of the hash function before the hash calculation is performed on the shard is determined as the first hash calculation state corresponding to the shard, and the Update method in the hash function is called to receive the byte data corresponding to the shard for hash calculation, and the current internal state of the hash function after the calculation is completed is determined as the second hash calculation state corresponding to the shard.
[0169] In summary, in the embodiments of the present disclosure, the first hash calculation state and the second hash calculation state corresponding to each shard are used to verify whether each shard is uploaded successfully and whether there is a contextual relationship between each shard. When the verification is successful, the complete hash value of the entire file can be directly derived from the second hash calculation state corresponding to the last shard of the file based on the hash function calculation characteristics and the determined contextual relationship between the shards, thereby achieving compatible parallel uploading of shards and obtaining the complete hash value of the entire file, thereby ensuring file transmission efficiency through parallel uploading of shards and meeting the demand for quickly obtaining the complete hash value of the file.
[0170] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0171] An embodiment of the present disclosure also provides an electronic device, including: a processor, a memory, and a computer program stored in the memory and capable of running on the processor. When the computer program is executed by the processor, the various processes of the above-mentioned file transfer method embodiment are implemented and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0172] The embodiment of the present disclosure also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the various processes of the above-mentioned file transfer method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0173] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0174] Those skilled in the art will appreciate that the embodiments of the disclosed embodiments may be provided as methods, devices, or computer program products. Therefore, the disclosed embodiments may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the disclosed embodiments may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0175] The embodiments of the present disclosure are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present disclosure. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0176] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0177] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0178] Although the preferred embodiments of the present disclosure have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present disclosure.
[0179] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0180] The above is a detailed introduction to a file transfer method and a file transfer device, an electronic device and a computer-readable storage medium provided by the present disclosure. Specific examples are used in this article to illustrate the principles and implementation methods of the present disclosure. The description of the above embodiments is only used to help understand the method of the present disclosure and its core idea. At the same time, for a person skilled in the art, according to the idea of the present disclosure, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present disclosure.
Claims
1. A file transmission method, characterized in that: Applied to a server, the method comprises: Obtaining each fragment of the same file uploaded in parallel by the client, and obtaining a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each fragment; Verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status respectively corresponding to each of the slices; When it is determined that all the fragments are uploaded successfully, using the first hash calculation state and the second hash calculation state respectively corresponding to each of the fragments to verify whether there is a contextual relationship between the fragments; When it is determined that all the fragments have a contextual relationship, deriving a complete hash value for the file from a second hash calculation state corresponding to the last fragment of the file; The using the first hash calculation status and the second hash calculation status corresponding to each of the slices to verify whether each of the slices is successfully uploaded includes: For any of the shards, the current internal state of the hash function of the server is set to the first hash calculation state corresponding to the shard, and the Update method in the hash function is called to receive the byte data corresponding to the shard for hash calculation, and the current internal state of the hash function after the calculation is completed is determined as the third hash calculation state corresponding to the shard. If the second hash calculation state corresponding to the shard is consistent with the third hash calculation state, it is determined that the shard has been uploaded successfully.
2. The method according to claim 1, characterized in that: The step of using the first hash calculation state and the second hash calculation state respectively corresponding to each of the shards to verify whether there is a contextual relationship between the shards includes: Each of the shards is traversed in order. For the next shard, if the first hash calculation state corresponding to the shard is consistent with the second hash calculation state corresponding to the previous shard, it is determined that there is a contextual relationship between the shard and the previous shard.
3. The method according to claim 2, characterized in that For the next shard, if the first hash calculation state corresponding to the shard is consistent with the second hash calculation state corresponding to the previous shard, before determining that the shard has a contextual relationship with the previous shard, the method further includes: Calling the Init method in the hash function to initialize the current internal state of the hash function to an initial state; For one of the shards, if the first hash calculation state corresponding to the shard is consistent with the initial state, the shard is determined to be the first shard of the file.
4. The method according to claim 1, characterized in that: The deriving a complete hash value for the file from a second hash calculation state corresponding to the last shard of the file includes: The Finalize method in the hash function is called to derive a complete hash value for the file from the second hash calculation state corresponding to the last fragment of the file to determine that the file has been uploaded successfully.
5. A file transmission method, characterized in that: Applied to a client, the method comprises: Obtain a file to be transmitted, and divide the file into multiple fragments; Perform hash calculations on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards; Upload each of the shards to the server in parallel, and upload the first hash calculation state and the second hash calculation state corresponding to each of the shards to the server, so that the server executes a file transfer method as described in any one of claims 1-4.
6. The method according to claim 5, characterized in that The performing hash calculation on each of the shards to obtain a first hash calculation state before the hash calculation and a second hash calculation state after the hash calculation corresponding to each of the shards, respectively, includes: Scan each of the shards in sequence. For any shard, determine the current internal state of the hash function before hash calculation for the shard as the first hash calculation state corresponding to the shard, call the Update method in the hash function to receive the byte data corresponding to the shard for hash calculation, and determine the current internal state of the hash function after the calculation is completed as the second hash calculation state corresponding to the shard.
7. A file transmission device, characterized in that: Applied to a server, the device comprises: An acquisition module, used to acquire each fragment of the same file uploaded in parallel by the client, and acquire a first hash calculation state before hash calculation and a second hash calculation state after hash calculation corresponding to each of the fragments; A first verification module, used to verify whether each of the slices is uploaded successfully by using the first hash calculation status and the second hash calculation status respectively corresponding to each of the slices; A second verification module is used to verify whether there is a contextual relationship between the fragments by using the first hash calculation state and the second hash calculation state respectively corresponding to the fragments when it is determined that the fragments are uploaded successfully; An export module, configured to export a complete hash value for the file from a second hash calculation state corresponding to the last fragment of the file when it is determined that all the fragments have a contextual relationship; The first verification module includes: The first verification submodule is used to set the current internal state of the hash function of the server to the first hash calculation state corresponding to the slice for any of the slices, and call the Update method in the hash function to receive the byte data corresponding to the slice for hash calculation, and determine the current internal state of the hash function after the calculation is completed as the third hash calculation state corresponding to the slice; if the second hash calculation state corresponding to the slice is consistent with the third hash calculation state, it is determined that the slice has been uploaded successfully.
8. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein when the computer program is executed by the processor, a file transmission method as described in any one of claims 1-4 or 5-6 is implemented.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements a file transmission method as described in any one of claims 1-4 or 5-6.
