Java 在jar build b Spring Boot中找不到文件
这些日志和控制器类是不言自明的: 日志: 类别:Java 在jar build b Spring Boot中找不到文件,java,spring,Java,Spring,这些日志和控制器类是不言自明的: 日志: 类别: @Controller public class RouteController { @RequestMapping(value = "/app/**", method = RequestMethod.GET) public ResponseEntity<String> defaultPath() { System.out.println("Unmapped request
@Controller
public class RouteController {
@RequestMapping(value = "/app/**", method = RequestMethod.GET)
public ResponseEntity<String> defaultPath() {
System.out.println("Unmapped request handling!");
try {
File index = ResourceUtils.getFile("classpath:/static/index.html");
FileInputStream inputStream = new FileInputStream(index);
String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error in redirecting to index");
}
}
如何解决这个问题
我尝试使用classpath:static/index.html
而不是classpath:/static/index.html
,没有任何改变。我解决了这个问题
不要使用ResourceUtils
(有时Spring不会使Java更简单!)使用旧的Java方式从类路径加载资源:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/static/index.html");
很高兴你明白了。有更难/更容易的,然后是兼容/不兼容的。我认为你是在处理后者对前者的问题。
../ocr-web-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/index.html
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/static/index.html");