【问题标题】:Directory Structure for saving image files in Java/Jersey REST project (Maven)在 Java/Jersey REST 项目 (Maven) 中保存图像文件的目录结构
【发布时间】:2017-09-19 03:10:20
【问题描述】:

我是 Maven 项目的新手,因此请多多指教。我有一个 jersey REST 项目,其目录结构如下:

我的计划是使用MediaType.MULTIPART_FORM_DATA将文件保存到磁盘并通过hibernate将文件路径保存到DB。

至于存储图像,我打算在src/main/resource 下创建一个名为img 的新文件夹(与hibernate.cfg.xml 处于同一级别)并将图像保存在那里。

问题:

  1. 我的方法正确吗?有更好的建议吗?
  2. 我是否需要更改pom.xml 才能添加任何标签。我目前的pom.xml如下

    http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.xxx.xxx</groupId>
    <artifactId>xxx</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxx</name>
    
    <build>
        <finalName>xxx</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
            <!-- jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <!-- postgresql -->
            <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.4</version>
        </dependency>
        <!-- hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.5</version>
        </dependency>
        <!-- logging for hibernate -->
        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.25</version>
        </dependency>
        <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
        </dependency>
        <!-- password hashing -->
        <dependency>
                <groupId>org.mindrot</groupId>
                <artifactId>jbcrypt</artifactId>
                <version>0.3m</version>
        </dependency>
        <!-- http client -->
        <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.9.0</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.25</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    

提前致谢。

【问题讨论】:

    标签: java hibernate maven jersey-2.0


    【解决方案1】:

    在包含WEB-INF 目录的Web 应用程序中存储用户数据(如图像)不是一个好的和推荐的方法。

    您打算将图像存储在src/main/resource/img 目录中,但这只是开发项目结构。要在实际服务器上部署它,您必须先创建一个war 文件,然后再部署它。部署后,src/main/resource/img 目录将进入WEB-INF 目录,用户数据将存储在那里。

    现在,想一想当您必须部署 Web 应用程序的升级版本或者您想将 Web 应用程序移动到不同的服务器时的情况。仅对于 Web 应用程序,只需部署 war 文件很容易,但是对于此用户图像会发生什么。另一个问题是这是用户数据,那么如何备份这些数据,以免在硬件故障时丢失。

    推荐的存储用户图像的方法,如果图像文件很小,最好在数据库中存储为binary数据。如果图像文件很大,那么你应该考虑将它存储在一些外部服务器或Web应用程序目录之外,可以配置数据的频繁备份,然后将图像的路径存储在数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      相关资源
      最近更新 更多