zb51500618 2016-09-20 09:51 采纳率: 83.3%
浏览 2976

web项目中获取配置文件路径问题

现在做的是maven搭建的spring mvc项目,在service层获取code.properties图片说明

代码如下

 public static void updateProperties(String fileName,Map<String, String> keyValueMap) {  

        //String filePath = CodeTools.class.getClass().getResource("/").getPath();
         String  filePath  =  Thread.currentThread().getContextClassLoader().getResource(fileName).getPath(); 
       // String filePath = ClassLoader.getSystemClassLoader().getResource(fileName).getPath();
        Properties props = null;  
        BufferedWriter bw = null;  

        try {  
            filePath = URLDecoder.decode(filePath,"utf-8");      
            props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));  

            // 写入属性文件  
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));  

            props.clear();// 清空旧的文件  

            for (String key : keyValueMap.keySet())  
                props.setProperty(key, keyValueMap.get(key));  

            props.store(bw, "");  
        } catch (IOException e) {  
        } finally {  
            try {  
                bw.close();
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

这个类在service层里,这里获取的filePath路径 转码后如下:
file:/D:/work space/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/ctoc-manage/WEB-INF/lib/ctoc-service-0.0.1-SNAPSHOT.jar!/code.properties

这个路径 在这行就找不到了
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));

单独测试的时候是没有问题的,路径没这么复杂。在项目里就不知道 这是什么路径,求解!!!

我换了个tomcat地址,路径为:
file:/D:/apache-tomcat-7.0.68/wtpwebapps/ctoc-seller/WEB-INF/lib/ctoc-service-0.0.1-SNAPSHOT.jar!/code.properties

ctoc-service被打了包,ctoc-service-0.0.1-SNAPSHOT.jar! ,在tomcat里面怎么获取这个类的地址? 带架包的路径 输出流都不能生成。

被这些路径搞得头都晕了,help!

  • 写回答

1条回答 默认 最新

  • chenminhua1 2016-09-20 12:15
    关注

    路径获取不对,你得文件在编译打包的时候回被打倒target下面的文件夹里面,所以你的文件的路径应该是工作空间项目目录,target目录下面的子目录里面

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?