【问题标题】:How can I get the resource in src/main/resources/upload-dir?如何获取 src/main/resources/upload-dir 中的资源?
【发布时间】:2020-09-13 11:07:13
【问题描述】:

我的项目树是:

+---.mvn
|   \---wrapper
+---.settings
+---src
|   +---main
|   |   +---java
|   |   |   \---com
|   |   |       +---google
|   |   |       |   \---api
|   |   |       |       \---services
|   |   |       |           \---samples
|   |   |       |               \---youtube
|   |   |       |                   \---cmdline
|   |   |       |                       \---data
|   |   |       \---musicnet
|   |   |           \---springboot
|   |   |               +---basicauth
|   |   |               +---controller
|   |   |               +---exception
|   |   |               +---model
|   |   |               +---repository
|   |   |               \---service
|   |   \---resources
|   |       +---static
|   |       |   +---css
|   |       |   +---img
|   |       |   \---js
|   |       +---templates
|   |       \---upload-dir
|   \---test
|       \---java
|           \---com
|               \---musicnet
|                   \---springboot
\---target
    +---classes
    |   +---com
    |   |   +---google
    |   |   |   \---api
    |   |   |       \---services
    |   |   |           \---samples
    |   |   |               \---youtube
    |   |   |                   \---cmdline
    |   |   |                       \---data
    |   |   \---musicnet
    |   |       \---springboot
    |   |           +---basicauth
    |   |           +---controller
    |   |           +---exception
    |   |           +---model
    |   |           +---repository
    |   |           \---service
    |   +---META-INF
    |   |   \---maven
    |   |       \---com.musicnet
    |   |           \---musicnet
    |   +---static
    |   |   +---css
    |   |   \---js
    |   +---templates
    |   \---upload-dir
    \---test-classes
        \---com
            \---musicnet
                \---springboot

我正在使用 com.google.api.services.samples.youtube.cmdline.data 中的 UploadVideo.java 上传 src/main/resources/upload-dir 下的视频。

src/main/resources 配置在类路径上:

    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>

UploadVideo.java 在一行中返回 null:

InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, UploadVideo.class.getResourceAsStream("/" + SAMPLE_VIDEO_FILENAME));

我正在阅读并尝试几种方法来将 getResourceAsStream 中的路径引入或配置到我的视频,但始终返回 null。它工作的唯一方法是当我的上传目录位于包 com.google.api.services.samples.youtube.cmdline.data 下时。那么我可以将相对路径从 com.google.api.services.samples.youtube.cmdline.data 放到 src/main/resources/upload-dir 吗?

问候

【问题讨论】:

    标签: java spring youtube-api youtube-data-api


    【解决方案1】:

    您可以使用 Spring 轻松地从资源目录加载文件。

    获取或创建应用上下文:

    appContext=new ClassPathXmlApplicationContext()
    

    然后获取你想要的资源:

    res = appContext.getResource("classpath:upload-dir/targetvideo.mkv")
    

    然后就可以从 res 变量中获取输入流了。

    【讨论】:

      【解决方案2】:

      现在我正在检查文件上传到文件系统服务器并创建 appContext 时似乎不同步。为了更具体: - 首先将文件从视图上传到文件系统服务器。 - 然后调用 youtube api 以将视频上传到 youtube。 - Youtube api 创建资源并将其上传到 youtube。 - 但是第一次上传视频,资源为空。如果我从 Eclipse 重新启动项目(有时我必须关闭 Eclipse 并再次打开)并尝试相同的过程,资源是好的,然后视频被上传到 Youtube。

      就像视频保存在文件系统服务器上,但 appContext 没有找到文件系统上的视频。需要重启项目才能让appContext找到视频。

      我已尝试在 Windows -> Preferences -> WorkSpace -> Refresh using native hooks or pooling and Project -> Build automatically 但问题仍然存在。

      【讨论】:

        猜你喜欢
        • 2013-05-11
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        相关资源
        最近更新 更多