Java Server limit Download speed

Source: Internet
Author: User

No resources are inexhaustible. Server bandwidth is limited, can limit a little bit. Recently using the cloud storage OpenStack Swift to do file storage downloads. Title First high speed code:

Private Long Writeresponse (HttpServletResponse Response,inputstream stream, long speed, long startTime, messagedigest MD5) {byte[] b = null;b = new Byte[swiftfiledownload.download_read_unit];try {outputstream os = Null;os = RESPONSE.GETOUTP Utstream (); Long Count = 0;int J;while ((j = Stream.read (b))! =-1) {if (count + J > Speed) {int need = (int) (speed-c Ount),//////left = (int) (j + count-speed); byte[] temp = new byte[need];byte[] lefttemp = new Byte[left]; System.arraycopy (b, 0, temp, 0, need); System.arraycopy (b, need, lefttemp, 0, left), Os.write (temp); md5.update (temp); Os.flush (); Long endTime = System.currenttimemillis (); Long sleeptime = StartTime + 1000-endtime;if (Sleeptime > 0) {thread.sleep (sleeptime);} StartTime = System.currenttimemillis (); count = 0;os.write (lefttemp); md5.update (lefttemp); Os.flush (); count + = left; Continue;} if (count + J < speed) {count + = j;byte[] temp = new BYTE[J]; System.arraycopy (b, 0, temp, 0, J); Os.write (temp); Md5.update (b); Os.flush (); conTinue;} if (count + j = = speed) {byte[] temp = new BYTE[J]; System.arraycopy (b, 0, temp, 0, J); Os.write (temp); Md5.update (b); Os.flush (); Long endTime = System.currenttimemillis (); Long sleeptime = StartTime + 1000-endtime;if (Sleeptime > 0) {thread.sleep (sleeptime);} Reset Counter starttime = System.currenttimemillis (); count = 0;continue;}}} catch (IOException E1) {Log.warn ("writeresponse ()-response=" + response + ", IOException", E1); throw new Businessswiftexc Eption (E1);} catch (Interruptedexception e) {Log.warn ("writeresponse ()-response=" + response + ", IOException", e); throw new Businesss Wiftexception (e);} Finally {try {stream.close ()} catch (IOException e) {Log.warn ("writeresponse ()-Close Swift object outflow error", e); throw new Businessswi Ftexception (e);}} return startTime;}

Main idea: Write a stream of data over a period of time, pausing for 1 seconds minus the time to read and write all the operations. Measured at about 10% of the float.

Java Server limit Download speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.