Disclosure of Invention
Based on this, it is necessary to provide a file transfer method, apparatus, storage medium and device for network bandwidth, aiming at the defect that the file transfer speed is slow under the condition that the network bandwidth is limited.
In a first aspect, an embodiment of the present invention provides a method for transmitting a file for a network bandwidth, where the method includes:
Compressing a source file to be transmitted in a target compression mode to obtain a compressed file;
After the compressed file is used as a file to be transmitted, dividing a data block corresponding to the file to be transmitted into a plurality of sub-data blocks to be transmitted according to a preset size;
determining whether a first token greater than or equal to a first preset threshold exists in the token bucket;
and transmitting the sub-data block to be transmitted to a corresponding destination terminal under the condition that the first token greater than or equal to the first preset threshold value exists in the token bucket and any one of the plurality of sub-data blocks to be transmitted applies for and acquires the corresponding first token.
Optionally, the method further comprises:
waiting and generating second tokens to store all second tokens in the token bucket in response to tokens in the token bucket having been exhausted;
Determining whether the second token greater than or equal to a second preset threshold exists in the token bucket;
and applying for and acquiring a corresponding second token from any one of the plurality of sub-data blocks to be transmitted, which are larger than or equal to the second preset threshold, in the token bucket, and transmitting the sub-data block to be transmitted to a corresponding destination.
Optionally, the method further comprises:
monitoring the limit condition of the current network and the use condition of the current network bandwidth in real time;
And dynamically determining the transmission rate of any one of the plurality of sub-data blocks to be transmitted according to the limit condition of the current network and the use condition of the current network bandwidth.
Optionally, the compressing the source file to be transmitted by using the target compression mode to obtain a compressed file includes:
Acquiring a target suffix matched with the target compression mode;
and compressing the source file to be transmitted in the target compression mode to obtain the compressed file with the target suffix.
Optionally, the method further comprises:
And creating an internal buffer area to store the compressed file through the internal buffer area.
In a second aspect, an embodiment of the present invention provides a file transmission apparatus for network bandwidth, where the apparatus includes:
The compression module is used for compressing the source file to be transmitted in a target compression mode to obtain a compressed file;
the segmentation module is used for segmenting a data block corresponding to the file to be transmitted into a plurality of sub-data blocks to be transmitted according to a preset size after the compressed file is used as the file to be transmitted;
A determining module, configured to determine whether a token bucket has a first token that is greater than or equal to a first preset threshold;
And the transmission module is used for transmitting the sub-data blocks to be transmitted to the corresponding destination end under the condition that the first token larger than or equal to the first preset threshold value exists in the token bucket and any one of the plurality of sub-data blocks to be transmitted applies for and acquires the corresponding first token.
Optionally, the apparatus further comprises:
A token generation module for waiting and generating second tokens to store all second tokens in the token bucket in response to the tokens in the token bucket having been exhausted;
The determining module is further configured to determine whether the second token is greater than or equal to a second preset threshold in the token bucket;
the transmission module is further configured to apply for and acquire a corresponding second token from any one of the plurality of sub-data blocks to be transmitted when the second token greater than or equal to the second preset threshold is determined to exist in the token bucket, and transmit the sub-data block to be transmitted to a corresponding destination.
Optionally, the apparatus further comprises:
the monitoring module is used for monitoring the limit condition of the current network and the use condition of the current network bandwidth in real time;
and the transmission rate determining module is used for dynamically determining the transmission rate of each sub-data block to be transmitted in the plurality of sub-data blocks to be transmitted according to the limit condition of the current network and the use condition of the current network bandwidth.
In a third aspect, embodiments of the present invention provide a computer readable storage medium storing a computer program for performing the above-described method steps.
In a fourth aspect, an embodiment of the present invention provides an electronic device, including:
A processor;
a memory for storing the processor-executable instructions;
the processor is configured to read the executable instructions from the memory and execute the executable instructions to implement the method steps described above.
In the embodiment of the invention, a source file to be transmitted is compressed in a target compression mode to obtain a compressed file, after the compressed file is used as the file to be transmitted, a data block corresponding to the file to be transmitted is segmented into a plurality of sub-data blocks to be transmitted according to a preset size, whether a first token larger than or equal to a first preset threshold value exists in a token bucket or not is determined, and under the condition that a first token larger than or equal to the first preset threshold value exists in the token bucket and any sub-data block to be transmitted in the plurality of sub-data blocks to be transmitted applies for and acquires the corresponding first token, the sub-data block to be transmitted is transmitted to a corresponding destination terminal. According to the file transmission method for the network bandwidth, only when a token bucket has enough first tokens and any one sub-data block to be transmitted applies for and acquires one token in the token bucket, the sub-data block to be transmitted is transmitted to the corresponding destination end, so that the network congestion phenomenon possibly occurring is avoided, and the quick, stable and controllable file transmission process is realized.
Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
It is noted that unless otherwise indicated, technical or scientific terms used herein should be given the ordinary meaning as understood by one of ordinary skill in the art to which this invention belongs.
In addition, the terms "first" and "second" etc. are used to distinguish different objects and are not used to describe a particular order. Furthermore, the terms "comprise" and "have," as well as any variations thereof, are intended to cover a non-exclusive inclusion. For example, a process, method, apparatus, article, or device that comprises a list of steps or elements is not limited to the list of steps or elements but may, in the alternative, include other steps or elements not expressly listed or inherent to such process, method, article, or device.
The embodiment of the invention provides a file transmission method and device for network bandwidth, electronic equipment and a computer readable medium, and the method and the device are described below with reference to the accompanying drawings.
Example 1
Referring to fig. 1, which is a flowchart illustrating a method for transmitting a file for network bandwidth according to some embodiments of the present invention, as shown in fig. 1, the method for transmitting a file for network bandwidth may include the following steps:
and step S101, compressing the source file to be transmitted in a target compression mode to obtain a compressed file.
In an actual application scenario, the target compression mode may be a gzip compression mode using a gzip algorithm. The gzip compression scheme can significantly reduce the size of the transmission file to reduce the network bandwidth requirements. In the compression process, the source file to be transmitted is converted into a compressed file in gz format.
In addition, GZIP Output Stream classes in Java can be used for compressing files. An open-source compression library, such as Apache Commons Compress, is integrated in the file transfer system. The compression level is set to balance the compression efficiency and the compression ratio.
In addition to the compression method of the above example, other compression methods may be used to compress the source file to be transmitted, which is not specifically limited herein.
In an actual application scene, the size of a transmission file can be obviously reduced by the compression mode, and the requirement on network bandwidth is directly reduced. By adopting the compression mode, such as gzip compression, the file volume of the source file can be reduced to 30% -50% of the file volume of the source file on the premise of ensuring the data integrity, so that the transmission time and the bandwidth occupation are effectively reduced.
In one possible implementation manner, compressing a source file to be transmitted in a target compression manner to obtain a compressed file, including the following steps:
Acquiring a target suffix matched with a target compression mode;
and compressing the source file to be transmitted in a target compression mode to obtain a compressed file with a target suffix.
In a possible implementation manner, the file transmission method for network bandwidth provided by the embodiment of the invention may further include the following steps:
an internal buffer is created to store the compressed file through the internal buffer.
In an actual application scenario, the compressed file is read using the data stream before the internal buffer is created.
In an actual application scene, file Input Stream types in Java are used for reading the compressed file. The creation of the internal Buffer can be implemented using Byte Buffer classes in Java or custom buffers.
ByteBuffer can be used as a buffer area, because it is a continuous space in the memory, and four indexes, namely mark, position, limit, capability, are defined in the ByteBuffer object, and are described for the above four indexes respectively, as follows:
mark is used for marking the current position;
The position represents the pointer of current read-write, if a byte is written into ByteBuffer objects, the byte is written into the address pointed by the position, if a byte is read out from ByteBuffer, the byte is read out from the address pointed by the position, and after the read-write is completed, the position is added with 1;
limit is a readable/writable boundary, and when positio reaches limit, it indicates that the content in ByteBuffer is completely read or ByteBuffer is full;
The capability is ByteBuffe bytes, and if a ByteBuffer.Allocate (128) is called in the program, it means that a ByteBuffer object with 128 bytes of capability is created.
Step S102, after taking the compressed file as a file to be transmitted, dividing a data block corresponding to the file to be transmitted into a plurality of sub-data blocks to be transmitted according to a preset size.
In the file transmission method provided in the embodiment of the present invention, the preset size in step S102 is not specifically limited, and may be configured according to the requirements of different application scenarios, which is not described herein.
In a specific application scenario, the preset size can be configured to be 1KB, and the data block corresponding to the file to be transmitted is divided into a plurality of sub-data blocks to be transmitted with the size of 1KB, so that network congestion is prevented by limiting the transmission rate of data, and the stability and the high efficiency of network communication are maintained.
Step S103, determining whether a first token greater than or equal to a first preset threshold exists in the token bucket.
It should be noted that the application scenario for this step is an application scenario in which there are enough tokens in the token bucket. The first preset threshold is not specifically limited, and may be adjusted according to requirements of different application scenarios, which is not described herein.
Step S104, under the condition that the token bucket has a first token which is larger than or equal to a first preset threshold value and any one of a plurality of sub-data blocks to be transmitted applies for and acquires the corresponding first token, the sub-data block to be transmitted is transmitted to the corresponding destination terminal.
It should be noted that the application scenario for this step is that there are enough first tokens in the token bucket, and any one of the plurality of sub-data blocks to be transmitted applies for and obtains the application scenario of the corresponding first token.
In order to avoid the problem of data congestion possibly occurring in the file transmission process, the file transmission method provided by the embodiment of the invention mainly has the following two application scenarios, and the specific steps are as follows:
the first case is that there are enough tokens in the token bucket:
and in response to the fact that enough first tokens (the first tokens larger than or equal to a first preset threshold value) exist in the token bucket, any one of the plurality of sub-data blocks to be transmitted applies for and acquires one first token in the token bucket, and the sub-data block to be transmitted is transmitted to the corresponding destination terminal.
In the second case, there are not enough tokens in the token bucket:
waiting to generate new tokens, namely second tokens, until the second tokens are generated, so as to store all the second tokens into the token bucket;
and determining that enough second tokens (second tokens larger than or equal to a second preset threshold) exist in the token bucket, and any one of a plurality of sub-data blocks to be transmitted applies for and acquires one second token in the token bucket, and transmitting the sub-data block to be transmitted to a corresponding destination terminal.
As above, the first token is directed to an application scenario in which there are enough tokens in the token bucket, while the second token is directed to an application scenario in which there are not enough tokens in the token bucket, generating a new token until the newly generated token, i.e., the second token.
It should be noted that the application scenario for this step is an application scenario in which there are enough newly generated second tokens in the token bucket. The second preset threshold is not specifically limited, and may be adjusted according to requirements of different application scenarios, which is not described herein.
In an actual application scene, the initialization process of the token bucket comprises the steps of setting the initial token number and the token generation rate. The initial number of tokens represents the number of tokens initially present in the token bucket and the token generation rate represents the number of tokens generated by the token generator per second.
The token generator dynamically adjusts the number of tokens generated per second according to the dynamic transmission rate control module.
In the file transmission method provided by the embodiment of the invention, the number of tokens generated by the token generator per second is not particularly limited, and the number of tokens generated per second can be determined by adopting the following strategy, and the method is as follows:
in the first case, if the current bandwidth of the current network is relatively abundant, the token generator increases the number of tokens generated per second to increase the transmission rate.
In the second case, the token generator may reduce the number of tokens generated per second to reduce the transmission rate if the current bandwidth of the current network is tight.
The token generator dynamically adjusts the number of tokens generated per second according to the dynamic transmission rate control module, thus flexibly adjusting the transmission speed under different network bandwidth conditions, improving the transmission efficiency to the greatest extent, reducing network congestion and ensuring the stability of the transmission process.
In a possible implementation manner, the file transmission method provided by the embodiment of the invention further includes the following steps:
monitoring the limit condition of the current network and the use condition of the current network bandwidth in real time;
And dynamically determining the transmission rate of each sub-data block to be transmitted in the plurality of sub-data blocks to be transmitted according to the limit condition of the current network and the use condition of the current network bandwidth.
In an actual application scenario, the process of dynamically determining the transmission rate of each of the plurality of sub-data blocks to be transmitted by the dynamic transmission rate control module is specifically as follows:
and inquiring the network bandwidth condition of the current departure private line through an SNMP protocol. The number of input bytes and the number of output bytes of the network device can be obtained through the query action executed by the SNMP protocol, so that the current bandwidth utilization rate is calculated. And calculating the current bandwidth duty ratio according to the query result. If the current bandwidth usage of the current network does not reach 50%, a part of the remaining bandwidth is allocated to each sub-data block to be transmitted for file transmission, and if the current bandwidth usage of the current network has reached 50% and above, the current transmission rate is limited to 1% of the total bandwidth. The method for dynamically adjusting the transmission rate can furthest improve the file transmission rate when the network bandwidth utilization rate is low, and ensure the normal operation of other key services when the network bandwidth utilization rate is high. And then, every 30 seconds, updating the file transmission rate according to the new bandwidth duty ratio calculation result. Therefore, the file transmission method provided by the embodiment of the invention can dynamically optimize the transmission process according to the change of the actual network environment, and ensure the high efficiency and stability of file transmission.
Since the SNMP protocol is a conventional protocol, the details are not repeated here.
It should be noted that, the foregoing dynamic transmission rate control module may determine other key transmission parameters besides being capable of determining a transmission rate of any one of the plurality of sub-data blocks to be transmitted, where the other key transmission parameters may include an upper limit value of the transmission rate and an initial value of the transmission rate.
Fig. 2 is a schematic diagram of a file transmission method for network bandwidth in a specific application scenario.
As shown in fig. 2, the file transfer process is specifically as follows:
The method comprises the steps of obtaining a compressed file after a source file is compressed, taking the compressed file as a file to be transmitted, dividing a data block corresponding to the file to be transmitted into a plurality of sub-data blocks to be transmitted according to a preset size, and if not, waiting for generating a new token (a second token) until the new token is generated, storing all the new tokens into a token bucket, determining that the new token in the token bucket can be transmitted only after the corresponding token is applied for and acquired, judging whether the token in the token bucket is enough, applying for and acquiring one token in the token bucket by any one of the sub-data blocks to be transmitted, and transmitting the sub-data block to the corresponding destination end, otherwise, waiting for generating the new token (the second token) until the new token is generated, storing all the new tokens into the token bucket, and determining that the new token in the token bucket is enough, repeating the steps, and applying for and acquiring any one of the sub-data blocks to be transmitted to the corresponding sub-data block to be transmitted to the corresponding destination end.
In addition, in the file transmission process shown in fig. 2, the process of dynamically determining the transmission rate of any one of the plurality of sub-data blocks to be transmitted by the dynamic transmission rate control module also uses the network bandwidth ratio threshold value, which is the network bandwidth ratio of 50%, and the description of this portion is referred to the description of the same or similar portions, and will not be repeated here.
It should be noted that, the file transmission method provided by the embodiment of the invention can be applied to application scenarios with limited network bandwidth, especially application scenarios with limited airport network bandwidth, but is not limited to airport network application scenarios, and other specific application scenarios are not listed one by one.
According to the file transmission method for the network bandwidth, only when a token bucket has enough first tokens and any one sub-data block to be transmitted applies for and acquires one token in the token bucket, the sub-data block to be transmitted is transmitted to the corresponding destination end, so that the network congestion phenomenon possibly occurring is avoided, and the quick, stable and controllable file transmission process is realized.
In addition, the file transmission method provided by the embodiment of the invention has the following advantages, and the specific steps are as follows:
The method has the advantages of self-adapting network bandwidth, having the capability of dynamically adjusting the transmission rate and being capable of adapting to continuously changing network conditions better.
The method has the advantage of effectively utilizing the network bandwidth, and can automatically improve the transmission rate when the network bandwidth is available so as to complete file transmission more quickly, thereby improving the transmission efficiency. Under high load conditions, the transmission rate can be reduced to avoid network congestion and ensure that other critical traffic is not affected.
The method has the advantages of optimizing network flow management, combining file compression and current limiting methods, not only reducing the transmission time required by transmitting the compressed file, but also reasonably distributing network resources and preventing excessive consumption of bandwidth so as to keep the network smooth and free from jamming.
The method has the advantage of 4 that the transmission reliability is enhanced, namely, the stability performance can be kept under various network environments by continuously monitoring the network condition and adjusting the strategy of the transmission speed, so that the risk of transmission interruption is reduced, and the reliability of network transmission is enhanced.
In the above embodiment, a file transmission method for network bandwidth is provided, and correspondingly, the invention also provides a file transmission device for network bandwidth. The file transmission device for network bandwidth provided by the embodiment of the invention can implement the file transmission method for network bandwidth, and the file transmission device for network bandwidth can be realized by software, hardware or a combination of software and hardware. For example, the file transfer means for network bandwidth may comprise integrated or separate functional modules or units to perform the corresponding steps in the methods described above.
Example 2
Referring to fig. 3, a schematic diagram of a file transfer device for network bandwidth according to some embodiments of the present invention is shown. Since the apparatus embodiments are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments for relevant points. The device embodiments described below are merely illustrative.
As shown in fig. 3, the file transfer device 300 for network bandwidth may include:
the compression module 301 is configured to compress a source file to be transmitted in a target compression manner to obtain a compressed file;
the splitting module 302 is configured to split, after taking the compressed file as a file to be transmitted, a data block corresponding to the file to be transmitted into a plurality of sub-data blocks to be transmitted according to a preset size;
a determining module 303, configured to determine whether there is a first token greater than or equal to a first preset threshold in the token bucket;
The transmission module 304 is configured to, when it is determined that there is a first token greater than or equal to a first preset threshold in the token bucket and any one of the plurality of sub-data blocks to be transmitted applies for and obtains a corresponding first token, transmit the sub-data block to be transmitted to a corresponding destination.
In some implementations of the embodiments of the present invention, the file transmission apparatus 300 for network bandwidth provided by the embodiments of the present invention may further include:
A token generation module (not shown in fig. 3) for waiting and generating second tokens to store all second tokens in the token bucket in response to tokens in the token bucket having been exhausted;
the determining module 303 is further configured to determine whether there are second tokens in the token bucket that are greater than or equal to a second preset threshold;
The transmission module 304 is further configured to apply for and obtain a corresponding second token from any one of the plurality of sub-data blocks to be transmitted when it is determined that the token bucket has a second token greater than or equal to a second preset threshold, and transmit the sub-data block to be transmitted to a corresponding destination.
In some implementations of the embodiments of the present invention, the file transmission apparatus 300 for network bandwidth provided by the embodiments of the present invention may further include:
A monitoring module (not shown in fig. 3) for monitoring the limit condition of the current network and the use condition of the current network bandwidth in real time;
A transmission rate determining module (not shown in fig. 3) for dynamically determining a transmission rate of each of the plurality of sub-data blocks to be transmitted according to a limit condition of the current network and a use condition of the current network bandwidth.
In some implementations of the embodiments of the present invention, the compression module 301 is specifically configured to:
Acquiring a target suffix matched with a target compression mode;
and compressing the source file to be transmitted in a target compression mode to obtain a compressed file with a target suffix.
In some implementations of the embodiments of the present invention, the file transmission apparatus 300 for network bandwidth provided by the embodiments of the present invention may further include:
A creation module (not shown in fig. 3) for creating an internal buffer to store the compressed file through the internal buffer.
In some implementations of the embodiment of the present invention, the file transmission device 300 for network bandwidth provided by the embodiment of the present invention has the same beneficial effects as the file transmission method for network bandwidth provided by the foregoing embodiment of the present invention due to the same inventive concept.
Example 3
An embodiment of the present invention provides a computer device, including a memory and a processor, where the processor is configured to read an instruction stored in the memory, where the instruction may perform a file transfer method for network bandwidth in any of the foregoing method embodiments.
It will be apparent to those skilled in the art that embodiments of the present invention may be provided as a method, apparatus, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (devices), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
Example 4
The present embodiment provides a computer-readable storage medium storing computer-executable instructions that can perform a file transfer method for network bandwidth in any of the above-described method embodiments. The storage medium may be a magnetic disk, an optical disc, a Read Only Memory (ROM), a random access Memory (Random Access Memory, RAM), a Flash Memory (Flash Memory), a hard disk (HARD DISK DRIVE, abbreviated as HDD), a Solid state disk (Solid STATE DRIVE, SSD), or the like, and the storage medium may further include a combination of the above types of memories.
It is apparent that the above examples are given by way of illustration only and are not limiting of the embodiments. Other variations or modifications of the above teachings will be apparent to those of ordinary skill in the art. It is not necessary here nor is it exhaustive of all embodiments. While still being apparent from variations or modifications that may be made by those skilled in the art are within the scope of the invention.