• 下载文件的写法


        @GetMapping("/download")
        public void download(String fid, HttpServletResponse response) throws Exception {
            if (StringUtils.isBlank(fid)) {
                throw new AIPGException("文件ID为空");
            }
            long id = Long.parseLong(fid);
            FileInfo fileInfo = fileOperationService.getFileInfo(id);
            if (fileInfo == null) {
                throw new Exception("该文件不存在");
            }
            String fileName = fileInfo.getFileName();
            byte[] data = fileOperationService.download(id);
            response.setContentType("application/octet-stream");//告诉浏览器输出内容为流
            fileName = new String(fileName.getBytes(), "utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            try (OutputStream os = response.getOutputStream()) {
                os.write(data);
                os.flush();
            }
        }
    

      前台JS代码

     if (layEvent === 'down') {
                    location.href = BASE_CONTEXT_PATH + '/file/download.dsr?fid=' + data.FILE_ID;

    通过这种方法可以实现对文件的下载功能。

  • 相关阅读:
    中风后遗症
    慢性湿疹半年
    女子脚背痒肿案
    肾盂肾炎病案
    鼻衄二则
    糖尿病病案
    慢性肠炎2例
    子宫肌瘤病案2例
    眩晕病案
    前列腺炎病案3例
  • 原文地址:https://www.cnblogs.com/jourage/p/9948248.html
Copyright © 2020-2023  润新知