SFTP 작업 서버 파일
11168 단어 자바
package com.socket;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SocketFactory;
/**
* SFTP
* @author zhaohaoa
*/
public class SFTPConnection {
// private static Logger log = LoggerFactory.getLogger(SFTPConnection.class);
private Session session;
private String host;
private int port;
private String username;
private String password;
private int timeout;
private String localbind;
/**
*
* @param host
* @param port
* @param username
* @param password
* @param timeout session
*/
public SFTPConnection(String host, int port, String username, String password, int timeout) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.timeout = timeout;
}
/**
*
* @param host
* @param port
* @param username
* @param password
*/
public SFTPConnection(String host, int port, String username, String password) {
this(host, port, username, password, 60 * 1000);
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getLocalbind() {
return localbind;
}
public void setLocalbind(String localbind) {
this.localbind = localbind;
}
/**
* , socket
*/
public static class SFTPSocketFactoty implements SocketFactory {
private String localbind;
private int timeout;
public SFTPSocketFactoty(String localbind, int timeout) {
this.localbind = localbind;
this.timeout = timeout;
}
@Override
public Socket createSocket(String s, int i) throws IOException, UnknownHostException {
Socket socket = new Socket();
socket.bind(new InetSocketAddress(localbind, 0));
socket.connect(new InetSocketAddress(s, i), timeout);
return socket;
}
@Override
public InputStream getInputStream(Socket socket) throws IOException {
return socket.getInputStream();
}
@Override
public OutputStream getOutputStream(Socket socket) throws IOException {
return socket.getOutputStream();
}
}
/**
* sftp
* , session
*/
public void disconnect() {
if (isConnected()) {
try {
session.disconnect();
// log.info("sftp disconnect host " + host + ".");
} catch (Exception e) {
// log.error("sftp disconnect host " + host + ". happen errors.", e);
}
}
}
/**
*
*/
public boolean isConnected() {
return session != null && session.isConnected();
}
/**
* sftp
* @throws Exception
*/
public void connect() throws Exception {
try {
// log.info("sftp connect host " + host + "@" + username);
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setTimeout(timeout);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
if (StringUtils.isNotEmpty(localbind)) {
session.setSocketFactory(new SFTPSocketFactoty(localbind, timeout));
}
session.connect();
// log.info("sftp connect success!");
} catch (Exception e) {
// log.error("sftp connect fail!");
throw e;
}
}
/**
*
* @param remoteDir
* @param localFile ( )
*/
public void upload(String remoteDir, String localFile) throws Exception {
if (!isConnected()) {
connect();
}
ChannelSftp sftp = null;
FileInputStream fis = null;
try {
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
sftp.setFilenameEncoding("GBK"); // ,
if (StringUtils.isNotEmpty(remoteDir)) {
sftp.cd(remoteDir);
}
File file = new File(localFile);
fis = new FileInputStream(file);
// log.info("upload file[{}] to remote dir[{}]", localFile, remoteDir);
sftp.put(fis, file.getName());
// log.info("upload file successful.");
} finally {
if (fis != null) {
fis.close();
}
if (sftp != null && sftp.isConnected()) {
sftp.disconnect();
}
}
}
/**
*
* @param remoteDir
* @param keyword , .jpg jpg
* @return list
* @throws Exception
*/
public List
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.