public class SftpUtil {
/**
* FTP服务器IP地址
*/
private String host = "192.168.1.147";
/**
* FTP服务器端口
*/
private Integer port = 9999;
/**
* FTP服务器登录用户名
*/
private String username = "test";
/**
* FTP服务器登录密码
*/
private String password = "123456";
/**
* FTP连接超时时间
*/
private Integer timeout = 5000;
// private String ftpPicUrl="http://192.168.1.147:8086/ciipImg/";
private String ftpPicPath = "/user/img/";
public SftpUtil() {
}
private Session getSession() {
JSch jsch = new JSch();
Session session = null;
try {
jsch.getSession(username, host, port);
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect(timeout);
} catch (JSchException e) {
throw new BusinessException("SFTP Session连接JSch异常 !", e);
} catch (Exception e) {
throw new BusinessException("SFTP Session连接异常! ", e);
}
return session;
}
private ChannelSftp getChannel(Session session) {
ChannelSftp sftp = null;
try {
Channel channel = session.openChannel("sftp");
channel.connect(timeout);
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
throw new BusinessException("SFTP ", e);
} catch (Exception e) {
throw new BusinessException("", e);
}
return sftp;
}
public void upload(InputStream input, AttachFile attach) throws Exception {
Session session = getSession();
ChannelSftp sftp = getChannel(session);
String dateDir = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
try {
String fileName = attach.getFileName();
fileName = attach.getFileId() + fileName.substring(fileName.lastIndexOf("."));
sftp.cd(ftpPicPath);
cd(ftpPicPath, dateDir, sftp);
sftp.put(input, fileName);
attach.setFileSavePath(ftpPicPath + dateDir + "/" + fileName);
attach.setFileUrl(dateDir + "/" + fileName);
} catch (SftpException e) {
throw new BusinessException("文件上传失败!", e);
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
IOUtils.closeQuietly(input);
session.disconnect();
sftp.disconnect();
}
}
/**
* 长传完整名称文件
*
* @param input
* @param attach
* @throws Exception
*/
public void uploadWholeName(InputStream input, AttachFile attach) throws Exception {
Session session = getSession();
ChannelSftp sftp = getChannel(session);
String dateDir = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
try {
String fileName = java.net.URLEncoder.encode(attach.getFileName(), "utf-8");
// fileName=attach.getFileId()+fileName.substring(fileName.lastIndexOf("."));
sftp.cd(ftpPicPath);
cd(ftpPicPath, dateDir, sftp);
sftp.put(input, fileName);
attach.setFileSavePath(ftpPicPath + dateDir + "/" + fileName);
attach.setFileUrl(dateDir + "/" + fileName);
} catch (SftpException e) {
throw new BusinessException("文件上传失败!", e);
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
IOUtils.closeQuietly(input);
session.disconnect();
sftp.disconnect();
}
}
public void delete(String fileSavePath) {
Session session = getSession();
ChannelSftp sftp = getChannel(session);
try {
sftp.rm(fileSavePath);
} catch (SftpException e) {
throw new BusinessException("文件删除失败!", e);
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw new BusinessException("Exception", e);
} finally {
try {
session.disconnect();
sftp.disconnect();
} catch (Exception e) {
}
}
}
private void cd(String baseDir, String destDir, ChannelSftp sftp) {
String[] dirs = destDir.split("/");
for (String dir : dirs) {
baseDir = baseDir + dir + "/";
try {
sftp.cd(baseDir);
} catch (SftpException e) {
try {
sftp.mkdir(baseDir);
sftp.cd(baseDir);
} catch (SftpException e1) {
}
} catch (Exception e) {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
new SftpUtil().delete("/usr/local/nginx/test.jpg");
}
}