: :xxx(文件名、目录名或卷标语法不正确。)

时间:2025-04-21 12:46:22

在写文件上传下载的时候遇到一个bug,虽然很弱智,但是浪费了不少时间还是要记录下来

: : xxxxxxx (文件名、目录名或卷标语法不正确。)
	at (:122)
	at $(:256)
	at (:47)
	at .invoke0(Native Method)
	at (:62)
	at (:43)
	at (:498)
	at (:197)
	at (:141)
	at (:106)
	at (:894)
	at (:808)
	at (:87)
	at (:1060)
	at (:962)
	at (:1006)
	at (:909)
	at (:652)
	at (:883)
	at (:733)
	at (:227)
	at (:162)
	at (:53)
	at (:189)
	at (:162)
	at (:42)
	at (:189)
	at (:162)
	at (:201)
	at (:119)
	at (:189)
	at (:162)
	at (:202)
	at (:97)
	at (:542)
	at (:143)
	at (:92)
	at (:78)
	at (:357)
	at .http11.(:374)
	at (:65)
	at $(:893)
	at $(:1707)
	at (:49)
	at (:1149)
	at $(:624)
	at $(:61)
	at (:748)
Caused by: : xxxxxxxx (文件名、目录名或卷标语法不正确。)
	at .open0(Native Method)
	at (:270)
	at .<init>(:213)
	at .<init>(:162)
	at (:406)
	at (:120)
	... 47 more

代码

@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Value(" ${}")
    private String basePath;

    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) throws IOException {
        //file是一个临时文件,需要转存到指定位置,否则本次请求完成后临时文件会删除
        (());

        //原始文件名
        String originalFilename = ();
        String substring = (("."));

        //使用UUID重新生成文件名,防止文件名称重复造成文件覆盖
        String fileName = ().toString() + substring;

        //创建一个目录对象
        File dir = new File(basePath);
        //判断当前目录是否存在
        if(!()){
            ();
        }
        try{
            //将临时文件转存到制定未知
            (new File(basePath,fileName));
        }catch (IOException e){
            ();
        }
        return (fileName);
    }
}

问题原因:@Value(" ${}")注释前加了一个空格,导致查询不到改路径。

思考:排查bug不要左点点右点点,总以为很快就能解决错误,其实非常浪费时间,要有方案有步骤有逻辑的去排查