Java Can';t从文件(Docker)打开指定的文件输入流
我正在尝试从以下路径读取excel数据:Java Can';t从文件(Docker)打开指定的文件输入流,java,spring,spring-boot,docker,docker-compose,Java,Spring,Spring Boot,Docker,Docker Compose,我正在尝试从以下路径读取excel数据:file:///Users/karen/prog/files/statuses.xlsx 但我经常收到一个错误: rest-module_1 | 2020-03-24 10:02:04.504 ERROR 72 --- [nio-9090-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in conte
file:///Users/karen/prog/files/statuses.xlsx
但我经常收到一个错误:
rest-module_1 | 2020-03-24 10:02:04.504 ERROR 72 --- [nio-9090-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file input stream from file: '/Users/karen/prog/files/statuses.xlsx'] with root cause
rest-module_1 |
rest-module_1 | java.io.FileNotFoundException: /Users/karen/prog/files/statuses.xlsx (No such file or directory)
这是我的代码:
public XSSFWorkbook getExcelWorkbook(String path) throws IOException, InvalidFormatException, URISyntaxException {
if (path.startsWith("file://")) {
System.out.println(path);
XSSFWorkbook wb = new XSSFWorkbook(new File(new URI(path)));
return wb;
} else {
XSSFWorkbook wb = new XSSFWorkbook(this.getClass().getResourceAsStream(path));
return wb;
}
}
可能出了什么问题?如果我通过终端导航到“files”文件夹并运行“pwd”,它将显示我上面提到的确切路径。我在用Mac
我对该文件具有读写权限:
我正在从docker运行应用程序,这可能是一个错误吗?
这是我的docker撰写文件:
services:
rest-module:
build:
context: ./Status
image: statusimage
ports:
- 9090:9090
volumes:
- ./Status/:/app:delegated
- ./LocalStorage/:/root/local:delegated
我通过向
docker compose
文件添加一个额外的卷来解决这个问题:
services:
rest-module:
build:
context: ./Status
image: statusimage
ports:
- 9090:9090
volumes:
- ./Status/:/app:delegated
- ./LocalStorage/:/root/local:delegated
- /Users/karen/prog/files:/Users/karen/prog/files:delegated
我通过向
docker compose
文件添加一个额外的卷来解决这个问题:
services:
rest-module:
build:
context: ./Status
image: statusimage
ports:
- 9090:9090
volumes:
- ./Status/:/app:delegated
- ./LocalStorage/:/root/local:delegated
- /Users/karen/prog/files:/Users/karen/prog/files:delegated
你能展示一下你是如何设置路径的吗?我正在从config.yml文件库中读取它,我能看看你是如何设置和读取它的吗?它是spring启动应用程序还是纯spring?我无法向您展示整个流程,因为它有多个文件,但以下是我如何读取资源路径:
XSSFWorkbook wb=getExcelWorkbook(getDataServiceConfig().getStatusesResource())代码>这只是一个简单的getter,使用strings响应。您可以演示如何设置路径吗?我正在从config.yml文件库中读取它,我可以看看您是如何设置和读取它的吗?它是spring启动应用程序还是纯spring?我无法向您展示整个流程,因为它有多个文件,但以下是我如何读取资源路径:XSSFWorkbook wb=getExcelWorkbook(getDataServiceConfig().getStatusesResource())代码>它只是简单地用字符串获取响应