This article is for download.

1. Implementation ideaIn the database design of the previous article, we did not use fileName for a field, which was used to store the fileName for the Layer object, so as to complete the correspondence between file and object.

Preview:

2.Code

View layer: first, when loading the data table asynchronously, we get fileName, and then get the current fileName fileName by getting the current row.

table.on('tool(test)', function(obj) { var data = obj.data; Var layEvent = obj.event; Var tr = obj.tr; // Get the value of lay-event (or event). $= layui.jquery; If (layEvent === 'download') {// delete var fileName = data.filename; window.location="layer/download? fileName="+fileName; }});Copy the code

However, it is not recommended to use Ajax for both upload and download (upload and download are resource acquisition requests, which can be achieved by a tag, etc., while Ajax is JS asynchronous encapsulation requests, which achieve different goals).

Window. location is used here to implement the file request.

Then there is the Controller layer:

@requestMapping (value = "Index/layer/download") @responseBody public Map<String,Object> downloadOne(HttpServletRequest req,HttpServletResponse response) throws IOException{ String fileName = req.getParameter("fileName"); // String layerId = req.getParameter("layerId"); String downloadDir = req.getSession().getServletContext().getRealPath("/") +"upload/"; // String savePath = req.getSession().getServletContext().getRealPath("/") +"download/"; downloadDir=downloadDir.substring(0,downloadDir.length()-1); downloadDir=downloadDir+"\\"; // Download directory String realPath=downloadDir+fileName; // File file = new File(realPath); If (file.exists()){response.setContentType("application/force-download"); response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment; fileName=" + fileName); byte[] buffer = new byte[1024]; FileInputStream fis = null; // File input stream BufferedInputStream bis = null; OutputStream os = null; // Output stream try {OS = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int i = bis.read(buffer); while(i ! = -1){ os.write(buffer); i = bis.read(buffer); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("----------file download" + fileName); try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return api.returnJson(2,"fail"+realPath+fileName); }Copy the code

The main is to tune the IO stream, and then download it, just spell the path and file name

3. Overview of effects

Feel useful to a small 💗💗 bar ~

** project is only for test learning use, reject any form of commercial use, abuse and deletion. Project source Code concern public number Code In Java, reply “SpringBoot upload download “can be obtained. In addition, there are Java learning maps, data structures and algorithm data can be accessed in the background. I hope to become a fellow traveler of Java technology with you