Vue + Axios+Java文件下载

前端代码

    /**
	 * 下载文件
	 */
	 download(id) {
	     this.$request({
	         url: '/file/download/' + id,
	         method: 'get',
	         //responseType 参数必不可少
	         responseType: 'blob'
	     }).then((response) => {
				//构造一个blob对象来处理数据
	             const blob = new Blob([response.data]);
	             //获取文件名称
	             let fileName = decodeURIComponent(response.headers['content-disposition'].split(';')[1].split('=')[1]);
	             fileName = fileName.substring(1, fileName.length - 1);
	             //对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
	             //IE10以上支持blob但是依然不支持download
	             //支持a标签download的浏览器
	             if ('download' in document.createElement('a')) {
	                 //创建a标签
	                 const link = document.createElement('a');
	                 //a标签添加属性
	                 link.download = fileName;
	                 link.style.display = 'none';
	                 link.href = URL.createObjectURL(blob);
	                 document.body.appendChild(link);
	                 //执行下载
	                 link.click();
	                 //释放url
	                 URL.revokeObjectURL(link.href);
	                 //释放标签
	                 document.body.removeChild(link);
	             } else { 
	             	 //其他浏览器
	                 navigator.msSaveBlob(blob, fileName);
	             }
	         }
	     );
	
	 },

后端代码

 /**
     * 下载文件
     */
    @GetMapping("/file/download/{id}")
    public ResponseEntity<InputStreamResource> download(@PathVariable Long id, HttpSession session) throws UnsupportedEncodingException, FileNotFoundException {
        FileResource fileResource = fileResourceService.findById(id);
        //获取需要下载的文件流对象
        FileInputStream fileInputStream = fileService.download(fileResource.getUri());
        //统计下载次数
        Long userId = (Long) session.getAttribute(Constant.USER_KEY);
        downService.save(new Down().setCreateTime(LocalDateTime.now()).setDownUserId(userId).setResourceId(id));
        //对文件名进行url编码处理防止出现乱码
        String newName = URLEncoder.encode(fileResource.getFileName() + fileResource.getUri().substring(fileResource.getUri().lastIndexOf(".")).toLowerCase(), "utf-8")
                .replaceAll("\\+", "%20").replaceAll("%28", "\\(")
                .replaceAll("%29", "\\)").replaceAll("%3B", ";")
                .replaceAll("%40", "@").replaceAll("%23", "\\#")
                .replaceAll("%26", "\\&").replaceAll("%2C", "\\,");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", newName));
        headers.add("Expires", "0");
        headers.add("Pragma", "no-cache");
        return org.springframework.http.ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(fileResource.getFileSize())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(fileInputStream));
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图书管理系统是一个常见的项目,下面是使用 SpringBoot + MyBatisPlus + Restful + Vue + Jquery + Axios 的图书管理系统的简单介绍。 1. 项目概述 该项目是一个图书管理系统,主要功能包括: - 图书的增删改查 - 图书分类的增删改查 - 图书借阅的管理 2. 技术栈 - 后端:SpringBoot + MyBatisPlus + Restful - 前端:Vue + Jquery + Axios 3. 功能模块 - 登录模块:用户登录、退出登录 - 图书管理模块:图书查询、添加、修改、删除 - 图书分类模块:图书分类查询、添加、修改、删除 - 借阅管理模块:借阅记录查询、添加、修改、删除 4. 项目结构 - backend:后端代码 - src/main/java/com/example/demo:Java 代码 - src/main/resources:配置文件和静态资源 - frontend:前端代码 - src:Vue 代码 5. 实现步骤 - 使用 Spring Initializr 创建一个 SpringBoot 项目 - 引入 MyBatisPlus、Druid 数据库连接池、Lombok 等依赖 - 创建数据库表,使用 MyBatisPlus 自动生成实体类和 Mapper 接口 - 创建 Restful API,提供图书、图书分类、借阅记录的增删改查接口 - 使用 Vue、Jquery、Axios 等前端技术实现前端界面,调用后端提供的接口实现相应功能 6. 总结 该项目基于 SpringBoot + MyBatisPlus + Restful + Vue + Jquery + Axios 技术栈,实现了一个简单的图书管理系统。通过该项目,可以学习到如何使用 SpringBoot 进行开发,如何使用 MyBatisPlus 简化数据库操作,以及如何使用 Vue、Jquery、Axios 等前端技术实现前端界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值