• java web路径和spring读取配置文件


    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题

    • 找不到自定义的property配置文件
    • 上传图片的时候找不到路径

    开发的时候是在windows上的,运行正常,部署的时候就出问题了,肯定是windows和linux路径区别导致的(一个小问题来回鼓捣了几个小时,因为有自己对windows下和linux下的区别还不是特别了解,还有就是每次在windows下修改完成以后都要重新上传到阿里云,项目较大来回也需要较多时间。。。),遂决定好好看看java web路径的问题。

    普通java程序获取路径

    Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath()
    null
    
    Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
    /D:/workspace/EPEducationManager/build/classes/
    
    UserResource.class.getClassLoader().getResource("/").toURI().getPath()
    null
    
    UserResource.class.getClassLoader().getResource("").toURI().getPath()
    /D:/workspace/EPEducationManager/build/classes/
    
    UserResource.class.getResource("").toURI().getPath()
    /D:/workspace/EPEducationManager/build/classes/com/phy/em/user/rest/
    
    UserResource.class.getResource("/").toURI().getPath()
    /D:/workspace/EPEducationManager/build/classes/
    
    System.getProperty("user.dir")
    D:workspaceEPEducationManager

    在java web中获取路径

    Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath()
    /D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/
    
    Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
    /C:/tomcat7/lib/
    
    UserResource.class.getClassLoader().getResource("/").toURI().getPath()
    /D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/
    
    UserResource.class.getClassLoader().getResource("").toURI().getPath()
    /C:/tomcat7/lib/
    
    UserResource.class.getResource("").toURI().getPath()
    /D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/com/phy/em/user/rest/
    
    UserResource.class.getResource("/").toURI().getPath()
    /C:/tomcat7/lib/
    
    System.getProperty("user.dir")
    C:Program Files (x86)eclipse

    根据上面的输出选择对应的获取路径的方法,特别注意获取得到的path前面有"/",不要手贱删除"/",对,我就是那个手贱的人,删除了"/",因为看到前面有斜杠在windows资源管理器中是打不开的,我就删除了,结果在windows上运行是正确的,但是部署在linux上的时候把"/"删除了就成了"var/share/lib",明显这个录警示不正确的,本来是根目录下var...成了当前目录下var...

    获取路径就可以读取制定目录下的配置文件了

    使用spring读取配置文件

    在xml中读取

    <bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
      <property name=”location”>
        <value>/WEB-INF/configInfo.properties</value>
      </property>
      <property name=”fileEncoding” value=”utf-8″ />
    </bean>

    在xml中使用

    <property name=”host”>
      <value>${email.host}</value>
    </property>
    <property name=”port”>
      <value>${email.port}</value>
    </property>

    通过以上两步就可以完成在读取property配置文件并注入到对应的bean中,但是有时候我们并不需要为了读取配置而创建一个bean,我们只想代码中直接读取配置文件,可以使用如下的方式

    ResourceUtils.getFile("classpath:config.properties").getPath()
    D:workspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsEPEducationManagerWEB-INFclasses

    可以直接在代码中使用"classpath"来定位配置文件,获取得到的是一个File对象,当然了获取路径肯定没问题


    通过这次的填坑经历又一次坚定了使用linux的信心和决心,以前多次使用linux的尝试都失败了,本着不pass(怕死)的心态又一次安装了kali和linux mint双系统(原来是windows和mint,把windows格了,把心爱的linux安装在了心爱的SSD上),坚持着一个月来,感觉越来越得心应手

  • 相关阅读:
    小M和天平(简单DP)
    前缀查询(维护字典树前缀和)
    假的字符串( trie树 + 拓扑)
    E. Two Teams(线段树+链表)
    B. Ugly Pairs(简单dfs)
    回文(牛客 https://ac.nowcoder.com/acm/problem/17062)
    Dubbo中CompletableFuture异步调用
    Dubbo消费者异步调用Future使用
    Dubbo消费者异步调用Future使用
    Dubbo服务暴露延迟
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/5468161.html
Copyright © 2020-2023  润新知