首页 > 解决方案 > 使用 docker 访问 src/main/resources 中的文件

问题描述

我只是在学习如何在 java 中使用 docker,所以我非常愿意接受建议。

我的项目从上到下都没有问题;但是现在我正在尝试进行本地 docker deploy 我在访问文件夹位置时遇到了问题。

我在本地运行一切;桌面是windows。

我要做的是获取 ffmpeg.exe 并在运行时将 exe 文件移到 jar 旁边。

我猜 docker 有不同的方法可以做到这一点;但我不确定如何寻求帮助。

这是整个方法:

static public String ExportResource(String resourceName) throws Exception {
        InputStream stream = null;
        OutputStream resStreamOut = null;
        String jarFolder;
        stream = VideoTranscode.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
        if (stream == null) {
            throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
        }

        int readBytes;
        byte[] buffer = new byte[4096];

        jarFolder = new File(VideoTranscode.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
        if (!resourceFileAlreadyCopied(resourceName)) {
            try {

                resStreamOut = new FileOutputStream(jarFolder + resourceName);
                while ((readBytes = stream.read(buffer)) > 0) {
                    resStreamOut.write(buffer, 0, readBytes);
                }
            } catch (Exception ex) {
                throw ex;
            } finally {
                stream.close();
                resStreamOut.close();
            }
        }

        return jarFolder + resourceName;

    }

我在这里得到 NPE:

jarFolder = new File(VideoTranscode.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');

我的目录结构

这是我的码头文件

FROM maven:3-openjdk-8
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

标签: javaspringdocker

解决方案


推荐阅读