使用sftp對檔案進行上傳或者是下載
阿新 • • 發佈:2018-12-20
Java
Jsch
1.jsch官方API檢視地址(附件為需要的jar)
http://www.jcraft.com/jsch/
2.jsch簡介
JSch(Java Secure Channel)是一個SSH2的純Java實現。它允許你連線到一個SSH伺服器,並且可以使用埠轉發,X11轉發,檔案傳輸等,當然你也可以整合它的功能到你自己的應用程式。
SFTP(Secure File Transfer Protocol)安全檔案傳送協議。可以為傳輸檔案提供一種安全的加密方法。 SFTP 為 SSH的一部份,是一種傳輸檔案到伺服器的安全方式,但是傳輸效率比普通的FTP要低。
3.api常用的方法:
put(): 檔案上傳
get(): 檔案下載
cd(): 進入指定目錄
ls(): 得到指定目錄下的檔案列表
rename(): 重新命名指定檔案或目錄
rm(): 刪除指定檔案
mkdir(): 建立目錄
rmdir(): 刪除目錄
5.需要的時間輔助類,順帶記下,下次可以直接拿來用
http://www.jcraft.com/jsch/
2.jsch簡介
JSch(Java Secure Channel)是一個SSH2的純Java實現。它允許你連線到一個SSH伺服器,並且可以使用埠轉發,X11轉發,檔案傳輸等,當然你也可以整合它的功能到你自己的應用程式。
SFTP(Secure File Transfer Protocol)安全檔案傳送協議。可以為傳輸檔案提供一種安全的加密方法。 SFTP
3.api常用的方法:
put(): 檔案上傳
get(): 檔案下載
cd(): 進入指定目錄
ls(): 得到指定目錄下的檔案列表
rename(): 重新命名指定檔案或目錄
rm(): 刪除指定檔案
mkdir(): 建立目錄
rmdir(): 刪除目錄
put和get都有多個過載方法,自己看原始碼
4.jar包的下載。
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version> </dependency>5.工具程式碼的實現(僅供參考)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;
/**
* sftp 工具類
*
* @author psy
* @date 2017-7-6
* @time 下午1:39:44
* @version 1.0
*/
public class SFTPUtils
{
private static Logger log = Logger.getLogger(SFTPUtils.class.getName());
private String host;//伺服器連線ip
private String username;//使用者名稱
private String password;//密碼
private int port = 22;//埠號
private ChannelSftp sftp = null;
private Session sshSession = null;
public SFTPUtils(){}
public SFTPUtils(String host, int port, String username, String password)
{
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
public SFTPUtils(String host, String username, String password)
{
this.host = host;
this.username = username;
this.password = password;
}
/**
* 通過SFTP連線伺服器
*/
public void connect()
{
try
{
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
if (log.isInfoEnabled())
{
log.info("Session created.");
}
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
if (log.isInfoEnabled())
{
log.info("Session connected.");
}
Channel channel = sshSession.openChannel("sftp");
channel.connect();
if (log.isInfoEnabled())
{
log.info("Opening Channel.");
}
sftp = (ChannelSftp) channel;
if (log.isInfoEnabled())
{
log.info("Connected to " + host + ".");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 關閉連線
*/
public void disconnect()
{
if (this.sftp != null)
{
if (this.sftp.isConnected())
{
this.sftp.disconnect();
if (log.isInfoEnabled())
{
log.info("sftp is closed already");
}
}
}
if (this.sshSession != null)
{
if (this.sshSession.isConnected())
{
this.sshSession.disconnect();
if (log.isInfoEnabled())
{
log.info("sshSession is closed already");
}
}
}
}
/**
* 批量下載檔案
* @param remotPath:遠端下載目錄(以路徑符號結束,可以為相對路徑eg:/assess/sftp/jiesuan_2/2014/)
* @param localPath:本地儲存目錄(以路徑符號結束,D:\Duansha\sftp\)
* @param fileFormat:下載檔案格式(以特定字元開頭,為空不做檢驗)
* @param fileEndFormat:下載檔案格式(檔案格式)
* @param del:下載後是否刪除sftp檔案
* @return
*/
public List<String> batchDownLoadFile(String remotePath, String localPath,
String fileFormat, String fileEndFormat, boolean del)
{
List<String> filenames = new ArrayList<String>();
try
{
// connect();
Vector v = listFiles(remotePath);
// sftp.cd(remotePath);
if (v.size() > 0)
{
System.out.println("本次處理檔案個數不為零,開始下載...fileSize=" + v.size());
Iterator it = v.iterator();
while (it.hasNext())
{
LsEntry entry = (LsEntry) it.next();
String filename = entry.getFilename();
SftpATTRS attrs = entry.getAttrs();
if (!attrs.isDir())
{
boolean flag = false;
String localFileName = localPath + filename;
fileFormat = fileFormat == null ? "" : fileFormat
.trim();
fileEndFormat = fileEndFormat == null ? ""
: fileEndFormat.trim();
// 三種情況
if (fileFormat.length() > 0 && fileEndFormat.length() > 0)
{
if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat))
{
flag = downloadFile(remotePath, filename,localPath, filename);
if (flag)
{
filenames.add(localFileName);
if (flag && del)
{
deleteSFTP(remotePath, filename);
}
}
}
}
else if (fileFormat.length() > 0 && "".equals(fileEndFormat))
{
if (filename.startsWith(fileFormat))
{
flag = downloadFile(remotePath, filename, localPath, filename);
if (flag)
{
filenames.add(localFileName);
if (flag && del)
{
deleteSFTP(remotePath, filename);
}
}
}
}
else if (fileEndFormat.length() > 0 && "".equals(fileFormat))
{
if (filename.endsWith(fileEndFormat))
{
flag = downloadFile(remotePath, filename,localPath, filename);
if (flag)
{
filenames.add(localFileName);
if (flag && del)
{
deleteSFTP(remotePath, filename);
}
}
}
}
else
{
flag = downloadFile(remotePath, filename,localPath, filename);
if (flag)
{
filenames.add(localFileName);
if (flag && del)
{
deleteSFTP(remotePath, filename);
}
}
}
}
}
}
if (log.isInfoEnabled())
{
log.info("download file is success:remotePath=" + remotePath
+ "and localPath=" + localPath + ",file size is"
+ v.size());
}
}
catch (SftpException e)
{
e.printStackTrace();
}
finally
{
// this.disconnect();
}
return filenames;
}
/**
* 下載單個檔案
* @param remotPath:遠端下載目錄(以路徑符號結束)
* @param remoteFileName:下載檔名
* @param localPath:本地儲存目錄(以路徑符號結束)
* @param localFileName:儲存檔名
* @return
*/
public boolean downloadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
{
FileOutputStream fieloutput = null;
try
{
// sftp.cd(remotePath);
File file = new File(localPath + localFileName);
// mkdirs(localPath + localFileName);
fieloutput = new FileOutputStream(file);
sftp.get(remotePath + remoteFileName, fieloutput);
if (log.isInfoEnabled())
{
log.info("===DownloadFile:" + remoteFileName + " success from sftp.");
}
return true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (SftpException e)
{
e.printStackTrace();
}
finally
{
if (null != fieloutput)
{
try
{
fieloutput.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return false;
}
/**
* 上傳單個檔案
* @param remotePath:遠端儲存目錄
* @param remoteFileName:儲存檔名
* @param localPath:本地上傳目錄(以路徑符號結束)
* @param localFileName:上傳的檔名
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
{
FileInputStream in = null;
try
{
createDir(remotePath);
File file = new File(localPath + localFileName);
in = new FileInputStream(file);
sftp.put(in, remoteFileName);
return true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (SftpException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return false;
}
/**
* 批量上傳檔案
* @param remotePath:遠端儲存目錄
* @param localPath:本地上傳目錄(以路徑符號結束)
* @param del:上傳後是否刪除本地檔案
* @return
*/
public boolean bacthUploadFile(String remotePath, String localPath,
boolean del)
{
try
{
connect();
File file = new File(localPath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isFile()
&& files[i].getName().indexOf("bak") == -1)
{
if (this.uploadFile(remotePath, files[i].getName(),
localPath, files[i].getName())
&& del)
{
deleteFile(localPath + files[i].getName());
}
}
}
if (log.isInfoEnabled())
{
log.info("upload file is success:remotePath=" + remotePath
+ "and localPath=" + localPath + ",file size is "
+ files.length);
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
this.disconnect();
}
return false;
}
/**
* 刪除本地檔案
* @param filePath
* @return
*/
public boolean deleteFile(String filePath)
{
File file = new File(filePath);
if (!file.exists())
{
return false;
}
if (!file.isFile())
{
return false;
}
boolean rs = file.delete();
if (rs && log.isInfoEnabled())
{
log.info("delete file success from local.");
}
return rs;
}
/**
* 建立目錄
* @param createpath
* @return
*/
public boolean createDir(String createpath)
{
try
{
if (isDirExist(createpath))
{
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry)
{
if (path.equals(""))
{
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString()))
{
sftp.cd(filePath.toString());
}
else
{
// 建立目錄
sftp.mkdir(filePath.toString());
// 進入並設定為當前目錄
sftp.cd(filePath.toString());
}
}
this.sftp.cd(createpath);
return true;
}
catch (SftpException e)
{
e.printStackTrace();
}
return false;
}
/**
* 判斷目錄是否存在
* @param directory
* @return
*/
public boolean isDirExist(String directory)
{
boolean isDirExistFlag = false;
try
{
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
}
catch (Exception e)
{
if (e.getMessage().toLowerCase().equals("no such file"))
{
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
/**
* 刪除stfp檔案
* @param directory:要刪除檔案所在目錄
* @param deleteFile:要刪除的檔案
* @param sftp
*/
public void deleteSFTP(String directory, String deleteFile)
{
try
{
// sftp.cd(directory);
sftp.rm(directory + deleteFile);
if (log.isInfoEnabled())
{
log.info("delete file success from sftp.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 如果目錄不存在就建立目錄
* @param path
*/
public void mkdirs(String path)
{
File f = new File(path);
String fs = f.getParent();
f = new File(fs);
if (!f.exists())
{
f.mkdirs();
}
}
/**
* 列出目錄下的檔案
*
* @param directory:要列出的目錄
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory) throws SftpException
{
return sftp.ls(directory);
}
public String getHost()
{
return host;
}
public void setHost(String host)
{
this.host = host;
}
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 int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public ChannelSftp getSftp()
{
return sftp;
}
public void setSftp(ChannelSftp sftp)
{
this.sftp = sftp;
}
/**測試*/
public static void main(String[] args)
{
SFTPUtils sftp = null;
// 本地存放地址
String localPath = "D:/tomcat5/webapps/ASSESS/DocumentsDir/DocumentTempDir/txtData/";
// Sftp下載路徑
String sftpPath = "/home/assess/sftp/jiesuan_2/2014/";
List<String> filePathList = new ArrayList<String>();
try
{
sftp = new SFTPUtils("10.163.201.115", "tdcp", "tdcp");
sftp.connect();
// 下載
sftp.batchDownLoadFile(sftpPath, localPath, "ASSESS", ".txt", true);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
sftp.disconnect();
}
}
}
5.需要的時間輔助類,順帶記下,下次可以直接拿來用
/**
* 時間處理工具類(簡單的)
* @author Aaron
* @date 2014-6-17
* @time 下午1:39:44
* @version 1.0
*/
public class DateUtil {
/**
* 預設時間字串的格式
*/
public static final String DEFAULT_FORMAT_STR = "yyyyMMddHHmmss";
public static final String DATE_FORMAT_STR = "yyyyMMdd";
/**
* 獲取系統時間的昨天
* @return
*/
public static String getSysTime(){
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DATE, ca.get(Calendar.DATE)-1);
Date d = ca.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String a = sdf.format(d);
return a;
}
/**
* 獲取當前時間
* @param date
* @return
*/
public static String getCurrentDate(String formatStr)
{
if (null == formatStr)
{
formatStr=DEFAULT_FORMAT_STR;
}
return date2String(new Date(), formatStr);
}
/**
* 返回年月日
* @return yyyyMMdd
*/
public static String getTodayChar8(String dateFormat){
return DateFormatUtils.format(new Date(), dateFormat);
}
/**
* 將Date日期轉換為String
* @param date
* @param formatStr
* @return
*/
public static String date2String(Date date, String formatStr)
{
if (null == date || null == formatStr)
{
return "";
}
SimpleDateFormat df = new SimpleDateFormat(formatStr);
return df.format(date);
}
}
在使用工具類上傳的時候,需要開啟伺服器,方可成功。必要的時候可以關不防火牆。