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