springboot文件上传以及虚拟路径设置

pom.xml配置文件上传

<!--文件上传-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

两者版本相对应版本

配置service中产生表格

@Override
public int updateHeaderSrc(Carousel carousel, MultipartFile file, HttpServletRequest request) {
    //获取源文件名
    String originalFilename =  file.getOriginalFilename();
    String ext ="." + originalFilename.split("\\.")[1];
    //使用UUID生成唯一标识
    String  uuid =  UUID.randomUUID().toString().replace("-","");
    String fileName = uuid + ext;
    String pre = "D:\\yutu\\images\\";//配置自己像放入文件夹
    String path= pre + fileName;
    try {
        file.transferTo(new File(path));//放入对应文件夹下
        carousel.setImage("http://localhost:8080"+"/api/file/" +fileName);//通过查询/api/file/时拦截
        return carouselDao.updateById(carousel);
    }catch(IOException e) {
        throw new RuntimeException("头像上传失败");
    }
}

配置拦截器

package com.itheam.food.common.Config;

import org.springframework.beans.factory.annotation.Value;
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 UploadFilePathConfig implements WebMvcConfigurer {
    @Value("${file.staticAccessPath}")//传出检测出来数据例如上表/api/file/
    private String staticAccessPath;
    @Value("${file.uploadFolder}")//虚拟路径进入真实访问路径
    private String uploadFolder;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder);
    }
}

配置application.yml

file:
  staticAccessPath: /api/file/**
  uploadFolder: D:\yutu\images\

staticAccessPath配置的是虚拟路径查出的值

uploadFolder为真实要跳转的路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 是一个开源的 JavaEE(Java Enterprise Edition)轻量级应用程序框架,它提供了很多便捷的功能,其中包括上传文件的功能。在 SpringBoot 中,上传文件时通常需要设置文件上传虚拟路径(Virtual Path)。 虚拟路径是指相对于应用程序的根目录的一个虚拟目录。其作用是将不同的物理路径映射到同一个虚拟路径下,从而使得服务器端能够统一管理不同的文件。这样一来,所有的文件可以通过同一个URL地址来访问,并且可以方便地实现访问权限管理。 在 SpringBoot 中,我们通常使用文件上传组件来上传文件,如 CommonsMultipartResolver 组件或 Spring Boot 内置的 MultipartAutoConfiguration 组件。在使用这些组件时,我们需要在 application.properties 或 application.yml 文件中设置上传文件的路径,也就是虚拟路径。通常情况下,我们可以将上传的文件放在应用程序的 resources 目录下,并将其在配置文件中设置虚拟路径。例如: # application.properties 文件配置 spring.servlet.multipart.location=classpath:/resources/upload/ 在这个例子中,我们设置了上传文件的虚拟路径为 /upload/,而实际上上传的文件会被存储在应用程序的 classpath:/resources/upload/ 目录下。这样一来,当我们需要访问上传的文件时,只需要通过 /upload/xxx 文件路径即可。同时,在代码中,我们可以通过 ResourceUtils.getFile("classpath:upload/xxx") 来获取上传的文件。 总之,SpringBoot 中的文件上传功能是非常方便和灵活的,只需要设置虚拟路径即可实现文件上传和访问功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值