我正在开发一个 Java EE 项目,该项目提供了一个 servlet,该 servlet 为特定用户将大量图像(或公共文件)存储到 glassfish 服务器中。我想知道是否有标准目录可以将文件保存到标准 Web 编程中。
例如,我有三个用户想要上传他们的文件,我可以在哪里将它们保存到服务器中?
没有任何标准目录。我建议您在服务器上为每个用户创建目录。例如:用户注册,一些数据进入数据库,并为该用户创建一个目录。比这个用户可以上传任何文件到他自己的目录。
PS您可以在服务器的任何位置创建目录,然后在服务器的JNDI资源中配置目录路径以在您的应用程序中查找。
您必须创建PropertiesObjectFactory
类来处理 java.util.Porperties 的 JNDI 属性(如果您使用的是 glassfish 2)。或者您也可以编写您的自定义 ObjectFactory。Glassfish 3 已经具备此功能。它设置为:org.glassfish.resources.custom.factory.PropertiesFactory。
打开 glassfish 管理控制台并导航到:资源 -> JNDI -> 自定义资源,单击“新建”。提供一个 JNDI 名称,例如:jndi/users_directories,选择资源类型“ java.util.Properties
”,指定工厂类:org.glassfish.resources.custom.factory.PropertiesFactory
,然后单击“添加属性”,为例如:指定名称:users.directories
并在值列中复制您的目录路径。在这种情况下:/server/glassfish/users
。单击确定,仅此而已。
重新启动应用程序服务器。
在您的应用程序中查找:
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。
如果你想使用弹簧做同样的事情:
<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 资源提供这个工厂类,重新启动服务器,您就可以使用上面介绍的相同查找了。