Ganymed ssh2 连接ftp server获取server的file
来源:互联网 发布:让我拥抱你倔强知乎 编辑:程序博客网 时间:2024/05/16 03:29
pom.xml
<dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>build210</version> </dependency>
java代码片段
import java.util.StringTokenizer;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public String runSftpConnect(HttpServletRequest request,String server,String port, String username, String password, String root,String fileName,int sku_no) throws Exception{
String EDIFile="";
Connection connection = null;
Session sess = null;
InputStream stdout = null;
BufferedReader stdoutReader = null;
try {
int ipPort = 2222;
if(port!=null&&!"".equals(port)){
ipPort = Integer.parseInt(port);
}
connection = new Connection(server,ipPort);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (!isAuthenticated) {
throw new IOException("authentication failed");
}
sess = connection.openSession();
//send commands.
String command = "";
StringBuffer output = new StringBuffer();
command = "cd "+root+";egrep -l ~"+ sku_no+"~ "+fileName;
sess.execCommand(command);
stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
stdoutReader = new BufferedReader(
new InputStreamReader(stdout));
while (true) {
String line = stdoutReader.readLine();
if(line!=null&&!"null".equals(line)){
output.append(line);
}else if (line == null) {
break;
}
}
String fileExist = "N";
if ((output.indexOf(fileName) >=0) && (output.indexOf("egrep") == -1)) {
fileExist = "Y";
}
sess.close();
if("Y".equals(fileExist)){
sess = connection.openSession();
command = "cd "+root+";grep ~"+ sku_no+"~ "+fileName;
sess.execCommand(command);
stdout = new StreamGobbler(sess.getStdout());
stdoutReader = new BufferedReader( new InputStreamReader(stdout));
output.setLength(0);
while (true) {
String line = stdoutReader.readLine();
output.append(line);
if (line == null)
break;
}
StringTokenizer st = new StringTokenizer(output.toString(), "\n");
String tmp1 = "";
while (st.hasMoreTokens()) {
tmp1 = st.nextToken();
if ((tmp1.indexOf(String.valueOf(sku_no))>=0) && (tmp1.indexOf("grep") == -1)){
EDIFile = tmp1;
break;
}
}
}
} catch (Exception e) {
request.setAttribute("error", e);
throw e;
} finally {
if (EDIFile.equals("")){
request.setAttribute("error", "There is no EDI raw data for this sku!");
}
stdoutReader.close();
stdout.close();
sess.close();
connection.close();
}
return EDIFile;
}
- Ganymed ssh2 连接ftp server获取server的file
- ganymed-ssh2
- ch.ethz.ganymed包ganymed-ssh2的使用
- FTP(File Transfer Protocol)Server
- ganymed-ssh2-build210.jar 的使用
- ganymed-ssh2简介
- Download a file from a FTP Server
- SSH2对于Java的轻量级实现方案-ganymed
- ganymed-ssh2-build250使用中遇到的问题
- ganymed-ssh2执行命令返回结果乱码的问题解决
- 使用ganymed-ssh2调用自己配置的命令
- ftp server
- ftp server
- ftp server
- ftp-server
- 說說俺的FTP Server服務器
- 获取SQL Server连接数与监视SQL Server连接状态的方法
- 【JAVA笔记】使用ganymed-ssh2-build210.jar访问Linux虚拟主机获取数据
- LayoutAnimation为ViewGroup子View添加出现特效
- [BZOJ2004]公交线路 DP+矩阵快速幂
- 【敏捷开发每日一贴】测试驱动开发
- jQuery的deferred对象详解
- DLX模板 +解树独(Dancing Links X)精确覆盖问题
- Ganymed ssh2 连接ftp server获取server的file
- STM32:USART串口通信笔记
- windows下PyCharm安装及使用
- C# WinForm开发系列
- 调试段错误 linux gdb
- POJ 1274 The Perfect Stall
- Android studio中java控制台输出中文乱码
- LeetCode-73. Set Matrix Zeroes (JAVA) 矩阵元素置0
- uvalive 5008 hdu 3717 double maze 四维bfs(好题)