- 浏览: 241931 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (101)
- js (10)
- java (39)
- HTTP请求:GET与POST方法的区别(转) (1)
- Freemarker 语法规则 (1)
- AJAX级联菜单实例 (1)
- oralce (1)
- myeclipse (5)
- struts (12)
- sql存储过程基础(转) (4)
- JBPM (1)
- mysql (4)
- hibernate (3)
- ibatis (4)
- spring (4)
- 计算机技术 (1)
- nosql (1)
- sqlserver (1)
- servlet (1)
- 拦截器 (1)
- andriod 开发 (1)
- 程序员 (0)
- 多线程 (2)
- Jenkins (1)
- zk (1)
- JPA (2)
最新评论
-
zhangzh888:
怎么下载 啊 都没有看见文件
sftp处理文件 -
wx_hello:
怎么得到文件的属性呢? 比如文件的新建时间
sftp处理文件 -
HappyVeryGood:
“运行时异常(即非受控异常)自动强制执行整个逻辑工作单元的回滚 ...
事物管理,spring事物详解,spring @transactional -
skeely1234:
感谢分享,太帅了
eclipse下修改项目名导致tomcat内发布名不一致的解决方法
最近工作涉及sftp处理文件,写了个工具类,代码已经测试。请需要的同仁自行下载,根据自己需要,修改加工。附件有参考源码及必要jar包。
Java代码
package nontax.helper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
/**
* 提供SFTP处理文件服务
* @author Tonny
* @since Apr.27 2012
*
*/
public class SFtpHelper {
private JSch jSch = null;
private ChannelSftp sftp = null;//sftp主服务
private Channel channel = null;
private Session session = null;
private String hostName="127.0.0.1";//远程服务器地址
private int port=22;//端口
private String userName="Tonny";//用户名
private String password="123";//密码
public SFtpHelper(String hostName, int port, String userName,
String password){
this.hostName=hostName;
this.port=port;
this.userName=userName;
this.password=password;
}
/**
* 连接登陆远程服务器
* @return
*/
public boolean connect() throws Exception{
try {
jSch= new JSch();
session = jSch.getSession(userName, hostName, port);
session.setPassword(password);
session.setConfig(this.getSshConfig());
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("登陆成功:"+sftp.getServerVersion());
} catch (JSchException e) {
System.err.println("SSH方式连接FTP服务器时有JSchException异常!");
System.err.println(e.getMessage());
throw e;
}
return true;
}
/**
* 关闭连接
* @throws Exception
*/
private void disconnect() throws Exception {
try{
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
}catch(Exception e){
throw e;
}
}
/**
* 获取服务配置
* @return
*/
private Properties getSshConfig()throws Exception{
Properties sshConfig=null;
try{
sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
}catch(Exception e){
throw e;
}
return sshConfig;
}
/**
* 下载远程sftp服务器文件
* @param remotePath
* @param remoteFilename
* @param localFilename
* @return
*/
public boolean downloadFile(String remotePath,
String remoteFilename,String localFilename)throws SftpException,
IOException, Exception{
FileOutputStream output = null;
boolean success = false;
try {
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}
File localFile = new File(localFilename);
//有文件和下载文件重名
if (localFile.exists()) {
System.err.println("文件: " + localFilename + " 已经存在!");
return success;
}
output = new FileOutputStream(localFile);
sftp.get(remoteFilename, output);
success = true;
System.out.println("成功接收文件,本地路径:"+localFilename);
} catch (SftpException e) {
System.err.println("接收文件时有SftpException异常!");
System.err.println(e.getMessage());
return success;
} catch (IOException e) {
System.err.println("接收文件时有I/O异常!");
System.err.println(e.getMessage());
return success;
} finally {
try {
if (null != output) {
output.close();
}
//关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
}
return success;
}
/**
* 上传文件至远程sftp服务器
* @param remotePath
* @param remoteFilename
* @param localFileName
* @return
*/
public boolean uploadFile(String remotePath,
String remoteFilename, String localFileName)throws SftpException,Exception {
boolean success = false;
FileInputStream fis=null;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}
File localFile = new File(localFileName);
fis = new FileInputStream(localFile);
// 发送文件
sftp.put(fis, remoteFilename);
success = true;
System.out.println("成功发送文件,本地路径:"+localFileName);
} catch (SftpException e) {
System.err.println("发送文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
throw e;
} catch (Exception e) {
System.err.println("发送文件时有异常!");
System.err.println(e.getMessage());
throw e;
} finally {
try {
if (null != fis) {
fis.close();
}
//关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
}
return success;
}
/**
* 上传文件至远程sftp服务器
* @param remotePath
* @param remoteFilename
* @param input
* @return
*/
public boolean uploadFile(String remotePath,
String remoteFilename, InputStream input)throws SftpException,Exception {
boolean success = false;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}
// 发送文件
sftp.put(input, remoteFilename);
success = true;
} catch (SftpException e) {
System.err.println("发送文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
throw e;
} catch (Exception e) {
System.err.println("发送文件时有异常!");
System.err.println(e.getMessage());
throw e;
} finally {
try {
if (null != input) {
input.close();
}
//关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
}
return success;
}
/**
* 删除远程文件
* @param remotePath
* @param remoteFilename
* @return
* @throws Exception
*/
public boolean deleteFile(String remotePath, String remoteFilename) throws Exception {
boolean success = false;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}
// 删除文件
sftp.rm(remoteFilename);
System.err.println("删除远程文件"+remoteFilename+"成功!");
success = true;
} catch (SftpException e) {
System.err.println("删除文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
return success;
} catch (Exception e) {
System.err.println("删除文件时有异常!");
System.err.println(e.getMessage());
return success;
} finally {
//关闭连接
disconnect();
}
return success;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
*测试方法
*
*/
public static void main(String [] args){
try{
SFtpHelper sftp=new SFtpHelper("192.168.1.2",22,"ftp","sftp_test");
System.out.println(new StringBuffer().append(" 服务器地址: ").append(sftp.getHostName()).append(" 端口:")
.append(sftp.getPort()).append("用户名:").append(sftp.getUserName()).append("密码:")
.append(sftp.getPassword().toString()));
sftp.connect();
// sftp.downloadFile("\\", "test.txt", "D:\\work\\test.txt");
// sftp.uploadFile("\\", "test.txt", "D:\\work\\readMe.txt");
// sftp.deleteFile("\\", "test.txt");
}catch(Exception e){
System.out.println("异常信息:" + e.getMessage());
}
}
}
发表评论
-
习惯的开发错误
2014-09-09 17:25 513在一个包的下面 创建一个test.java 文件 这样一个小 ... -
得到指定年份的所有周末
2014-08-20 18:18 1226/** * 得到指定年份的所有周末 */ publi ... -
对对字符串可能出现报空指针的小问题
2014-04-14 14:42 911今天很是郁闷啊,遇到一个基础的问题比对字符串的两种写法: ... -
Java 单例模式详解(转)
2014-03-26 16:52 845概念: java中单例 ... -
往文件里写入字符串
2014-01-20 13:52 1174package ab; import java.io.Bu ... -
Java数组,去掉重复值、增加、删除数组元素
2014-01-02 14:18 5185import java.util.List; import ... -
java定时器的使用(Timer)
2013-10-14 16:42 2466java定时器的使用(Timer) 2008-02-14 13 ... -
JSch - Java实现的SFTP(文件上传详解篇)(转)
2013-10-14 16:40 3466JSch是Java Secure Channel的缩写。JSc ... -
jvm
2013-09-30 15:03 787网上看到一位javaeye的同志写的文章,感觉总结的比较好,虽 ... -
java BigDecimal的使用和四舍五入及格式规范(精准数据)
2013-06-17 15:37 21598• Java中的简单浮点数类型float和double不能够进 ... -
servlet拦截器代码
2013-03-29 13:45 22711- 实现Servlet.Filter接口 public cl ... -
session 超时的时间设置
2013-03-22 14:47 1002为单个Web应用 配置超时时间可以在web.xml中使用< ... -
Calendar 获取日期
2013-01-23 10:44 1347Calendar 获取日期 如果想得到某个星期几是什么日期, ... -
JAVA帮助文档全系列
2013-01-05 11:02 0JAVA帮助文档全系列 JDK1.5 JDK1.6 JD ... -
Cannot create a server using the selected type
2012-08-27 11:02 0eclipse中安装tomcat服务器,报错" Ca ... -
线程池(jdk实现)
2012-07-10 15:01 0Sun在Java5中,对Java线程的类库做了大量的扩展,其中 ... -
遍历集合
2012-06-26 17:28 1090* * To change this template, c ... -
(转)Java 序列化
2012-06-26 14:55 1968当我们需要序列化一个J ... -
权限控制的发散性思维
2012-06-15 17:31 1008权限控制的讨论 http://www.iteye.com ... -
(转载)Java设计模式的三大块讲解
2012-06-15 17:27 1225转载自http://www ...
评论