您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何解決SpringBoot大文件RestTemplate下載”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
近期基于項(xiàng)目上使用到的RestTemplate下載文件流,遇到1G以上的大文件,下載需要3-4分鐘,因?yàn)檎{(diào)用API接口沒(méi)有做分片與多線(xiàn)程, 文件流全部采用同步方式加載,性能很慢。最近結(jié)合網(wǎng)上案例及自己總結(jié),寫(xiě)了一個(gè)分片下載tuling/fileServer項(xiàng)目:
1.包含同步下載文件流在瀏覽器加載輸出相關(guān)代碼; 2.包含分片多線(xiàn)程下載分片文件及合并文件相關(guān)代碼;
同步下載,支持分片下載Range主要代碼:
@Controller public class DownLoadController { private static final String UTF8 = "UTF-8"; @RequestMapping("/download") public void downLoadFile(HttpServletRequest request, HttpServletResponse response) throws IOException { File file = new File("D:\\DevTools\\ideaIU-2021.1.3.exe"); response.setCharacterEncoding(UTF8); InputStream is = null; OutputStream os = null; try { // 分片下載 Range表示方式 bytes=100-1000 100- long fSize = file.length(); response.setContentType("application/x-download"); String fileName = URLEncoder.encode(file.getName(), UTF8); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); // 支持分片下載 response.setHeader("Accept-Range", "bytes"); response.setHeader("fSize", String.valueOf(fSize)); response.setHeader("fName", fileName); long pos = 0, last = fSize - 1, sum = 0; if (null != request.getHeader("Range")) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); String numberRange = request.getHeader("Range").replaceAll("bytes=", ""); String[] strRange = numberRange.split("-"); if (strRange.length == 2) { pos = Long.parseLong(strRange[0].trim()); last = Long.parseLong(strRange[1].trim()); if (last > fSize-1) { last = fSize - 1; } } else { pos = Long.parseLong(numberRange.replaceAll("-", "").trim()); } } long rangeLength = last - pos + 1; String contentRange = new StringBuffer("bytes").append(pos).append("-").append(last).append("/").append(fSize).toString(); response.setHeader("Content-Range", contentRange); response.setHeader("Content-Length", String.valueOf(rangeLength)); os = new BufferedOutputStream(response.getOutputStream()); is = new BufferedInputStream(new FileInputStream(file)); is.skip(pos); byte[] buffer = new byte[1024]; int length = 0; while (sum < rangeLength) { int readLength = (int) (rangeLength - sum); length = is.read(buffer, 0, (rangeLength - sum) <= buffer.length ? readLength : buffer.length); sum += length; os.write(buffer,0, length); } System.out.println("下載完成"); }finally { if (is != null){ is.close(); } if (os != null){ os.close(); } } } }
多線(xiàn)程分片下載分片文件,下載完成之后合并分片主要代碼:
@RestController public class DownloadClient { private static final Logger LOGGER = LoggerFactory.getLogger(DownloadClient.class); private final static long PER_PAGE = 1024L * 1024L * 50L; private final static String DOWN_PATH = "F:\\fileItem"; ExecutorService taskExecutor = Executors.newFixedThreadPool(10); @RequestMapping("/downloadFile") public String downloadFile() { // 探測(cè)下載 FileInfo fileInfo = download(0, 10, -1, null); if (fileInfo != null) { long pages = fileInfo.fSize / PER_PAGE; for (long i = 0; i <= pages; i++) { Future<FileInfo> future = taskExecutor.submit(new DownloadThread(i * PER_PAGE, (i + 1) * PER_PAGE - 1, i, fileInfo.fName)); if (!future.isCancelled()) { try { fileInfo = future.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } } return System.getProperty("user.home") + "\\Downloads\\" + fileInfo.fName; } return null; } class FileInfo { long fSize; String fName; public FileInfo(long fSize, String fName) { this.fSize = fSize; this.fName = fName; } } /** * 根據(jù)開(kāi)始位置/結(jié)束位置 * 分片下載文件,臨時(shí)存儲(chǔ)文件分片 * 文件大小=結(jié)束位置-開(kāi)始位置 * * @return */ private FileInfo download(long start, long end, long page, String fName) { File dir = new File(DOWN_PATH); if (!dir.exists()) { dir.mkdirs(); } // 斷點(diǎn)下載 File file = new File(DOWN_PATH, page + "-" + fName); if (file.exists() && page != -1 && file.length() == PER_PAGE) { return null; } try { HttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/download"); httpGet.setHeader("Range", "bytes=" + start + "-" + end); HttpResponse response = client.execute(httpGet); String fSize = response.getFirstHeader("fSize").getValue(); fName = URLDecoder.decode(response.getFirstHeader("fName").getValue(), "UTF-8"); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int ch; while ((ch = is.read(buffer)) != -1) { fos.write(buffer, 0, ch); } is.close(); fos.flush(); fos.close(); // 最后一個(gè)分片 if (end - Long.parseLong(fSize) > 0) { // 開(kāi)始合并文件 mergeFile(fName, page); } return new FileInfo(Long.parseLong(fSize), fName); } catch (IOException e) { e.printStackTrace(); } return null; } private void mergeFile(String fName, long page) { File file = new File(DOWN_PATH, fName); try { BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file)); for (long i = 0; i <= page; i++) { File tempFile = new File(DOWN_PATH, i + "-" + fName); while (!file.exists() || (i != page && tempFile.length() < PER_PAGE)) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } byte[] bytes = FileUtils.readFileToByteArray(tempFile); os.write(bytes); os.flush(); tempFile.delete(); } File testFile = new File(DOWN_PATH, -1 + "-null"); testFile.delete(); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取遠(yuǎn)程文件尺寸 */ private long getRemoteFileSize(String remoteFileUrl) throws IOException { long fileSize = 0; HttpURLConnection httpConnection = (HttpURLConnection) new URL(remoteFileUrl).openConnection(); //使用HEAD方法 httpConnection.setRequestMethod("HEAD"); int responseCode = httpConnection.getResponseCode(); if (responseCode >= 400) { LOGGER.debug("Web服務(wù)器響應(yīng)錯(cuò)誤!"); return 0; } String sHeader; for (int i = 1;; i++) { sHeader = httpConnection.getHeaderFieldKey(i); if (sHeader != null && sHeader.equals("Content-Length")) { LOGGER.debug("文件大小ContentLength:" + httpConnection.getContentLength()); fileSize = Long.parseLong(httpConnection.getHeaderField(sHeader)); break; } } return fileSize; } class DownloadThread implements Callable<FileInfo> { long start; long end; long page; String fName; public DownloadThread(long start, long end, long page, String fName) { this.start = start; this.end = end; this.page = page; this.fName = fName; } @Override public FileInfo call() { return download(start, end, page, fName); } } }
“如何解決SpringBoot大文件RestTemplate下載”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。