SpringBoot项目打成jar包后无法读取到resource下的文件

错误信息:找不到指定的文件

java.io.FileNotFoundException: sample.docx (No such file or directory)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.(FileInputStream.java:138)
	at com.zhaofeng.outbound.util.WordUtils.getWord(WordUtils.java:33)
	at com.zhaofeng.outbound.service.impl.ContractServiceImpl.createContract(ContractServiceImpl.java:122)
	at com.zhaofeng.outbound.controller.ContractController.createContract(ContractController.java:43)
	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:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)

文件应该放在resources目录下,并且以流的方式打开文件。
SpringBoot项目打成jar包后无法读取到resource下的文件_第1张图片

final String path = "word/sample.docx";
ClassPathResource resource = new ClassPathResource(path);
InputStream is = resource.getInputStream();

参考文章

SpringBoot项目打成jar包后无法读取到resource下的文件

你可能感兴趣的:(后端)