JAVA操作FTP进行上传删除等操作

来源:互联网 发布:java sftp下载文件 编辑:程序博客网 时间:2024/04/28 13:22

JDK 1.6及以下版本 FTP操作

package com.lmd.util;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;


import com.lmd.util.listener.FTPListener;


public class FTPUplad{



private FtpClient ftp;

/**
* 变更目录 
*/
public boolean changeEdir(FtpClient ftp, String dir) {
try {
ftp.cd(dir);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}


/**
* 上传文件
*/
public boolean addFile(String path, String file, InputStream stream)
throws FtpProtocolException, IOException {
this.changeEdir(ftp, path);
TelnetOutputStream os = ftp.put(file);
byte[] by = new byte[1023];
int m = 0;
while((m=stream.read(by))!=-1){
os.write(by,0,m);
}
os.flush();
os.close();
return true;
}


/**
* 删除文件
*/
public boolean deleteFile(String path, String fileName)
throws FtpProtocolException, IOException {
// TODO Auto-generated method stub
this.ftp.cd(path);
return false;
}


/**
* 初始化FTP服务
*/
public FtpClient initFTP(String ip, int point, String user, String pwd)
throws FtpProtocolException, IOException {
FtpClient ftp = new FtpClient();
ftp.openServer(ip,point);
ftp.login(user, pwd);
this.ftp  = ftp;
return ftp;
}


}



JDK 1.7及以上版本的FTP操作


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;


import com.lmd.util.listener.FTPListener;


import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;


/**
 * FTP操作文件
 * 适用于JDK1.7及以上版本
 * @author zjt
 *
 */
public class FTPControl{


private FtpClient ftp  = null;

/**
* 切换目录
* @param ftp
* @param dir
* @return
*/
public boolean changeEdir(FtpClient ftp,String dir){
try {
ftp.changeDirectory(dir);
return true;
} catch (FtpProtocolException e) {
return false;
} catch (IOException e) {
return false;
}
}

public FTPControl(String ip,int point,String user,String pwd){
try {
this.ftp = initFTP(ip, point, user, pwd);
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public FtpClient initFTP(String ip,int point,String user,String pwd) throws FtpProtocolException, IOException{
sun.net.ftp.FtpClient ftp = sun.net.ftp.FtpClient.create(new InetSocketAddress(ip, point));
ftp.login(user,pwd.toCharArray());
this.ftp = ftp;
return ftp;
}

/**
* 增加文件
* @param path路径
* @param file文件
* @return
* @throws IOException 
* @throws FtpProtocolException 
*/
public boolean addFile(String path,String file,InputStream stream) throws FtpProtocolException, IOException{
if(!changeEdir(this.ftp, file)){
ftp.makeDirectory(path);
changeEdir(ftp, path);
}

byte[] by = new byte[1034];
int m = 0;
OutputStream out = ftp.putFileStream(file);
while((m=stream.read(by))>0){
out.write(by,0,m);
}
out.flush();
out.close();
return true;
}
/**
* 删除对应文件
* @param path 路径
* @param fileName 文件名
* @return
* @throws FtpProtocolException
* @throws IOException
*/
public boolean deleteFile(String path,String fileName) throws FtpProtocolException, IOException{
if(changeEdir(ftp, path)){
ftp.deleteFile(fileName);
return true;
}
return false;

}



}

0 0
原创粉丝点击