• spring boot文件路径映射配置


    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 (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
              }
  • 相关阅读:
    UVA 12284 Digital Matrix
    lightoj 1052
    light oj 1236
    light oj 1151
    省选准备 MISTAKE 大全
    我的省选 Day -15
    「FJ2014集训」采药人的路径
    【NOI2012】迷失游乐园
    寒假这十天
    计算几何 大杂烩
  • 原文地址:https://www.cnblogs.com/huqi96/p/13124215.html
Copyright © 2020-2023  润新知