vue通过spring boot 下载文件教程

1、application.yml

复制代码
# 服务器端口
server:
  port: 8081



user-dir: D://app/stshare

2、配置多个路径(保留默认+新增)

若需保留默认路径并添加自定义路径,需显式包含默认路径:

复制代码
# application.properties
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/custom-static/

3、 添加本地磁盘目录

通过 file:前缀指定本地文件系统中的目录(如项目外的上传目录),适用于存储大量静态资源(如用户上传的图片):

复制代码
# application.properties
spring.web.resources.static-locations=classpath:/static/,file:D:/upload/

4、通过配置类自定义路径

复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将/static/**路径映射到classpath:/custom-static/目录
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/custom-static/");
        
        // 添加本地磁盘目录(如D盘upload目录)
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:D:/upload/");
    }
}

5、controller层代码

复制代码
package com.example.login;


import org.springframework.web.bind.annotation.*;


@RestController
public class StudentController {
  
    @RequestMapping("/api/public/newdownload")
    public void getnewdownload(){

    }
}

6、启动类

复制代码
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;




@SpringBootApplication(scanBasePackages={"com.example.service","com.example.service.impl","com.example.login","com.example.util"})
public class DemoApplicationTest {

	public static void main(String[] args) {

		SpringApplication.run(DemoApplicationTest.class, args);
	}

}

7、项目启动完成后在地址栏输入http://127.0.0.1:8081/api/public/newdownload/1.xlsx

出现下载弹窗说明文件路径配置成功

8、前端vue测试代码

复制代码
const exportpageStudent = () =>{
  location.href="api/public/newdownload/1.xlsx";
    }
相关推荐
章豪Mrrey nical17 分钟前
前后端分离工作详解Detailed Explanation of Frontend-Backend Separation Work
后端·前端框架·状态模式
派大鑫wink1 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
程序员爱钓鱼2 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII2 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
PineappleCoder2 小时前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
Dolphin_Home2 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
zfj3212 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
weixin_462446232 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL2 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码3 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web