weixin_42259076 2008-09-03 14:55
浏览 633
已采纳

求教: java移动文件(移动ftp上的文件)

通过JAVA操作FTP上的文件(我用的是sun.net.ftp.FtpClient)
如:FTP上有目录A和目录B,现要将A目录中的文件移动到B目录,请问该怎么做?
第一次操作ftp上的文件,实在是太多不懂,还望各位指点一二,谢谢!!!

前面搜索了下,好像这位老兄liugj99跟我一样的问题,可是没有满意答案

[b]问题补充:[/b]
感谢lijie250,也非常感谢mickeymak,不过,这个只能处理本地文件吧。
能否再扩展一些,操作ftp上的文件?

  • 写回答

2条回答 默认 最新

  • mickeymak2005 2008-09-03 23:14
    关注

    希望对你有帮助

    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛