Java 无法获取该文件
我试图获取存储在本地系统中的文件,为此我编写了以下代码 我从数据库获得的原始路径是Java 无法获取该文件,java,file,Java,File,我试图获取存储在本地系统中的文件,为此我编写了以下代码 我从数据库获得的原始路径是C:\Users\Admin\Documents\BulkmailExcelPath\159920358103000000_4898E400.xlsx 如果我直接给出,那就是抛出错误。为此,我将\替换为\。但我还是犯了同样的错误 String path = individualObject.getAttchmentPath().replace("\\", "\\
C:\Users\Admin\Documents\BulkmailExcelPath\159920358103000000_4898E400.xlsx
如果我直接给出,那就是抛出错误。为此,我将\
替换为\
。但我还是犯了同样的错误
String path = individualObject.getAttchmentPath().replace("\\", "\\\\");
System.out.println(path);
FileSystemResource file = new FileSystemResource(path);
Exception in thread "Timer-0" java.nio.file.InvalidPathException: Trailing char < > at index 499: C:\Users\user\Documents\BulkmailExcelPath\attachments\1599205942697000000_log4j-application.log
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.io.File.toPath(File.java:2234)
at org.springframework.core.io.FileSystemResource.<init>(FileSystemResource.java:82)
at com.util.bulkmailer.service.BulkMailSender.sendSimpleMessage(BulkMailSender.java:54)
at com.util.bulkmailer.processor.BulkMailProcessor.processor(BulkMailProcessor.java:247)
at com.kcs.util.bulkmailer.controller.BulkMailerController.sendMail(BulkMailerController.java:109)
at com.util.bulkmailer.controller.BulkMailerController$1.run(BulkMailerController.java:80)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
String path=individualObject.getAttchmentPath().replace(“\\”,“\\\”);
System.out.println(路径);
FileSystemResource file=新的FileSystemResource(路径);
线程“Timer-0”中出现异常java.nio.file.InvalidPathException:索引499处的尾随字符<>C:\Users\user\Documents\BulkmailExcelPath\attachments\159920594267000000_log4j-application.log
位于sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:191)
位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
位于sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
位于sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
位于java.io.File.toPath(File.java:2234)
位于org.springframework.core.io.FileSystemResource。(FileSystemResource.java:82)
在com.util.bulkmailer.service.BulkMailSender.sendSimpleMessage上(BulkMailSender.java:54)
位于com.util.bulkmailer.processor.BulkMailProcessor.processor(BulkMailProcessor.java:247)
在com.kcs.util.bulkmailer.controller.BulkMailerController.sendMail上(BulkMailerController.java:109)
位于com.util.bulkmailer.controller.BulkMailerController$1.run(BulkMailerController.java:80)
位于java.util.TimerThread.mainLoop(Timer.java:555)
在java.util.TimerThread.run(Timer.java:505)
如果我给出path变量中的字符串,那么它能够执行该行,并且不会给出任何错误。原因可能是什么。根据错误消息,我猜测路径的末尾包含无效的空格字符。您可以使用Path对象而不是字符串来验证URI,如下所示
Path Path=Path.get(textPath)代码>根据错误消息,我猜测路径末尾包含无效的空格字符。您可以使用Path对象而不是字符串来验证URI,如下所示Path Path=Path.get(textPath)代码>路径末尾有尾随空格。将第一行替换为:
String path=individualObject.getAttchmentPath().replace(“\\”,“\\\\”).trim()代码>路径末尾有尾随空格。将第一行替换为:
String path=individualObject.getAttchmentPath().replace(“\\”,“\\\\”).trim()代码>“索引499处的尾随字符”<>?在路径字符串的末尾似乎有大量不必要的空格。也许你需要修剪一下。看起来文件名的末尾有多余的空格。这是来自那些固定记录宽度的数据库吗?为什么你说你必须替换斜杠?我想,一旦你解决了空间问题,你就不需要了。是的,仅仅使用trim()
就解决了我的问题。“在索引499处拖尾字符”<>?在路径字符串的末尾似乎有大量不必要的空格。也许你需要修剪一下。看起来文件名的末尾有多余的空格。这是来自那些固定记录宽度的数据库吗?为什么你说你必须替换斜杠?我想,一旦你解决了空间问题,你就不需要了。是的,仅仅使用trim()
就解决了我的问题。为什么他们会这样做,它已经得到验证,这就是为什么他们会得到一个异常?为什么他们会这样做,它已经得到验证,这就是为什么他们会得到一个异常?