• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring boot文件路径映射配置代码实例

springboot 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

springboot配置本地资源映射路径需要配置一下映射资源位置,下面来介绍一下过程。

1.添加配置类

package org.jcut.tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component
public class MvcConfing implements WebMvcConfigurer {
  @Value("${imgpath}")
  String path;
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
//当请求服务器中带有/imgs/的时候就是到指定路径中找出对应名字对应图片,其中**就代表匹配多层路径下的名字
registry.addResourceHandler("/imgs/**").addResourceLocations("file:"+path);
  }
}

2.图片上传代码

//添加展示图片
     String t_img=UUID.randomUUID().toString();//商品展示图片id
     boolean flag=false;
     for(MultipartFile ff:file1) {
       String tImgId=UUID.randomUUID().toString();
//写入图片名字的时候最好把获取图片映射名称写进去,方便获取图片
       timg.insert(new TImg(tImgId, t_img, "imgs/"+tImgId+".jpg"));
       try {
        ff.transferTo(new File(path+tImgId+".jpg"));
        flag=true;
      } catch (E<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i>xception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } 
     }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring boot文件路径映射配置代码实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址