基于uploadFile对象进行图片上传,并返回服务器虚拟路径进行图片回显!!!

这边不说业务,只说注意事项:

一丶只要是不用nginx进行虚拟路径设定,那么我们虚拟如何设定呢:
//获取服务器协议 IP 端口 并拼接
String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
//编辑虚拟路径返回给前端,自己拼接自己设定的路径
image.setUrl(path+urlDirPath+"/"+realName);
二丶配置文件将真实路径映射到虚拟路径即可:
@PropertySource(value = {"classpath:/properties/image.properties","classpath:/properties/updateImage.properties"},encoding = "utf-8")
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Value("${image.urlDirPath}")
    private String urlDirPath;

    @Value("${image.localDirPath}")
    private String localDirPath;

    @Value("${image.name}")
    private String name;

    @Value("${image.medicalPath}")
    private String medicalPath;

    @Value("${image.newsPath}")
    private String newsPath;

    @Value("${image.propaganda}")
    private String propagandaPath;


    @Resource
    private LoginInterceptor loginInterceptor;

    //映射图片路径
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        //截取name
        String[] names = name.split(",");
        //定义返回数值
        List<String> realPathAll = new ArrayList<>();
        List<String> fictitiousPathAll = new ArrayList<>();
        //循环
        for (int i=0; i<names.length; i++){
            //真实路径集合
            String realNewsPath = "file:"+localDirPath+newsPath+names[i]+"/";
            String realMedicalPath = "file:"+localDirPath+medicalPath+names[i]+"/";
            String realPropagandaPath = "file:"+localDirPath+propagandaPath+names[i]+"/";
            realPathAll.add(realNewsPath);
            realPathAll.add(realMedicalPath);
            realPathAll.add(realPropagandaPath);
            //虚拟路径集合
            String fictitiousNewsPath = urlDirPath+newsPath+names[i]+"/**";
            String fictitiousMedicalPath = urlDirPath+medicalPath+names[i]+"/**";
            String fictitiousPropagandaPath = urlDirPath+propagandaPath+names[i]+"/**";
            fictitiousPathAll.add(fictitiousNewsPath);
            fictitiousPathAll.add(fictitiousMedicalPath);
            fictitiousPathAll.add(fictitiousPropagandaPath);
        }
        registry.addResourceHandler(fictitiousPathAll.toArray(new String[0]))            //对应返回的虚拟路径
                .addResourceLocations(realPathAll.toArray(new String[0]));               //对应返回的真实路径
        super.addResourceHandlers(registry);
    }

这里贴出比较复杂的代码,其实就是告诉大家配置的虚拟路径和真实路径是可以添加多个的,看自己的业务进行设定!!

欢迎评论!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值