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";
    }
相关推荐
showker几秒前
ecstore等产品开启缓存-后台及前台不能登录原因-setcookie+session问题
java·linux·前端
追逐时光者22 分钟前
分享 4 款基于 .NET 开源免费、实用的文件搜索工具,效率提升利器!
后端·.net
程序新视界25 分钟前
什么是OLTP ,MySQL是如何支持OLTP的?
数据库·后端·mysql
songroom40 分钟前
dbpystream webapi: 一次clickhouse数据从系统盘迁至数据盘的尝试
后端·clickhouse·阿里云
csj501 小时前
前端基础之《React(4)—webpack简介-编译打包优化》
前端·react
万少2 小时前
Trae AI 编辑器6大使用规则
前端·javascript·人工智能
Lisonseekpan2 小时前
为什么要避免使用 `SELECT *`?
java·数据库·后端·sql·mysql·oracle
好玩的Matlab(NCEPU)2 小时前
如何编写 Chrome 插件(Chrome Extension)
前端·chrome
Yan-英杰2 小时前
Deepseek大模型结合Chrome搜索爬取2025AI投资趋势数据
前端·chrome
Crystal3282 小时前
app里video层级最高导致全屏视频上的操作的东西显示不出来的问题
前端·vue.js