本
文为实现对linux服务器文件的操作。windows服务器不支持。
引
入jar包:jsch-0.1.42.jar
- package com.csvreader.sftp;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.List;
- import java.util.Properties;
- import java.util.Vector;
-
- import org.junit.Test;
-
- 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;
-
- public class SftpUtil {
-
-
-
-
-
-
-
-
-
- public ChannelSftp connect(String host, int port, String username,String password) throws JSchException {
-
- ChannelSftp nChannelSftp = null;
-
- JSch nJSch = new JSch();
-
- Session nSShSession = nJSch.getSession(username, host, port);
- System.out.println("Session创建成功");
-
- nSShSession.setPassword(password);
-
- Properties nSSHConfig = new Properties();
-
- nSSHConfig.put("StrictHostKeyChecking", "no");
-
- nSShSession.setConfig(nSSHConfig);
-
- nSShSession.connect();
- System.out.println("Session已连接");
-
- Channel channel = nSShSession.openChannel("sftp");
-
- channel.connect();
- nChannelSftp = (ChannelSftp) channel;
- System.out.println("连接到主机" + host + ".");
- return nChannelSftp;
- }
-
-
-
-
-
-
-
-
- public void renameFile(String directory, String oldname, String newname,ChannelSftp sftp) {
- try {
- sftp.cd(directory);
- sftp.rename(oldname, newname);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
- public void upload(String directory, String uploadFile, ChannelSftp sftp) {
- try {
- sftp.cd(directory);
- File file = new File(uploadFile);
- sftp.put(new FileInputStream(file), file.getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
- public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {
- try {
- sftp.cd(directory);
- File file = new File(saveFile);
- sftp.get(downloadFile, new FileOutputStream(file));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
- public void delete(String directory, String deleteFile, ChannelSftp sftp) {
- try {
- sftp.cd(directory);
- sftp.rm(deleteFile);
- System.out.println("删除成功");
- } catch (Exception e) {
- System.out.println("删除失败");
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
- public Vector listFiles(String directory, ChannelSftp sftp)throws SftpException {
- return sftp.ls(directory);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- @Test
- public List<String> downloadDirFile(String viDirectory, String viHost,int viPort, String viUserName, String viPassWord, String viSaveDir) {
- ChannelSftp nChannelSftp = null;
- List<String> nFileNameList = null;
- try {
-
- SftpUtil nSftpUtil = new SftpUtil();
-
- nChannelSftp = nSftpUtil.connect(viHost, 22, viUserName, viPassWord);
-
- Vector nVector = nChannelSftp.ls(viDirectory);
-
- for (int i = 0; i < nVector.size(); i++) {
-
- nChannelSftp.cd(viDirectory);
-
- String nFileName = nVector.get(i).toString().substring(56, nVector.get(i).toString().length());
- if (!nFileName.contains("csv")) {
- continue;
- }
- File nFile = new File(viSaveDir + File.separator + nFileName);
-
- nChannelSftp.get(nFileName, new FileOutputStream(nFile));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- nChannelSftp.disconnect();
- }
- return nFileNameList;
- }