❶ 请问一下,java中有没直接判断ftp上文件夹下是否存在某文件的方法通过遍历文件夹的方式判断太耗内存了
第一个种方法 :
org.apache.commons.net.ftp.* 看这个目录下是否有你要的方法
第二种方法:
package com.soft4j.log4j;
import java.io.IOException;
import sun.net.ftp.FtpClient;
public class FtpTest
{
static String middle_ftpServer = "10.103.2.250";
static String middle_user = "ora9iftp";
static String middle_password = "ftp";
static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";
public static void main(String[] args)
{
FtpClient ftpClient = new FtpClient();
try
{
ftpClient.openServer(middle_ftpServer);
ftpClient.login(middle_user, middle_password);
FtpTest ft = new FtpTest();
ft.isDirExist(ftpClient, middle_dir);
} catch (IOException e)
{
e.printStackTrace();
}
}
/** 判断Ftp目录是否存在,如果不存在则创建目录 */
public void isDirExist(FtpClient ftpClient, String dir)
{
try
{
ftpClient.cd(dir); //想不到什么好办法来判断目录是否存在,只能用异常了(比较笨).请知道的告诉我一声`
} catch (IOException e1)
{
ftpClient.sendServer("MKD " + dir + "\r\n");
try
{
ftpClient.readServerResponse();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
❷ 求用java写一个ftp服务器客户端程序。
import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP服务器启动....");
System.out.println("正在等待连接....");
//监听21号端口
server = new ServerSocket(21);
socket = server.accept();
System.out.println("连接成功");
System.out.println("**********************************");
System.out.println("");
InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("请等待验证客户信息....");
while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "请输入密码:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "连接成功。";
dout.writeUTF(s);
break;
}
else{s ="密码错误,请重新输入用户名:";<br> dout.writeUTF(s);<br> <br> }
}
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
System.out.println("验证客户信息完毕...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;i<dirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;
}
else if(i<dirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP关闭!"+e);
}
}}
❸ java ftp 怎么建立多层文件夹
public static void buildList(FtpClient aftp,String pathList) throws Exception {
aftp.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); //sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
aftp.sendServer("XMKD " + pathName + "\r\n"); }
catch (Exception e) { e.printStackTrace(); }
int reply = aftp.readServerResponse(); }
aftp.binary();
}
❹ 用java怎么获取ftp上的文件
在Java中,可以通过FtpClientUtil类连接到FTP服务器并执行文件操作。首先,需要定义类FtpClientUtil,包含服务器地址、端口、用户名和密码等属性。
要连接到服务器,可以使用open()方法。该方法尝试打开FTP服务器连接并登录。如果连接成功,则返回true,否则返回false。
为了上传文件,可以使用upload()方法。首先,确保已经成功连接到服务器。然后,使用FileInputStream读取本地文件内容,并通过TelnetOutputStream将文件传输到FTP服务器。
下载文件的方法是download()。它同样需要先连接到FTP服务器,然后通过TelnetInputStream读取文件内容,并使用FileOutputStream将文件保存到本地。
要查看FTP目录下的文件列表,可以使用getFileNameList()方法。该方法首先连接到服务器,然后使用DataInputStream读取文件列表并返回。
如果需要删除文件或目录,可以分别调用deleteFile()和deleteDirectory()方法。这两个方法都会发送相应的命令到FTP服务器并返回操作结果。
最后,可以通过调用close()方法关闭与FTP服务器的连接。
❺ 我想登录一个ftp然后把某个目录的所有文件考到另一个ftp的目录的某个文件夹下用java代码实现
用的commons-net包中的FTPClient
ftp1为拷贝目录,ftp2为被拷贝目录
你先登录ftp2调用ftp1,
ftpClient1.changeWorkingDirectory(path);
InputStream inputStream = ftpClient1.retrieveFileStream(file.getName());
用这个代码应该可以从ftp1中获得一个inputStream ,在ftp2中可以做上传操作
目录的话ftp2还要做递归存放到list中,ftp2遍历上传. 其实我也没做这个,希望思路有点帮助,应该可以实现.good luck!~~~
❻ 怎么用java代码创建ftp用户和密码
创建ftp用户名和密码,其实就在ftp服务器的用户文件里面添加条记录。
方法有两种,我说下思路。
一、你可以用java程序找到相应的配置文件,打开、把用户名密码写入进去。ok了。
二、你用用java程序调用创建ftp用户的命令,来创建ftp用户。