前言

这里前端访问使用的是element

一、前端

说明:

scope.row.img 是后端返回的URL数组

<el-table-column sortable prop="img" label="图片" width="200">

        <template slot-scope="scope">

          <el-image style="width: 100px; height: 100px" :src="setImgUrl(scope.row)"  :preview-src-list="scope.row.img"></el-image>

        </template>

      </el-table-column>

--------------------------------------------------------------------

setImgUrl方法

/设置产品第一张图片

    setImgUrl(row) {

      if(row.img.length != 0) {

        return row.img[0]

      }else {

        return ""

      }

    }

二、后端 

1.配置文件

#文件上传保存路径
file.path.localPath=D:/Admind/java/project/crm/src/main/resources/images/

2.拦截器配置

注意:如果配置有其他的拦截器,一定要排除“/images/**”路径,不要拦截它

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Value("${file.path.localPath}")
private String path;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //图片访问
    registry.addResourceHandler("/" + FileUtils.getPathLastName(path) + "/**").addResourceLocations("file:" + path + "/");
}

}

 FileUtils工具类

工具类调用getPathLastName方法的目的是获取路径的最后一个目录名,作用是:配置文件存储图片的path的路径修改的时候,也同样能够访问得到,不用我们去到代码中进行修改,降低了冗余度。

public class FileUtils {
/**
 * 获取文件路径最后的名
 * @return
 */
public static String  getPathLastName(String path) {
    int i = path.lastIndexOf("/") + 1;
    return path.substring(i);
}

}

 3.访问效果

我的图片存在位置:

Logo

前往低代码交流专区

更多推荐