Tomcat7.0 Java SFTP 取文件报错

十林 2017-03-10 11:49:34
在Eclipse 中我可以顺利使用下面的代码SFTP下载文件
但是我把函数startFTP整合如我得web 应用代码中,就会出现报错信息“Unknown message with code "Could not determine the type of file。。。”
web 应用是部署在Tomcat 7.0上,使用的是SSH 框架。 web 应用有个定时器,定时运行函数去SFTP上取文件。
系统 win10
JDK 1.8
网络连接没有问题。
麻烦大神了!
系统输出信息如下
initial FileSystemManager successful
start download
initial SftpFileSystemConfigBuilder successful
manager.resolveFile localFile successful
Unknown message with code "Could not determine the type of file "sftp://username:***@192.168.64.37/FILE_20170308.CSV".".


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.*;
import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

import com.jcraft.jsch.*;
public class SFTPTest {

public static void main(String[] args) {

startFTP("192.168.64.37","username","1234","/","C:/Temp/", "FILE_20170308.CSV");
}
public static boolean startFTP(String serverAddress,String userId,String password,String remoteDirectory ,String localDirectory, String fileToDownload){

StandardFileSystemManager manager = new StandardFileSystemManager();

try {

//Initializes the file manager
manager.init();

//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

//Create the SFTP URI using the host name, userid, password, remote path and file name
String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" +
remoteDirectory + fileToDownload;

// Create local file object
String filepath = localDirectory + fileToDownload;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println("manager.resolveFile localFile successful");

// Create remote file object
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
System.out.println("manager.resolveFile remoteFile successful");

// Copy local file to sftp server
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
System.out.println("File download successful");

}
catch (Exception ex) {
ex.printStackTrace();
return false;
}
finally {
manager.close();
}

return true;
}
}
...全文
389 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
月明星稀灬 2017-03-11
  • 打赏
  • 举报
回复
帮顶一下!!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧