繁体   English   中英

Java Webapp媒体目录

[英]Java webapp media directory

我正在一个Java EE项目上,该项目提供了一个servlet,该servlet可以为特定用户将很多图像(或通用文件)存储到glassfish服务器中。 我想知道是否有标准目录将文件保存到标准Web编程中。

例如,我有三个要上载文件的用户,我可以将它们保存到服务器中吗?

没有任何标准目录。 我建议您在服务器上为每个用户创建目录。 例如:用户注册,一些数据进入数据库,并为此用户创建目录。 比该用户可以将任何文件上传到他自己的目录。

PS,您可以在服务器上的任何位置创建目录,然后在服务器的JNDI资源中配置目录的路径以在您的应用程序中查看。

您必须创建PropertiesObjectFactory类来处理java.util.Porperties的JNDI属性(如果使用的是glassfish 2)。 或者您也可以编写自定义ObjectFactory。 Glassfish 3已经具有此功能。 它设置为:org.glassfish.resources.custom.factory.PropertiesFactory。

  1. 在服务器上的某处创建目录。 例如:/ server / glassfish / users
  2. 打开glassfish管理控制台,然后导航至:资源-> JNDI->自定义资源,单击“新建”。 提供一个JNDI名称,例如:jndi / users_directories,选择一种资源类型“ java.util.Properties ”,指定Factory类: org.glassfish.resources.custom.factory.PropertiesFactory ,然后单击“添加属性”,为该名称指定名称例如: users.directories和在value列中复制您的目录路径。 在这种情况下: /server/glassfish/users 单击确定,仅此而已。

  3. 重新启动应用程序服务器。

  4. 在您的应用程序中查找:

public Properties getProperties(String jndiName) { Properties properties = null; try { InitialContext context = new InitialContext(); properties = (Properties) context.lookup(jndiName); context.close(); } catch (NamingException e) { LOGGER.error("Naming error occurred while initializing properties from JNDI.", e); return null; } return properties; } public Properties getProperties(String jndiName) { Properties properties = null; try { InitialContext context = new InitialContext(); properties = (Properties) context.lookup(jndiName); context.close(); } catch (NamingException e) { LOGGER.error("Naming error occurred while initializing properties from JNDI.", e); return null; } return properties; }在应用程序中调用此方法时,请提供您在应用程序服务器中配置的JNDI名称: jndi/users_directories 如果已在deploymet描述符中映射资源,则必须使用:java:comp / env / jndi / users_directories。

如果您想使用spring做同样的事情:

<jee:jndi-lookup id="usersDirectories"
                 jndi-name="jndi/users_directories"/>

或者,如果您使用的是glassfish 2,则创建一个自定义的PropertiesObjectFactory类:

public class PropertiesObjectFactory implements Serializable, ObjectFactory {

    /**
     * File property name.
     */
    public static final String FILE_PROPERTY_NAME = "org.glassfish.resources.custom.factory.PropertiesFactory.fileName";

    /**
     * Implemented method from object factory interface.
     *
     * @param obj object
     * @param name name
     * @param nameCtx context name
     * @param environment environment
     * @return file properties
     * @throws Exception if error occurs
     */
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
            throws Exception {
        Reference ref = (Reference) obj;
        Enumeration<RefAddr> refAddrs = ref.getAll();

        String fileName = null;
        Properties fileProperties = new Properties();
        Properties properties = new Properties();

        while (refAddrs.hasMoreElements()) {
            RefAddr addr = refAddrs.nextElement();
            String type = addr.getType();
            String value = (String) addr.getContent();

            if (type.equalsIgnoreCase(FILE_PROPERTY_NAME)) {
                fileName = value;
            } else {
                properties.put(type, value);
            }
        }

        if (fileName != null) {
            File file = new File(fileName);
            if (!file.isAbsolute()) {
                file = new File(System.getProperty("com.sun.aas.installRoot") + File.separator + fileName);
            }
            try {
                if (file.exists()) {
                    try {
                        FileInputStream fis = new FileInputStream(file);
                        if (fileName.toUpperCase().endsWith("XML")) {
                            fileProperties.loadFromXML(fis);
                        } else {
                            fileProperties.load(fis);
                        }
                    } catch (IOException ioe) {
                        throw new IOException("IO Exception during properties load : " + file.getAbsolutePath());
                    }
                } else {
                    throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
                }
            } catch (FileNotFoundException fnfe) {
                throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
            }
        }
        fileProperties.putAll(properties);
        return fileProperties;
    }
}

制作此类的.jar文件,并将其放入服务器全局库目录。 为您的JNDI资源提供此工厂类,重新启动服务器,可以使用上面介绍的相同查找。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM