基于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);
    }

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

欢迎评论!

猜你喜欢

转载自blog.csdn.net/spring_is_coming/article/details/105076899
今日推荐