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";
    }
相关推荐
Thneonl1 分钟前
故意弄坏生产:混沌工程不是乱砸,是实验设计
后端·程序员
deli0071 分钟前
限流算法到底怎么选?令牌桶 vs 漏桶 vs 滑动窗口 3 种同屏实测
前端
陈前端1 分钟前
AI 猜错了接口返回格式之后,我开始怀疑喂给它的上下文
前端
风花一世月3 分钟前
🏫 我把一整座校园的数据大屏塞进了一个 HTML 文件(零构建、双击即开,在线直接玩)
前端
Thneonl10 分钟前
别上来就 strace:60 秒十条命令看清一台病机
后端·程序员
变与不变80610 分钟前
GET请求知识详解
前端·javascript
智塑未来11 分钟前
网站建设哪家服务好?把交付前、交付中、交付后拆开看
大数据·前端
百度一下吧12 分钟前
H5 应用开发:Android、iOS 与鸿蒙安全区域适配实战
前端
马剑威(威哥爱编程)19 分钟前
【AI全栈后端12-11】Spring Boot 把 AI 接口真正上线扛量:限流 / 降级 / 可观测
人工智能·spring boot·后端
tachibana236 分钟前
Golang 中数组和切片的区别?
开发语言·后端·golang·数组·切片