遇到这个bug,花费了不少时间
报错
java
.io
.FileNotFoundException
: class path resource
[fastdfs
-client
.properties
] cannot be resolved to absolute file path because it does not reside in the file system
: jar
:file
:/data
/FastDFS
/fastdfs
-1.0-SNAPSHOT
.jar
!/BOOT
-INF
/classes
!/fastdfs
-client
.properties
at org
.springframework
.util
.ResourceUtils
.getFile(ResourceUtils
.java
:217) ~[spring
-core
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.core
.io
.AbstractFileResolvingResource
.getFile(AbstractFileResolvingResource
.java
:133) ~[spring
-core
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at com
.lsh
.unit
.FastDFSUtil
.upload(FastDFSUtil
.java
:26) ~[classes
!/:1.0-SNAPSHOT
]
at com
.lsh
.controller
.FileController
.upload(FileController
.java
:34) ~[classes
!/:1.0-SNAPSHOT
]
at sun
.reflect
.NativeMethodAccessorImpl
.invoke0(Native Method
) ~[na
:1.8.0_161
]
at sun
.reflect
.NativeMethodAccessorImpl
.invoke(NativeMethodAccessorImpl
.java
:62) ~[na
:1.8.0_161
]
at sun
.reflect
.DelegatingMethodAccessorImpl
.invoke(DelegatingMethodAccessorImpl
.java
:43) ~[na
:1.8.0_161
]
at java
.lang
.reflect
.Method
.invoke(Method
.java
:498) ~[na
:1.8.0_161
]
at org
.springframework
.web
.method
.support
.InvocableHandlerMethod
.doInvoke(InvocableHandlerMethod
.java
:209) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.method
.support
.InvocableHandlerMethod
.invokeForRequest(InvocableHandlerMethod
.java
:136) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.mvc
.method
.annotation
.ServletInvocableHandlerMethod
.invokeAndHandle(ServletInvocableHandlerMethod
.java
:102) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.mvc
.method
.annotation
.RequestMappingHandlerAdapter
.invokeHandlerMethod(RequestMappingHandlerAdapter
.java
:877) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.mvc
.method
.annotation
.RequestMappingHandlerAdapter
.handleInternal(RequestMappingHandlerAdapter
.java
:783) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.mvc
.method
.AbstractHandlerMethodAdapter
.handle(AbstractHandlerMethodAdapter
.java
:87) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.DispatcherServlet
.doDispatch(DispatcherServlet
.java
:991) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.DispatcherServlet
.doService(DispatcherServlet
.java
:925) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.FrameworkServlet
.processRequest(FrameworkServlet
.java
:974) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.servlet
.FrameworkServlet
.doPost(FrameworkServlet
.java
:877) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at javax
.servlet
.http
.HttpServlet
.service(HttpServlet
.java
:661) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.springframework
.web
.servlet
.FrameworkServlet
.service(FrameworkServlet
.java
:851) ~[spring
-webmvc
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at javax
.servlet
.http
.HttpServlet
.service(HttpServlet
.java
:742) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:231) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.tomcat
.websocket
.server
.WsFilter
.doFilter(WsFilter
.java
:52) ~[tomcat
-embed
-websocket
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:193) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.springframework
.web
.filter
.RequestContextFilter
.doFilterInternal(RequestContextFilter
.java
:99) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.filter
.OncePerRequestFilter
.doFilter(OncePerRequestFilter
.java
:107) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:193) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.springframework
.web
.filter
.HttpPutFormContentFilter
.doFilterInternal(HttpPutFormContentFilter
.java
:109) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.filter
.OncePerRequestFilter
.doFilter(OncePerRequestFilter
.java
:107) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:193) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.springframework
.web
.filter
.HiddenHttpMethodFilter
.doFilterInternal(HiddenHttpMethodFilter
.java
:81) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.filter
.OncePerRequestFilter
.doFilter(OncePerRequestFilter
.java
:107) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:193) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.springframework
.web
.filter
.CharacterEncodingFilter
.doFilterInternal(CharacterEncodingFilter
.java
:200) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.springframework
.web
.filter
.OncePerRequestFilter
.doFilter(OncePerRequestFilter
.java
:107) ~[spring
-web
-5.0.5.RELEASE
.jar
!/:5.0.5.RELEASE
]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.internalDoFilter(ApplicationFilterChain
.java
:193) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.ApplicationFilterChain
.doFilter(ApplicationFilterChain
.java
:166) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.StandardWrapperValve
.invoke(StandardWrapperValve
.java
:198) ~[tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.StandardContextValve
.invoke(StandardContextValve
.java
:96) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.authenticator
.AuthenticatorBase
.invoke(AuthenticatorBase
.java
:496) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.StandardHostValve
.invoke(StandardHostValve
.java
:140) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.valves
.ErrorReportValve
.invoke(ErrorReportValve
.java
:81) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.core
.StandardEngineValve
.invoke(StandardEngineValve
.java
:87) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.catalina
.connector
.CoyoteAdapter
.service(CoyoteAdapter
.java
:342) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.coyote
.http11
.Http11Processor
.service(Http11Processor
.java
:803) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.coyote
.AbstractProcessorLight
.process(AbstractProcessorLight
.java
:66) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.coyote
.AbstractProtocol$ConnectionHandler
.process(AbstractProtocol
.java
:790) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.tomcat
.util
.net
.NioEndpoint$SocketProcessor
.doRun(NioEndpoint
.java
:1459) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at org
.apache
.tomcat
.util
.net
.SocketProcessorBase
.run(SocketProcessorBase
.java
:49) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at java
.util
.concurrent
.ThreadPoolExecutor
.runWorker(ThreadPoolExecutor
.java
:1149) [na
:1.8.0_161
]
at java
.util
.concurrent
.ThreadPoolExecutor$Worker
.run(ThreadPoolExecutor
.java
:624) [na
:1.8.0_161
]
at org
.apache
.tomcat
.util
.threads
.TaskThread$WrappingRunnable
.run(TaskThread
.java
:61) [tomcat
-embed
-core
-8.5.29.jar
!/:8.5.29]
at java
.lang
.Thread
.run(Thread
.java
:748) [na
:1.8.0_161
]
原因
项目在本地的时候,启动项目运行可以读取到我们resources目录中的文件。但是项目构建成jar的形式之后,resources目录中的文件并不是直接存在系统中,而是嵌套在jar文件中。所以就获取不到,我们只能使用流的方式获取。
需要使用org.apache.commons.io包下的FileUtils.copyInputStreamToFile();方法。
依赖
<dependency>
<groupId>org
.apache
.directory
.studio
</groupId
>
<artifactId>org
.apache
.commons
.io
</artifactId
>
<version>2.4</version
>
</dependency
>
解决
ClassPathResource classPathResource
= new ClassPathResource("fastdfs-client.properties");
InputStream inputStream
= classPathResource
.getInputStream();
File fastdfsfile
= new File("/data/FastDFS/fastdfs-client.properties");
FileUtils
.copyInputStreamToFile(inputStream
,fastdfsfile
);
log
.info("fastdfs配置文件:"+fastdfsfile
.getAbsolutePath());
意思就是获取这个文件的流,通过流再复制一份新的文件,这个文件的位置是我们自己指定。 (有一种更简单的方式,就是将这个需要的配置文件复制在服务器的一个目录下,项目不读取jar包中的,而是读取我们这个目录下的文件)