file.getCanonicalPath()和multipartFile.getCanonicalPath()
时间: 2024-06-05 07:09:59 浏览: 11
这两个方法是不同的。
`file.getCanonicalPath()`是一个`java.io.File`类的方法,它返回一个规范化的绝对路径名字符串。这个方法会解析所有的`.`和`..`,并将路径中的符号链接转换为它们所指向的目标文件。
而`multipartFile.getCanonicalPath()`则不存在。可能是您想问的是`multipartFile.getOriginalFilename()`,它是`org.springframework.web.multipart.MultipartFile`接口的方法,返回上传文件的原始文件名。
相关问题
android file getabsolutepath和getcanonicalpath区别
getAbsolutePath() 和 getCanonicalPath() 都是 Java 中的 File 类的方法,用于获取文件的路径,但是它们之间有一些区别。
getAbsolutePath() 方法返回文件的绝对路径。绝对路径是从根路径(如“/”或“C:\”)开始的完整路径。如果文件是使用相对路径创建的,则getAbsolutePath()方法会将其转换为绝对路径。
getCanonicalPath() 方法返回规范路径,即消除了路径中的符号链接和冗余的名称。也就是说,它返回的是重写的绝对路径,不包含任何符号链接或冗余的路径项。如果文件本身不存在,或者不是一个目录,则该方法会抛出 IOException 异常。
因此,getAbsolutePath() 方法返回的是文件的实际存储位置的路径,而 getCanonicalPath() 方法返回的是文件的标准路径,将所有的符号链接和冗余路径项全部消除。在实际开发中,我们根据需要使用这两种方法来获取文件的所需信息。
在Java中File.getAbsolutePath()是干嘛的
在Java中,`File.getAbsolutePath()`方法返回文件的绝对路径。绝对路径是文件在文件系统中的完整路径,包括文件名和父目录。相对路径是相对于当前工作目录的路径。此外,`File.getCanonicalPath()`方法返回规范化的绝对路径,消除了路径中的冗余部分和符号链接等。
例如,如果文件`test.txt`在Windows系统中的路径为`C:\Users\John\Documents\test.txt`,则`file.getAbsolutePath()`将返回该路径字符串。而`file.getCanonicalPath()`将返回`C:\Users\John\Documents\test.txt`,因为它将消除路径中的冗余部分并解析符号链接。
需要注意的是,`File.getAbsolutePath()`可能返回的路径字符串在不同操作系统上可能会有所不同,因为不同操作系统的文件系统命名约定和路径分隔符可能不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)