Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of downloading SpringBoot large files RestTemplate

Shulou Source: shulou.com Published: 2022-06-03 00:57:19 09月19日 Update

This article introduces the relevant knowledge of "how to solve the RestTemplate download of large SpringBoot files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Recently, based on the RestTemplate download file stream used in the project, it takes 3-4 minutes to download large files of more than 1G, because the call to API API does not do sharding and multithreading, and the file stream is all loaded synchronously, so the performance is very slow. Recently, combined with the online case and my own summary, I wrote a fragment download tuling/fileServer project:

1. Contains synchronous download file stream to load and output related code in the browser; 2. Contains code related to multithreaded download of sharded files and merged files

Download synchronously, and you can download the main Range codes in parts:

@ Controllerpublic 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 {/ / multipart download Range representation 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) / support multipart download 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))

Tags: File code location thread synchronization content size way more case knowledge project UTF-8 support output utility achievement next dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Apple Shulou Tech Info Redmi