window 和linux系统分隔符的不同

来源:互联网 发布:国家统计局人口数据 编辑:程序博客网 时间:2024/06/06 03:57
今天遇到个bug。


这是个导出功能,使用的是POI,使用已经做好的模板,向里面添加数据,在action中进行处理。
首先取得模板,
路径:
\\export\\excel\\template\\temp.xls

代码:
String modelpath = getRequest().getSession().getServletContext().getRealPath("/")+ CommonConstants.MODEL_PATH;

我电脑使用的win7系统。以上代码我在本地tomcat测试,很正常。导出一点问题没有。
当提交到测试组,测试就报出bug:
java.io.FileNotFoundException: /usr/local/apache-tomcat-5.5.23/webapps/项目名称\export\excel\template\temp.xls (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at com.stock.wrhcheck.web.StockCheckAction.exportExcel(StockCheckAction.java:85) 。。。。。。


开始还觉得很奇怪,怎么会就找不到模板文件呢?
仔细想下,想起不同系统的文件分隔符是不相同的:
获得系统名字System.out.println(System.getProperty( "os.name "));----------------------------获得系统文件分隔符System.out.println(System.getProperty("file.separator"));  文件分隔符,各个操作系统不一样    如WIndows的是"\",而Unix的是"/"


修改成下面就没问题了:
String modelpath = getRequest().getSession().getServletContext().getRealPath(CommonConstants.FILESEPARATOR)+ CommonConstants.MODEL_PATH;public static final String FILESEPARATOR = System.getProperty("file.separator");public static final String MODEL_PATH =FILESEPARATOR+"export"+FILESEPARATOR+"excel"+FILESEPARATOR+"template"+FILESEPARATOR+"temp.xls" ;


虽然问题很小,如果不注意所不定要花费很久去处理。还好想起系统分隔符的原因: