由于我们公司的安全政策,我无法在普通属性文件中设置FTP服务器的密码。为此,我想通过JNDI获得连接。我们的服务器仅支持FTPS协议,这就是我使用org.apache.commons.net.ftp.FTPSClient
类的原因。
但是问题是如何通过JNDI访问FTPSClient
我的tomcat FTP资源
<Resource name="url/ftpService"
auth="Container"
type="org.apache.commons.net.ftp.FTPSClient"
factory="URLFactory"
username="usr" password="encyrepte_pass"
url="host" />
URLFactory
public class URLFactory implements ObjectFactory {
private Encryptor encryptor = null;
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
FTPSClient ftpClient = new FTPSClient(false);
try {
//decrept pass...
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login(username, password)) {
ftpClient.execPBSZ(0);
ftpClient.execPROT("P");
ftpClient.enterLocalPassiveMode();
} else {
System.out.println("FTP login failed");
}
} else {
System.out.println("FTP connect to host failed");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return ftpClient;
}
}
在我的应用中 web.xml
<resource-ref>
<res-ref-name>ftp/ftpService</res-ref-name>
<res-type>org.apache.commons.net.ftp.FTPClient</res-type>
<res-auth>Container</res-auth>
</resource-ref>
春天
<bean id="urlFtpResource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="url/ftpService"/>
<property name="resourceRef" value="true"/>
</bean>
我的例外
Could not convert constructor argument value of type [org.apache.commons.net.ftp.FTPSClient] to required type [java.net.URL]
谁可以通过URL JNDI资源获得FTPSClient?