0


前端vue通过URL访问存储在服务器或磁盘的图片

前言

这里前端访问使用的是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.访问效果

我的图片存在位置:

标签: 前端 vue.js 服务器

本文转载自: https://blog.csdn.net/weixin_68266674/article/details/133892742
版权归原作者 keep the 所有, 如有侵权,请联系我们删除。

“前端vue通过URL访问存储在服务器或磁盘的图片”的评论:

还没有评论