JSCH实现文件上传的代码实例
package com.vcredit.ddcash.monitor.sendmail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
/**
* created by ding 2016-12-02 17:28 Friday
* */
public class FtpsFileList {
private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);
public static void main(String[] args) {
listFileNames("****","**", "root", "}KCxBZC6IO]hWC>CfDkgD<41WhTP(", "/usr");
}
private static List<String> listFileNames(String host, int port, String username, final String password,
String dir) {
List<String> list = new ArrayList<String>();
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
LOG.debug("Session connected!");
channel = sshSession.openChannel("sftp");
channel.connect();
LOG.debug("Channel connected!");
sftp = (ChannelSftp) channel;
// 文件上传 下面这种方式也可以
// File file = new File("15888888_back_cut.jpg");
// file.getAbsolutePath();
// if (!file.exists()){
// file.createNewFile();
// }
// FileInputStream in = new FileInputStream(file);
// sftp.put(in, "/usr/local/ding/15888888_back_cut.jpg1");
// sftp.put(src, dst);//src本地文件,dst 服务器上的文件
sftp.put("15888888_back_cut.jpg", "/usr/local/ding/");
// 文件下载
sftp.cd("/usr/local/ding/");
// sftp.get(src, dst); src为服务器上的文件,dst问本地的目录,如果只是路径 ,默认文件名一致
sftp.get("/usr/local/ding/15888888_back_cut.jpg", "D:/");
Vector<?> vector = sftp.ls(dir);
for (Object item : vector) {
LsEntry entry = (LsEntry) item;
System.out.println(entry.getFilename());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeChannel(sftp);
closeChannel(channel);
closeSession(sshSession);
}
return list;
}
private static void closeChannel(Channel channel) {
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
}
private static void closeSession(Session session) {
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
}
JSCH实现文件上传的代码实例的更多相关文章
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- JavaWeb实现文件上传下载功能实例解析 (好用)
转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...
- web service上传参数代码实例
web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...
- php文件上传——php经典实例
php文件上传——php经典实例 表单页 <html> <head> <title>文件上传</title> <meta charset='ut ...
- WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现
WebLogic 任意文件上传远程代码执行_CVE-2018-2894漏洞复现 一.漏洞描述 Weblogic管理端未授权的两个页面存在任意上传getshell漏洞,可直接获取权限.Oracle 7月 ...
- Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)
所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...
- JSCH实现文件上传下载至sftp服务器
文件服务器采用FreeSSHd,文件服务器配置就不细说了. 直接上代码,该代码可以直接使用. import com.jcraft.jsch.*; import java.io.InputStream; ...
- js 实现 input type="file" 文件上传示例代码
在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...
- Java文件上传之SpringMVC实例
一.配置文件 web.xml <?xml version="1.0" encoding="UTF-8"?><web-app version=& ...
随机推荐
- sqoop中,如果数据中本身有换行符,会导致数据错位
sqoop中,如果数据中本身有换行符,会导致数据错位: 解决办法: 在sqoop import时修改配置文件 sudo -u hive sqoop import --connect jdbc:mysq ...
- LoadRunner脚本实例来验证参数化的取值
LoadRunner脚本实例来验证参数化的取值 SINM {3]!G0问题提出: 主要想试验下,在Controller中,多个用户,多次迭代中参数的取值.51Testing软件测试网(['H5f,d ...
- Liferay 6.2 改造系列之九:修改用户信息填写规则
为了让用户信息更贴近实际需求,修改如下信息: 1.让登录名可以使用数字.“.”.“_”等 在/portal-master/portal-impl/src/portal.properties文件中,有如 ...
- java,我准备好了
1.你对自己的未来有什么规划?做了哪些准备? 长期上学好公务员和本专业知识,将来能找一份好工作,并能在职务上履行好相称的工作.短期上在下一学期拿一等奖学金,尽快入党,考出英语四级和二级c语言.任何成就 ...
- Codeforces 528D Fuzzy Search(FFT)
题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...
- unity gizmo绘制圆形帮助调试
using UnityEngine; using System.Collections; using System; public class LearnGrazio : MonoBehaviour ...
- Android Drawable
1. Shape 属性: (1) solid ( 填充 ) 参数:android:color ( 填充的颜色 ) (2) gradient ( 渐变 ) 参数:android:startColor ( ...
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- ACM: HDU 1028 Working out 解题报告-DP
Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- ACM : Travel-并查集-最小生成树 + 离线-解题报告
Travel Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...