java FTP文件下载操作 我想进行如下操作,给定一个FTP文件夹路径,程序自动搜索文件夹下文件,并打印文件名。请各位指点,最好附代码,谢谢~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args){ File file = new File("d:/111"); test(file); } public static void test(File file){ File[] filelist = file.listFiles(); for(File f : filelist){ if(f.isDirectory()) test(f); else System.out.println(f.getName()); } } import sun.net.*;import sun.net.ftp.*;import java.io.*;import java.util.*;public class FtpUtils { private FtpClient ftpclient; private String ipAddress; private int port; private String username; private String password; /** * 构造函数 * @param ip String 机器IP * @param port String 机器FTP端口号 * @param username String FTP用户名 * @param password String FTP密码 * @throws Exception */ public FtpUtils(String ip, int port, String username, String password) throws Exception { this.ipAddress = ip; this.port = port; this.ftpclient = new FtpClient(ipAddress, port); this.username = username; this.password = password; } /** * 构造函数 * @param ip String 机器IP,默认端口为21 * @param username String FTP用户名 * @param password String FTP密码 * @throws Exception */ public FtpUtils(String ip, String username, String password) throws Exception { this(ip,21,username,password); } /** * 登录FTP服务器 * @throws Exception */ public void login() throws Exception { ftpclient.login(username, password); } /** * 退出FTP服务器 * @throws Exception */ public void logout() throws Exception { ftpclient.closeServer(); /*用ftpclient.closeServer()断开FTP出错时用下更语句退出 ftpclient.sendServer("QUIT\r\n"); int reply = ftpclient.readServerResponse(); //取得服务器的返回信息 */ } /** * 取得指定目录下的所有文件名,不包括目录名称 * 分析nameList得到的输入流中的数,得到指定目录下的所有文件名 * @param fullPath String * @return ArrayList * @throws Exception */ public ArrayList fileNames(String fullPath) throws Exception { ftpclient.ascii(); //注意,使用字符模式 TelnetInputStream list = ftpclient.nameList(fullPath); byte[] names = new byte[2048];//如果文件数目很多,有可能溢出 int bufsize = 0; bufsize = list.read(names, 0, names.length); //从流中读取 list.close(); ArrayList namesList = new ArrayList(); int i = 0; int j = 0; while (i < bufsize ) { if (names[i] == 10) { //字符模式为10,二进制模式为13 String tempName = new String(names, j, i - j); namesList.add(tempName); //j = i + 2; //上一次位置二进制模式 j = i + 1; //上一次位置字符模式 } i = i + 1; } return namesList; } /** * 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP * 上传文件时保证目录的存在目录格式必须以"/"根目录开头 * @param pathList String * @throws Exception */ public void buildRemoteDir(String pathList) throws Exception { ftpclient.ascii(); StringTokenizer s = new StringTokenizer(pathList, "/"); //sign int count = s.countTokens(); String pathName = ""; while (s.hasMoreElements()) { pathName = pathName + "/" + (String) s.nextElement(); try { ftpclient.sendServer("XMKD " + pathName + "\r\n"); } catch (Exception e) { e = null; } int reply = ftpclient.readServerResponse(); } ftpclient.binary(); } /** * 上传文件到FTP服务器,remote路径以FTP服务器的"/"开始,带文件名、 * 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖 * @param local String * @param remote String * @throws Exception */ public void upFile(String local, String remote) throws Exception { buildRemoteDir(remote.substring(0, remote.lastIndexOf("/"))); ftpclient.binary(); //此行代码必须放在buildRemoteDir之后 TelnetOutputStream ftpOut = ftpclient.put(remote); InputStream in = new FileInputStream(local);// byte[] buf = new byte[204800]; int bufsize = 0; while((bufsize = in.read(buf, 0, buf.length)) != -1){ ftpOut.write(buf, 0, bufsize); } in.close(); ftpOut.close(); } public void buildLocalDir(String fullPath) throws Exception { if(fullPath.lastIndexOf("/")<=0)return; String path=fullPath.substring(0,fullPath.lastIndexOf("/")); File f= new File(path); if(!f.exists()){ f.mkdirs(); } } public void downFile(String remote,String local) throws Exception { buildLocalDir(local); ftpclient.binary(); //此行代码必须放在buildRemoteDir之后 OutputStream out=new FileOutputStream(new File(local)); TelnetInputStream ftpIn = ftpclient.get(remote); byte[] buff=new byte[204800]; int len=0; while((len=ftpIn.read(buff))!=-1){ out.write(buff,0,len); } out.close(); ftpIn.close(); } public static void main(String args[])throws Exception{ FtpUtils upfile=new FtpUtils("192.168.187.130","root","1-1=0"); upfile.login(); List list=upfile.fileNames("/"); System.out.println(list); upfile.upFile("FtpUtils.java","/root/xjs/test/FtpUtils.java" ); upfile.downFile("/root/xjs/2.txt","xjs/2.txt" ); upfile.logout(); }} 菜鸟求问 java socket服务端如何检查客户端是处于输入还是输出? 读取取txt最后一行数据 简单链表出错,帮看看,谢谢 如何给程序添加一个起始界面 Jbuilder怎样生成.exe文件? 关于io的问题 java程序与虚拟机问题 初学JAVA的问题。 加入JAVAFANS群号~~大家一起学习~(还没有满要尽快啊~) NetBeans 怎么搭建Java3D 开发、运行环境 请教一个分割字符串插入数据库的问题
public static void main(String[] args){
File file = new File("d:/111");
test(file);
}
public static void test(File file){
File[] filelist = file.listFiles();
for(File f : filelist){
if(f.isDirectory()) test(f);
else System.out.println(f.getName());
}
}
import sun.net.*;
import sun.net.ftp.*;
import java.io.*;
import java.util.*;
public class FtpUtils {
private FtpClient ftpclient;
private String ipAddress;
private int port;
private String username;
private String password;
/**
* 构造函数
* @param ip String 机器IP
* @param port String 机器FTP端口号
* @param username String FTP用户名
* @param password String FTP密码
* @throws Exception
*/
public FtpUtils(String ip, int port, String username, String password) throws
Exception {
this.ipAddress = ip;
this.port = port;
this.ftpclient = new FtpClient(ipAddress, port);
this.username = username;
this.password = password;
}
/**
* 构造函数
* @param ip String 机器IP,默认端口为21
* @param username String FTP用户名
* @param password String FTP密码
* @throws Exception
*/
public FtpUtils(String ip, String username, String password) throws
Exception {
this(ip,21,username,password);
}
/**
* 登录FTP服务器
* @throws Exception
*/
public void login() throws Exception {
ftpclient.login(username, password);
} /**
* 退出FTP服务器
* @throws Exception
*/
public void logout() throws Exception {
ftpclient.closeServer();
/*用ftpclient.closeServer()断开FTP出错时用下更语句退出
ftpclient.sendServer("QUIT\r\n");
int reply = ftpclient.readServerResponse(); //取得服务器的返回信息
*/
}
/**
* 取得指定目录下的所有文件名,不包括目录名称
* 分析nameList得到的输入流中的数,得到指定目录下的所有文件名
* @param fullPath String
* @return ArrayList
* @throws Exception
*/
public ArrayList fileNames(String fullPath) throws Exception {
ftpclient.ascii(); //注意,使用字符模式
TelnetInputStream list = ftpclient.nameList(fullPath);
byte[] names = new byte[2048];//如果文件数目很多,有可能溢出
int bufsize = 0;
bufsize = list.read(names, 0, names.length); //从流中读取
list.close();
ArrayList namesList = new ArrayList();
int i = 0;
int j = 0;
while (i < bufsize ) {
if (names[i] == 10) { //字符模式为10,二进制模式为13
String tempName = new String(names, j, i - j);
namesList.add(tempName);
//j = i + 2; //上一次位置二进制模式
j = i + 1; //上一次位置字符模式
}
i = i + 1;
}
return namesList;
}
/**
* 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
* 上传文件时保证目录的存在目录格式必须以"/"根目录开头
* @param pathList String
* @throws Exception
*/
public void buildRemoteDir(String pathList) throws Exception {
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); //sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}
/**
* 上传文件到FTP服务器,remote路径以FTP服务器的"/"开始,带文件名、
* 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
* @param local String
* @param remote String
* @throws Exception
*/
public void upFile(String local, String remote) throws Exception {
buildRemoteDir(remote.substring(0, remote.lastIndexOf("/")));
ftpclient.binary(); //此行代码必须放在buildRemoteDir之后
TelnetOutputStream ftpOut = ftpclient.put(remote);
InputStream in = new FileInputStream(local);//
byte[] buf = new byte[204800];
int bufsize = 0;
while((bufsize = in.read(buf, 0, buf.length)) != -1){
ftpOut.write(buf, 0, bufsize);
}
in.close();
ftpOut.close();
}
public void buildLocalDir(String fullPath) throws Exception { if(fullPath.lastIndexOf("/")<=0)return;
String path=fullPath.substring(0,fullPath.lastIndexOf("/")); File f= new File(path);
if(!f.exists()){
f.mkdirs();
}
}
public void downFile(String remote,String local) throws Exception {
buildLocalDir(local);
ftpclient.binary(); //此行代码必须放在buildRemoteDir之后
OutputStream out=new FileOutputStream(new File(local));
TelnetInputStream ftpIn = ftpclient.get(remote);
byte[] buff=new byte[204800];
int len=0;
while((len=ftpIn.read(buff))!=-1){
out.write(buff,0,len);
}
out.close();
ftpIn.close();
}
public static void main(String args[])throws Exception{
FtpUtils upfile=new FtpUtils("192.168.187.130","root","1-1=0");
upfile.login();
List list=upfile.fileNames("/");
System.out.println(list);
upfile.upFile("FtpUtils.java","/root/xjs/test/FtpUtils.java" );
upfile.downFile("/root/xjs/2.txt","xjs/2.txt" );
upfile.logout();
}
}