java實現大文件下載,基於http方式,控件神馬的就不說了。
思路:下載文件無非要讀取文件然后寫文件,主要這兩個步驟,主要難點:
1.讀文件,就是硬盤到內存的過程,由於jdk內存限制,不能讀的太大。
2.寫文件,就是響應到瀏覽器端的過程,http協議是短鏈接,如果寫文件太慢,時間過久,會造成瀏覽器死掉。
知識點:
1.org.apache.http.impl.client.CloseableHttpClient 模擬httpClient客戶端發送http請求,可以控制到請求文件的字節位置。
2.BufferedInputStream都熟悉,用它接受請求來的流信息緩存。
3.RandomAccessFile文件隨機類,可以向文件寫入指定位置的流信息。
- 1
- 2
- 3
基於以上信息,我的實現思路就是首先判斷下載文件大小,配合多線程分割定制http請求數量和請求內容,響應到寫入到RandomAccessFile指定位置中。在俗點就是大的http分割成一個個小的http請求,相當於每次請求一個網頁。
廢話不說,上代碼。
DownLoadManagerTest類:
package xxxx; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.CountDownLatch; import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.task.TaskExecutor; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * 文件下載管理類 */ @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("test") @ContextConfiguration(locations={"classpath:test/applicationContext.xml"}) public class DownLoadManagerTest extends AbstractTransactionalJUnit4SpringContextTests{ private static final Logger LOGGER = LoggerFactory.getLogger(DownLoadManagerTest.class); /** * * 每個線程下載的字節數 */ private long unitSize = 1000 * 1024; @Autowired private TaskExecutor taskExecutor; private CloseableHttpClient httpClient; private Long starttimes; private Long endtimes; @Before public void setUp() throws Exception{ starttimes = System.currentTimeMillis(); System.out.println("測試開始...."); } @After public void tearDown() throws Exception{ endtimes = System.currentTimeMillis(); System.out.println("測試結束!!"); System.out.println("********************"); System.out.println("下載總耗時:"+(endtimes-starttimes)/1000+"s"); System.out.println("********************"); } public DownLoadManagerTest()