Skip to content

Instantly share code, notes, and snippets.

@adilkurniaramdan
Last active December 17, 2015 00:48
Show Gist options
  • Save adilkurniaramdan/5523262 to your computer and use it in GitHub Desktop.
Save adilkurniaramdan/5523262 to your computer and use it in GitHub Desktop.
package com.qbiel.service;
import java.io.File;
import java.util.Vector;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.SftpException;
import com.qbiel.conn.SftpConn;
public class ServiceSftp {
//menampilkan list direktory pada ftp
public Vector<LsEntry> getList(String path) {
ChannelSftp channelSftp = null;
Vector<LsEntry> vector = null;
try {
channelSftp = SftpConn.getChannel();
vector = channelSftp.ls(path);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return vector;
}
//membuat folder baru pada ftp
private void mkdir(String folder) {
// TODO Auto-generated method stub
ChannelSftp channelSftp = null;
boolean exist = ChangeDirectory(folder);
if (!exist) {
try {
channelSftp.mkdir(folder);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//melakukan transfer file pada dari client ke ftp
public void transfer(File file, String destination) {
ChannelSftp channelSftp = null;
try {
channelSftp.put(file.getAbsolutePath(),
destination + "/" + file.getName());
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//menghapus file atau folder
public boolean removeFile(String path, String fileName) {
ChannelSftp channelSftp = null;
try {
channelSftp.rm(path + "/" + fileName);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
//pindah direktory
public boolean ChangeDirectory(String path) {
ChannelSftp channelSftp = null;
try {
channelSftp = SftpConn.getChannel();
channelSftp.cd(path);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment