Spring Boot,Spring Cloud專案打成Jar包時,無法讀取自定義檔案的解決辦法
使用this.getClass().getResource()獲取檔案路徑的方法時,報錯!提示讀取不到檔案。
報錯資訊:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at com.kk.receiver.utils.ImageUtil.getImageBinary(ImageUtil.java:26)
at com.kk.receiver.web.JsDataController.notice(JsDataController.java:61)
at com.kk.receiver.web.JsDataController.javaScriptCollector(JsDataController.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMet
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandl
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMe
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMeth
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.ja
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
解決方案:
使用InputStream stream = getClass().getClassLoader().getResourceAsStream()方法代替即可。
相關文章
- Spring Boot啟動後讀取jar包內部檔案Spring BootJAR
- 檔案無法粉碎解決辦法
- jvm:jmap無法dump檔案的解決辦法JVM
- docker方式部署的spring boot專案無法訪問宿主機目錄(解決)DockerSpring Boot
- IDEA Spring-boot-devTools 無效解決辦法二IdeaSpringbootdev
- IDEA Spring Boot 專案內既有SVN 也有Git時,無法更新程式碼問題解決IdeaSpring BootGit
- CentOS下OpenCV無法讀取影片檔案的解決方法CentOSOpenCV
- spring cloud+spring boot 電子商務spring boot獲取配置檔案的屬性CloudSpring Boot
- Spring Boot - 自定義 Banner 圖案Spring Boot
- Spring boot 讀取properties檔案的四種方式Spring Boot
- Spring Boot讀取配置檔案的幾種方式Spring Boot
- CentOS下OpenCV無法讀取視訊檔案的解決方法CentOSOpenCV
- SpringBoot打jar包或war包獲取不到資原始檔解決辦法Spring BootJAR
- Intellij IDEA 使用spring-boot-devtools無效解決辦法一IntelliJIdeaSpringbootdev
- IDEA專案突然出現異常無法啟動時的有效解決辦法Idea
- maven打包jar無法打入依賴專案問題解決MavenJAR
- Maven 本地倉庫明明有jar包,pom檔案還是報錯解決辦法MavenJAR
- python檔案無法讀寫怎麼辦Python
- 無法讀取來源檔案或磁碟
- 精進 Spring Boot 03:Spring Boot 的配置檔案和配置管理,以及用三種方式讀取配置檔案Spring Boot
- Spring之Property檔案讀取Spring
- idea無法直接建立web專案的解決辦法(附整合Tomcat方法)IdeaWebTomcat
- 在spring boot專案(maven)中引入其他 spring boot專案Spring BootMaven
- 檔案或目錄損壞且無法讀取怎麼辦?
- spring-boot-route(二)讀取配置檔案的幾種方式Springboot
- Spring boot 獲取yml檔案工具類Spring Boot
- 解決Spring Boot無法跳轉jsp頁面問題Spring BootJS
- Spring Boot 自定義註解失效Spring Boot
- springboot 執行 jar 包讀取外部配置檔案Spring BootJAR
- Spring Boot 配置檔案Spring Boot
- Spring-Boot專案中配置redis註解快取SpringbootRedis快取
- mac檔案刪除不了的解決辦法Mac
- Win7 IIS7 配置錯誤由於許可權不足而無法讀取配置檔案的解決辦法Win7
- Spring Boot專案建立Spring Boot
- 行動硬碟無法訪問 “檔案或目錄損壞且無法讀取” 怎麼辦?硬碟
- Spring Boot入門(一):使用IDEA建立Spring Boot專案並使用yaml配置檔案Spring BootIdeaYAML
- Spring Boot讀取自定義外部屬性Spring Boot
- Spring Boot入門(一):搭建Spring Boot專案Spring Boot