java.io.FileNotFoundException:文件名、目录名或卷标语法不正确

时间:2022-12-13 19:11:51

 最近在做多文件上传的时候出现了一个比较诡异的错误。

我在做文件上传的时候要更改上传文件的名字防止名字重复刷新掉之前的文件(重复shangchuan)

//服务器时间 
String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

通过该方法获取到服务器当前的时间之后,加入到文件的后缀形成新的文件名然后上传。

问题出现了:

这个错误是在上传文件。给我报java.io.FileNotFoundException: D:\ProgramTools\apache-tomcat-6.0.26\webapps\BAW-phaseM\2011-10-30_2015-4-22 23:59:59_xx.jsp (文件名、目录名或卷标语法不正确。)   的错误。我这边来回跟踪,都没有问题,就是最后一步上传到写入到文件的时候,愣是写不进去。然后后面查找才发现wewindows下的文件名不允许出现23:59:59(不允许出现)类似这样的字符的。

java.io.FileNotFoundException:文件名、目录名或卷标语法不正确


然后我把冒号去掉就没有报这个错误啦。

改成用:

                       

    /**
     * Returns the current time in milliseconds.  Note that
     * while the unit of time of the return value is a millisecond,
     * the granularity of the value depends on the underlying
     * operating system and may be larger.  For example, many
     * operating systems measure time in units of tens of
     * milliseconds.
     *
     * <p> See the description of the class <code>Date</code> for
     * a discussion of slight discrepancies that may arise between
     * "computer time" and coordinated universal time (UTC).
     *
     * @return  the difference, measured in milliseconds, between
     *          the current time and midnight, January 1, 1970 UTC.
     * @see     java.util.Date
     */
    public static native long currentTimeMillis();

就可以啦。



native关键字:
Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能。

提供三个讲解native的链接:

native关键字

native关键字解析

NATIVE


System.currentTimeMillis() 获得的是自()1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long

Windows 中文件夹命名规则是: 
① 文件名或文件夹名可以由1~256个西文字符或128个汉字(包括空格)组成,不能多于256个字符。 
② 文件名可以有扩展名,也可以没有。有些情况下系统会为文件自动添加扩展名。一般情况下,文件名与扩展名中间用符号“.”分隔。 
③ 文件名和文件夹名可以由字母、数字、汉字或~、!、@、#、$、%、^、&、( )、_、-、{}、’等组合而成。 
④ 可以有空格,可以有多于一个的圆点。 
⑤ 文件名或文件夹名中不能出现以下字符:\、/、:、*、?、"、<、>、| 。 
不区分英文字母大小写。