通过JAVA操作FTP上的文件(我用的是sun.net.ftp.FtpClient)
如:FTP上有目录A和目录B,现要将A目录中的文件移动到B目录,请问该怎么做?
第一次操作ftp上的文件,实在是太多不懂,还望各位指点一二,谢谢!!!
前面搜索了下,好像这位老兄liugj99跟我一样的问题,可是没有满意答案
[b]问题补充:[/b]
感谢lijie250,也非常感谢mickeymak,不过,这个只能处理本地文件吧。
能否再扩展一些,操作ftp上的文件?
希望对你有帮助
[code="java"]
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while((i = fis.read(buf))!=-1){
fos.write(buf, 0, i);
}
fis.close();
fos.close();
------------整个目录下--------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFile {
private static String sourcepath;
private static String destpath;
//处理参数
private void processArgs(String[] args) {
if (args.length != 2) {
System.out.println("请输入两个文件目录……");
System.exit(1);
}//end if
else {
sourcepath = args[0];
destpath = args[1];
}//end else
}//end processArgs
//拷贝文件
private void copyFile(String source, String dest) {
try {
File in = new File(source);
File out = new File(dest);
FileInputStream inFile = new FileInputStream(in);
FileOutputStream outFile = new FileOutputStream(out);
byte[] buffer = new byte[1024];
int i = 0;
while ((i = inFile.read(buffer)) != -1) {
outFile.write(buffer, 0, i);
}//end while
inFile.close();
outFile.close();
}//end try
catch (Exception e) {
}//end catch
}//end copyFile
//处理目录
public void copyDict(String source, String dest) {
String source1;
String dest1;
File[] file = (new File(source)).listFiles();
for (int i = 0; i < file.length; i++)
if (file[i].isFile()) {
source1 = source + "/" + file[i].getName();
dest1 = dest + "/" + file[i].getName();
copyFile(source1, dest1);
}//end if
for (int i = 0; i < file.length; i++)
if (file[i].isDirectory()) {
source1 = source + "/" + file[i].getName();
dest1 = dest + "/" + file[i].getName();
File dest2 = new File(dest1);
dest2.mkdir();
copyDict(source1, dest1);
}//end if
}//end copyDict
public static void main(String args[]) {
CopyFile copyFile = new CopyFile();
copyFile.processArgs(args);
copyFile.copyDict(sourcepath, destpath);
}
}//end CopyFile
[/code]