There is no inexhaustible resource. Server has limited bandwidth. Operators can limit a little bit. Recent use of the cloud storage OpenStack Swift backlog file storage download. If the first speed limit 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;}
Basic idea: A period of time when the data stream is written to the cell. Pause 1 Next minus all the time for read and write operations. At the discovery of about 10% floats.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Java Server download speed limit