In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the interface of java file stream and the method of downloading". 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!
Java file stream interface
Package com.rainmanqqst.controller.filestream;import com.Ostermiller.util.Base64;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.* / * * @ author RAINMANQQST * @ date 2019-07-15 14:52 * @ description * / @ RestController@RequestMapping ("/ fille") public class FileController {private static Logger logger = LoggerFactory.getLogger (FileController.class) / * * convert the direct resource request interface address to the file stream interface * the file address encrypted by filePath for base64 * @ param filePath * @ param request * @ param response * / @ RequestMapping (value= "/ resource/ {filePath}") public void achieveResource (@ PathVariable ("filePath") String filePath, HttpServletRequest request, HttpServletResponse response) {try {String path = Base64.decode (filePath) Logger.info ("resource path is" + path); File file = new File (filePath); logger.info ("fileName:" + file.getName ()); response.setHeader ("Content-disposition", "attachment; filename=" + file.getName ()); response.setContentType ("application/octet-stream;charset=UTF-8") Response.setHeader ("Content-Length", file.length () + "); InputStream inputStream = new FileInputStream (file); ServletOutputStream servletOutputStream = response.getOutputStream (); int len= 0; byte [] buffer = new byte [1024]; while ((len=inputStream.read (buffer)) > 0) {servletOutputStream.write (buffer, 0, len) } servletOutputStream.flush (); inputStream.close (); servletOutputStream.close ();} catch (Exception ex) {ex.printStackTrace ();}
JS download code
Function exportFile () {/ / name= file name, blob= file binary object / / get timestamp var timestamp=new Date () .getTime (); / get XMLHttpRequest var xmlResquest = new XMLHttpRequest (); / / initiate request xmlResquest.open ("POST", "http://localhost:8086/rainmanqqst/api/system/resource/aHR0cHM6Ly9pbWcxLmRvdWJhbmlvLmNvbS92aWV3L3Bob3RvL2wvcHVibGljL3AxNDIyMDcxNjA4LndlYnA=", true)" / / set request header type xmlResquest.setRequestHeader ("Content-type", "application/json"); / / set request token / / xmlResquest.setRequestHeader (/ / "Authorization", / / Vue.cookie.get ('token') / /); xmlResquest.responseType = "blob" / return xmlResquest.onload = function (oEvent) {var content = xmlResquest.response; alert (content); / / assemble a tag var elink = document.createElement ("a"); / / set the download file name elink.download = timestamp + ".png" Elink.style.display = "none"; var blob = new Blob ([content]); elink.href = URL.createObjectURL (blob); document.body.appendChild (elink); elink.click (); document.body.removeChild (elink);}; xmlResquest.send ();}
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.