我正在處理一個Java EE項目,該項目提供了一個servlet,可將特定用戶的許多圖像(或常用文件)存儲到glassfish服務器中。 我想知道是否有標準的目錄來保存文件到標準的網絡編程。Java webapp媒體目錄
例如,我有三個用戶希望上傳他們的文件,我可以將他們保存到服務器中?
我正在處理一個Java EE項目,該項目提供了一個servlet,可將特定用戶的許多圖像(或常用文件)存儲到glassfish服務器中。 我想知道是否有標準的目錄來保存文件到標準的網絡編程。Java webapp媒體目錄
例如,我有三個用戶希望上傳他們的文件,我可以將他們保存到服務器中?
沒有任何標準目錄。我建議你在服務器上爲每個用戶創建一個目錄。例如:用戶註冊時,一些數據將進入數據庫,同時還會爲該用戶創建一個目錄。比這個用戶可以上傳任何文件到他自己的目錄。 P可以在服務器上的任何位置創建目錄,然後在服務器的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/users_directories
JNDI名稱。如果已經在deploymet描述符中映射了資源,則必須使用:java:comp/env/jndi/users_directories。
如果whant做用彈簧一樣:
<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資源提供此工廠類,重新啓動服務器,並且您可以使用上面所述的相同查找。
嗨,謝謝你的回覆。我也讀過關於替代文檔根目錄,我正在嘗試配置:http://stackoverflow.com/questions/10224887/problems-with-glassfish-web-xml但不起作用。現在,我嘗試你的解決方案。你能否提供一些例子或教程來快速理解JNDI函數? – CeccoCQ 2012-04-19 09:29:01
我編輯了我的答案。 – 2012-04-19 09:51:33
對不起,最後一個問題。如果用戶想要從瀏覽器(使用http:// url)顯示他的文件,我如何提供href鏈接? – CeccoCQ 2012-04-19 10:05:00