In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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))
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.