代码ftp上传下载
21 上传代码:
import javaioFile;
import javaioFileInputStream;
import orgapachecommonsnetftpFTPClient;
import orgapachecommonsnetftpFTPReply;
public class test {
private FTPClient ftp;
/
@param path 上传到ftp服务器哪个路径下
@param addr 地址
@param port 端口号
@param username 用户名
@param password 密码
@return
@throws Exception
/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftpconnect(addr,port);
ftplogin(username,password);
ftpsetFileType(FTPClientBINARY_FILE_TYPE);
reply = ftpgetReplyCode();
if (!FTPReplyisPositiveCompletion(reply)) {
ftpdisconnect();
return result;
}
ftpchangeWorkingDirectory(path);
result = true;
return result;
}
/
@param file 上传的文件或文件夹
@throws Exception
/
private void upload(File file) throws Exception{
if(fileisDirectory()){
ftpmakeDirectory(filegetName());
ftpchangeWorkingDirectory(filegetName());
String[] files = filelist();
for (int i = 0; i < fileslength; i++) {
File file1 = new File(filegetPath()+"\\"+files[i] );
if(file1isDirectory()){
upload(file1);
ftpchangeToParentDirectory();
}else{
File file2 = new File(filegetPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
}else{
File file2 = new File(filegetPath());
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
public static void main(String[] args) throws Exception{
test t = new test();
tconnect("", "localhost", 21, "yhh", "yhhazr");
File file = new File("e:\\uploadify");
tupload(file);
}
}
22 下载代码
这里没有用到filter,如果用filter就可以过滤想要的文件。
public class Ftp { / @param args / public static void main(String[] args) { // TODO Auto-generated method stub Ftp ftp = new Ftp(); String hostname = ">
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)