前端代码
<el-button type="primary" @click="dowload2('1662023440868上传测试.jpg')">下载</el-button>
js代码
dowload2(msg){ let href = "http://localhost:9090/myFiles/downloadFile?flag="+msg; let downloadElement = document.createElement('a') downloadElement.style.display = 'none' downloadElement.href = href document.body.appendChild(downloadElement) // 点击下载 downloadElement.click() },
后端代码
@RequestMapping("/downloadFile") public void downloadFiles(@RequestParam("flag") String downUrl, HttpServletRequest request, HttpServletResponse response) { downUrl = "D:/FileUpload/"+downUrl; System.out.println("downUrl========"+downUrl); OutputStream outputStream = null; InputStream inputStream = null; BufferedInputStream bufferedInputStream = null; byte[] bytes = new byte[1024]; File file = new File(downUrl); String fileName = file.getName(); System.out.println("本次下载的文件为" + downUrl); // 获取输出流 try { // StandardCharsets.ISO_8859_1 *=UTF-8' // response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 以流的形式返回文件 response.setContentType("application/octet-stream;charset=utf-8"); inputStream = new FileInputStream(file); bufferedInputStream = new BufferedInputStream(inputStream); outputStream = response.getOutputStream(); int i = bufferedInputStream.read(bytes); while (i != -1) { outputStream.write(bytes, 0, i); i = bufferedInputStream.read(bytes); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } if (bufferedInputStream != null) { bufferedInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
标签:vue,Springboot,bufferedInputStream,downUrl,outputStream,downloadElement,new,null From: https://www.cnblogs.com/xianz666/p/16649474.html