sftp连接mysql_远程连接sftp,下载csv文件,读取插入数据库

该博客介绍了如何利用Java的JSch库建立SFTP连接,从远程服务器下载CSV文件,并将数据批量导入到MySQL数据库。提供了解析CSV并执行数据库插入操作的方法。
摘要由CSDN通过智能技术生成

packagecom.examstack.common.util;import com.jcraft.jsch.*;import java.io.*;import java.util.*;public classSftpUtil {private ChannelSftp sftp = null;private Session sshSession = null;privateString host;privateString username;privateString password;private intport;privateString keyFilePath;privateString passphrase;public SftpUtil(String host, String username, String password, intport, String keyFilePath, String passphrase) {this.host =host;this.username =username;this.password =password;this.port =port;this.keyFilePath =keyFilePath;this.passphrase =passphrase;

}/*** 连接sftp服务器

* 密码连接

*

*@returnChannelSftp sftp连接实例*/

publicChannelSftp connect() {

JSch jsch= newJSch();try{

jsch.getSession(username, host, port);

sshSession=jsch.getSession(username, host, port);

sshSession.setPassword(password);

Properties properties= newProperties();

properties.put("StrictHostKeyChecking", "no");

sshSession.setConfig(properties);

sshSession.connect();

Channel channel= sshSession.openChannel("sftp");

channel.connect();

sftp=(ChannelSftp) channel;

info(" ftp Connected to " + host + ":" +port);

}catch(JSchException e) {throw new RuntimeException("sftp连接失败", e);

}returnsftp;

}/*** 连接sftp服务器

* 密玥连接

*

*@returnChannelSftp sftp连接实例*/

publicChannelSftp connectKey() {

JSch jsch= newJSch();

Session session= null;

ChannelSftp channel= null;try{

jsch= newJSch();if (keyFilePath != null) {if (passphrase != null) {

jsch.addIdentity(keyFilePath, passphrase);//设置私钥

} else{

jsch.addIdentity(keyFilePath);//设置私钥

}

System.out.println("连接sftp,私钥文件路径:" +keyFilePath);

}

System.out.println("SFTP Host: " + host + "; UserName:" +username);

session=jsch.getSession(username, host, port);

System.out.println("Session 已建立.");if (password != null) {

session.setPassword(password);

}

Properties sshConfig= newProperties();

sshConfig.put("StrictHostKeyChecking", "no");

session.setConfig(sshConfig);

session.setConfig("kex", "diffie-hellman-group1-sha1");

session.connect();

System.out.println("Session 已连接.");

channel= (ChannelSftp) session.openChannel("sftp");

channel.connect();

sftp=(ChannelSftp) channel;

System.out.println("连接到SFTP成功.Host: " +host);

}catch(Exception e) {

System.out.println("连接SFTP失败:" +e);

}returnsftp;

}/*** 批量下载文件

*

*@paramremotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)

*@paramlocalPath:本地保存目录(以路径符号结束,D:\\Duansha\\sftp\\)

*@return

*/

public ListdownloadFile(String remotePath, String localPath) {

List filenames = new ArrayList();try{

connectKey();

Vector v=listFiles(remotePath);if (v.size() > 0) {

System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + (v.size() - 2));

Iterator it=v.iterator();while(it.hasNext()) {

ChannelSftp.LsEntry entry=(ChannelSftp.LsEntry) it.next();

String filename=entry.getFilename();

SftpATTRS attrs=entry.getAttrs();if (".".equals(filename) || "..".equals(filename)) {continue;

}if (!attrs.isDir()) {boolean flag = false;

String localFileName= localPath +filename;

flag=downloadFile(remotePath, filename, localPath, filename);if(flag) {

filenames.add(localFileName);

}

}

}

}

}catch(SftpException e) {

e.printStackTrace();

}finally{

}returnfilenames;

}/*** 列出目录下的文件

*

*@paramdirectory:要列出的目录

*@return*@throwsSftpException*/

public Vector> listFiles(String directory) throwsSftpException {returnsftp.ls(directory);

}/*** 下载单个文件

*

*@paramremotePath:远程下载目录(以路径符号结束)

*@paramremoteFileName:下载文件名

*@paramlocalPath:本地保存目录(以路径符号结束)

*@paramlocalFileName:保存文件名

*@return

*/

public booleandownloadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {

FileOutputStream fieloutput= null;try{

File file= new File(localPath + "/" +localFileName);

File f= newFile(localPath);if (!f.exists()) f.mkdirs();if(file.exists()) {return true;

}

fieloutput= newFileOutputStream(file);

sftp.get(remotePath+ "/" +remoteFileName, fieloutput);return true;

}catch(FileNotFoundException e) {

error(e.getMessage());

e.printStackTrace();

}catch(SftpException e) {

error(e.getMessage());

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{if (null !=fieloutput) {try{

fieloutput.close();

}catch(IOException e) {

error(e.getMessage());

e.printStackTrace();

}

}

}return false;

}/*** 上传单个文件,如果指正下载文件名则使用,否则保留原有文件名

*

*@paramremoteFilePath 远程文件路径 /tmp/xxx.txt ||xxx.txt.zip

*@paramuploadFilePath 要上传的文件 如:D:\\test\\xxx.txt*/

public voiduploadFile(String remoteFilePath, String uploadFilePath) {

info(" begin uploadFile from:" + uploadFilePath +

", to: " +remoteFilePath);

FileInputStream in= null;

connect();

String remoteFileName= "";

String remoteDir=remoteFilePath;

String localFileName= "";//远端目录确定以 / 作为目录格式

String rFileSeparator = "/";if(remoteFilePath.endsWith(rFileSeparator)) {

localFileName= uploadFilePath.substring(uploadFilePath.lastIndexOf(File.separator) + 1);

remoteFileName=localFileName;

}else{int fileNameDirSep = remoteFilePath.lastIndexOf(rFileSeparator) + 1;

remoteDir= remoteFilePath.substring(0, fileNameDirSep);

remoteFileName=remoteFilePath.substring(fileNameDirSep);

}try{

sftp.cd(remoteDir);

}catch(SftpException e) {try{

sftp.mkdir(remoteDir);

sftp.cd(remoteDir);

}catch(SftpException e1) {

error("ftp创建文件路径失败,路径为" +remoteDir);throw new RuntimeException("ftp创建文件路径失败" +remoteDir);

}

}

File file= newFile(uploadFilePath);try{

in= newFileInputStream(file);

sftp.put(in, remoteFileName);

}catch(FileNotFoundException e) {

error("文件不存在-->" +uploadFilePath);

}catch(SftpException e) {

error("sftp异常-->", e);

}finally{if (in != null) {try{

in.close();

}catch(IOException e) {

info("Close stream error." +e.getMessage());

}

}

disconnect();

}

info(">>>>>>>>>uploadFile--ftp上传文件结束>>>>>>>>>>>>>");

}/*** 关闭连接*/

public voiddisconnect() {if (this.sftp != null) {if (this.sftp.isConnected()) {this.sftp.disconnect();this.sftp = null;

info("sftp 连接已关闭!");

}

}if (this.sshSession != null) {if (this.sshSession.isConnected()) {this.sshSession.disconnect();this.sshSession = null;

info("sshSession 连接已关闭!");

}

}

}private voidinfo(String msg) {

System.out.println("info: " +msg);

}private voiderror(String msg) {

error(msg,null);

}private voiderror(String msg, Throwable e) {

System.out.println("error: " +msg);if (e != null) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值