Apache commons VFS与Spring整合无法正常运行?
编写的工具类
public SecuredFileTransferProtocol(String IP, String Port, String Username, String Password, String Path) { url = "sftp://" + Username + ":" + Password + "@" + IP + ":" + Port + "/" + Path; } public FileSystemOptions createDefaultOptions() throws FileSystemException { // Create sftp options FileSystemOptions opts = new FileSystemOptions(); // // SSH Key checking SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); // // Root directory set to user home SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); // // Timeout is count by Milliseconds SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); return opts; } public Boolean Connection(){ System.out.println("URL:" + this.url); // return true; //执行到这句话Debug不到这句话,个人感觉应该改写到Bean // FileSystemOptions opts = new FileSystemOptions(); return false; }
package com.springapp.mvc.service.server; import com.hava.utils.SecuredFileTransferProtocol; import com.springapp.mvc.entity.server.LocalSftpServer; import com.springapp.mvc.repository.server.LocalSftpServerDAOImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created by hava on 2014/5/13. */ // Spring Service Bean的标识. @Service // 默认将类中的所有public函数纳入事务管理. @Transactional public class LocalSftpServerServiceImpl { @Autowired private LocalSftpServerDAOImpl localSftpServerDAO; //while check the server status and update the status @Scheduled(fixedDelay = 5 * 1000) public void StatusUpdate() { System.out.println("StatusUpdate LocalSSH"); //server connected LocalSftpServer server = this.localSftpServerDAO.read(); System.out.println("Show Server Information"); server.Show(); System.out.println("Server init start"); //Server init SecuredFileTransferProtocol sftp = new SecuredFileTransferProtocol(server.getIp(),server.getPort().toString(),server.getUsername(),server.getPassword(),"/"); System.out.println("Server init final"); if(sftp.Connection()) { System.out.println("local sftp connect return true"); // this.postgresqlServerDAO.setStatus(true); this.localSftpServerDAO.setStatus(true); } else { System.out.println("local sftp connect return false"); // this.postgresqlServerDAO.setStatus(false); this.localSftpServerDAO.setStatus(false); } } }
http://blog.csanchez.org/2005/09/13/writing-file-system-independent-ap/
这里问下,有已经调整好的配置么?
!!!
这里最诡异的是写的单元测试竟然能够正常运行并得出结果
package com.hava.utils; import org.apache.commons.vfs2.FileSystemException; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; /** * Created by hava on 2014/5/13. */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") public class VFSTest { String url = ""; @Test public void vfx() { String user = "root"; String pass = "123456"; String host = "192.168.1.135"; String dir = "/"; SecuredFileTransferProtocol sftp = new SecuredFileTransferProtocol(host,"22",user,pass,dir); System.out.println("sftp Connected:" + sftp.Connection()); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在搜索的过程中发现Spring原生项目中具有SFTP的支持,该项目名称为Spring Integration,感觉Spring的商业化项目支持好强大