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()方法代替即可。
相關文章
- 在springboot打包成jar後,無法讀取自定義檔案的解決辦法Spring BootJAR
- Spring Boot啟動後讀取jar包內部檔案Spring BootJAR
- python中無法正確讀取.mat檔案的解決辦法Python
- 檔案無法粉碎解決辦法
- jvm:jmap無法dump檔案的解決辦法JVM
- docker方式部署的spring boot專案無法訪問宿主機目錄(解決)DockerSpring Boot
- Java讀取檔案中含有中文的解決辦法 (轉)Java
- CentOS下OpenCV無法讀取影片檔案的解決方法CentOSOpenCV
- IDEA Spring Boot 專案內既有SVN 也有Git時,無法更新程式碼問題解決IdeaSpring BootGit
- Spring Boot - 自定義 Banner 圖案Spring Boot
- 無法建立目錄或檔案 無法建立目錄的解決辦法
- CentOS下OpenCV無法讀取視訊檔案的解決方法CentOSOpenCV
- IDEA Spring-boot-devTools 無效解決辦法二IdeaSpringbootdev
- spring cloud+spring boot 電子商務spring boot獲取配置檔案的屬性CloudSpring Boot
- Spring Boot讀取配置檔案的幾種方式Spring Boot
- Spring boot 讀取properties檔案的四種方式Spring Boot
- Word的檔案被鎖定,無法編輯的解決辦法
- SpringBoot打jar包或war包獲取不到資原始檔解決辦法Spring BootJAR
- maven打包jar無法打入依賴專案問題解決MavenJAR
- Maven 本地倉庫明明有jar包,pom檔案還是報錯解決辦法MavenJAR
- IDEA專案突然出現異常無法啟動時的有效解決辦法Idea
- 無法讀取來源檔案或磁碟
- 解決:windows無法拖拽檔案Windows
- Spring Boot 自定義註解Spring Boot
- 關於無法建立aps.web專案的解決辦法 (轉)Web
- 檔案boot.ini非法怎麼回事 boot.ini非法的解決辦法boot
- 檔案或目錄損壞且無法讀取怎麼辦?
- python檔案無法讀寫怎麼辦Python
- Intellij IDEA 使用spring-boot-devtools無效解決辦法一IntelliJIdeaSpringbootdev
- iOS新建推送證書無法生成p12檔案的解決辦法iOS
- 精進 Spring Boot 03:Spring Boot 的配置檔案和配置管理,以及用三種方式讀取配置檔案Spring Boot
- 丟失Oracle中資料檔案Ora時的解決辦法Oracle
- Spring之Property檔案讀取Spring
- “專案檔案已被重新命名或已不在解決方案中”的解決辦法
- Spring Boot 自定義註解失效Spring Boot
- 在spring boot專案(maven)中引入其他 spring boot專案Spring BootMaven
- spring-boot-route(二)讀取配置檔案的幾種方式Springboot
- mac檔案刪除不了的解決辦法Mac