Windows路径或者Linux路径映射成web路径进行访问代码

package com.soft.mpms.zframe.config;
import java.io.File;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
 * 文件路径配置,可以直接访问
 * 映射的路径后面必须加/,否则访问不到
 */
@SuppressWarnings("deprecation")
@Configuration
public class FilePathConfig extends WebMvcConfigurerAdapter {
    //window临时目录
    public static  String windowtempfile = "C:/usr/tempfile/";
    //liunx临时目录
    public static  String liunxtempfile = "/usr/tempfile/";
    public static  String tempfilepath = "/platform/tempfile";
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(isWindows()) {
            //和页面有关的静态目录都放在项目的static目录下
            File file = new File(windowtempfile) ;
            if(!file.exists()) {
                file.mkdirs();
            }
            registry.addResourceHandler("/tempfile/**").addResourceLocations("file:"+windowtempfile);
        }else {
            //和页面有关的静态目录都放在项目的static目录下
            File file = new File(liunxtempfile) ;
            if(!file.exists()) {
                file.mkdirs();
            }
            registry.addResourceHandler("/tempfile/**").addResourceLocations("file:"+liunxtempfile);
        }
    }
    /**
     * 
     * @Title: gettempfilepath
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @return 参数
     * @return String 返回类型
     * @throws
     */
    public static String gettempfilepath() {
        if(isWindows()) {
            return windowtempfile;
        }else {
            return liunxtempfile;
        }
    }
    /**
     * 
     * @Title: isWindows
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @return 参数
     * @return boolean 返回类型
     * @throws
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
}
posted @   小侯学编程  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示