从这篇文章中:http : //prelevain.wordpress.com/2010/12/18/url-as-jndi-
resource/
像这样在您的context.xml中定义您的FTP URL:
<Resource name="url/SomeService" auth="Container" type="java.net.URL" factory="com.mycompany.common.URLFactory" url="ftp://ftpserver/folder" />
提供com.mycompany.common.URLFactory实现,并确保结果类可用于Tomcat:
import java.net.URL;import java.util.Hashtable;import javax.naming.*;import javax.naming.spi.ObjectFactory;public class URLFactory implements ObjectFactory { public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { Reference ref = (Reference) obj; String urlString = (String) ref.get("url").getContent(); return new URL(urlString); }}
在web.xml中创建参考
<resource-ref> <res-ref-name> url/SomeService </res-ref-name> <res-type> java.net.URL </res-type> <res-auth> Container </res-auth></resource-ref>
然后在您的代码中通过执行JNDI查找来获取FTP URL:
InitialContext context = new InitialContext();URL url = (URL) context.lookup("java:comp/env/url/SomeService");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)