public class FtpUtils {
private FTPClient fc; //--FTP客户端
private String ftphost; //--FTP服务器
private String ftpuser; //--用户名
private String ftppass; //--密码
private String ftppath; //--路径
private int ftpport; //--端口
private int silent;
private String pasv;
private String ftpssl;
public void setFtpValues(String ftphost, String ftpuser, String ftppass, String ftppath, int ftpport, String ftpssl, int silent, String pasv){
this.ftphost = ftphost;
this.ftppass = ftppass;
this.ftppath = ftppath;
this.pasv = pasv;
this.ftpport = ftpport;
this.ftpuser = ftpuser;
this.silent = silent;
this.ftpssl = ftpssl;
}
/**
* 判断FTP服务器是否从在
* @return
*/
public boolean isEmpty(){
if(ftphost==null||ftphost.equals("")){
return true;
}
return false;
}
/**
* 连接FTP服务器
* @return
*/
public String connectToFtpServer(){
if ((ftphost==null)||(ftphost.equals(""))) return "ftp_service_nameerror";
if(fc!=null){
try {
fc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
fc = new FTPClient();
if(ftpssl.equals("1")){
SSLServerSocketFactory sslserverfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLSocketFactory sslfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
fc.setServerSocketFactory(sslserverfactory);
fc.setSocketFactory(sslfactory);
}
return connectToServer();
}
/**
* 连接FTP服务器
* @return
*/
private String connectToServer(){
try{
fc.connect(this.ftphost, this.ftpport);
if(!fc.login(ftpuser, ftppass)){
return "ftp_service_connect";
}
if(pasv.equals("1")){
fc.pasv();
}
if(silent!=0){
fc.setDataTimeout(silent);
}
if(!fc.changeWorkingDirectory(ftppath)){
return "ftp_directory";
}
}catch(FtpLoginException e){
return "ftp_connect_access";
}catch(IOException e){
return "ftp_connect_fail";
}catch(SecurityException e){
return "ftp_noaccess";
}
return "";
}
/**
* 测试连接
* @return
*/
public boolean isConnect(){
if(fc==null||!fc.isConnected()){
return false;
}else{
try {
String path = fc.printWorkingDirectory();
if(path==null){
return false;
}
} catch (IOException e) {
return false;
}
return true;
}
}
/**
* 关闭FTP连接
*/
public void closeFtpConnect(){
if (fc!=null){
try{
fc.disconnect();
}catch(Exception e){
}finally{
fc = null;
}
}
}
/**
* 创建目录
* @param newdir
* @return
*/
public boolean dftp_mkdir(String newdir){
boolean makebool;
try{
makebool = fc.makeDirectory(newdir);
return makebool;
}catch(Exception e){
return false;
}
}
/**
* 删除目录
* @param newdir
* @return
*/
public boolean dftp_rmdir(String newdir){
try{
return fc.removeDirectory(newdir);
}catch(Exception e){
return false;
}
}
/**
* 删除文件
* @param newdir
* @return
*/
public boolean dftp_delete(String newdir){
try{
fc.changeWorkingDirectory(ftppath);
return fc.deleteFile(newdir);
}catch(Exception e){
return false;
}
}
/**
* 修改权限
* @param newdir
* @return
*/
public boolean dftp_site(String newdir){
String cmd_mkdir= "chmod 0777 "+ftppath+"/"+newdir+"\r\n";
try{
return fc.sendSiteCommand(cmd_mkdir);
}catch(Exception e){
return false;
}
}
/**
* 改变工作路径
* @param dir
* @return
*/
public boolean dftp_chdir(String dir){
boolean workboolean;
try{
workboolean = fc.changeWorkingDirectory(dir);
return workboolean;
}catch(Exception e){
return false;
}
}
public boolean isdirwork(String directory){
try{
String path = fc.printWorkingDirectory();
String targetpath = ftppath+"/"+directory;
if(targetpath.startsWith(".")){
targetpath = targetpath.substring(1);
}
return targetpath.equals(path);
}catch(Exception e){
return false;
}
}
public void cdrootdir(){
try {
fc.changeWorkingDirectory(ftppath);
} catch (IOException e) {
}
}
public boolean put(String sourcename, String targetname, boolean test, String directory){
if(test){
connectToServer();
}
try {
if(!test){
if(!isdirwork(directory)){
if(!fc.changeWorkingDirectory(ftppath)){
return false;
}
if(fc.changeWorkingDirectory(directory)){
return false;
}
}
}
fc.setBufferSize(3072);
fc.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = new FileInputStream(sourcename);
boolean strore = fc.storeFile(targetname, is);
is.close();
return strore;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean get(String local_file,String remote_file){
try {
fc.setBufferSize(2076);
fc.setFileType(FTPClient.BINARY_FILE_TYPE);
FileOutputStream os = new FileOutputStream(local_file);
fc.retrieveFile(remote_file, os);
os.close();
return true;
} catch (IOException e) {
return false;
}
}
public boolean readfile(String url,OutputStream os){
InputStream in=null;
URL servletURL =null;
try {
servletURL = new URL(url);
servletURL.openConnection();
in = servletURL.openStream();
if(os!=null){
byte[] bytes=new byte[1024];
int c;
while ((c=in.read(bytes))!=-1){
os.write(bytes,0,c);
}
}
return true;
} catch (Exception e) {
return false;
}finally{
try{
if(in!=null){
in.close();
}
}catch(Exception e){}
servletURL=null;
}
}
}