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";
    }
相关推荐
GreenTea3 小时前
GrokBot 核心成员 Lauren Tan:每月交付 2000 个 PR 的人,是怎么用 AI 的
前端·后端·架构
码事漫谈3 小时前
FDE:一个缩写,两种命运
后端
mCell4 小时前
用 Knip 清理 AI Coding 留下的冗余代码
前端·ai编程·前端工程化
名字还没想好☜4 小时前
Go context.AfterFunc 实战(Go 1.21):context 一取消就自动跑清理,告别手写 goroutine 监听 Done
后端·golang·go
笃行3505 小时前
使用Rokid AIUI做了一个童年推箱子游戏
前端
明月_清风5 小时前
为什么最近开始关注 JEV?几个实战案例告诉你答案
人工智能·后端
GetcharZp5 小时前
5 分钟拥有自己的 S3:RustFS 上手(MinIO 的 Rust 替代,Apache 2.0 可商用)
后端
excel6 小时前
前端加密的作用与使用场景
前端
行百里er6 小时前
Redis 版本演进、新特性与协议那些事儿
redis·后端
Joy T7 小时前
Spring AI 2.0 Agent 进阶:Memory、State 与 Context Engineering 常见技术全景
java·人工智能·后端·spring·agent入门·agent state