我碰到了一个很奇怪的问题,不知道能不能说清楚,先谢谢了!
public class FileMove
{
public static boolean upload(String localfilename,String localbak,String remotefilename) 
{
 try 
 {
  //----------------此处ftp上传代码,能正确上传
  File file_in = new File(localfilename);
  System.out.println(file_in.toString());
  //此处输出了E:\workzxm\ftpandfilter\ftpdir\test.txt
  File bak=new File(localbak);
  System.out.println(bak.toString());
  //此处输出了E:\workzxm\ftpandfilter\ftpbak
  FileMove.move(file_in,bak);
  //此处移动文件失败!什么原因???????????????请大家指点!感激不尽
  return true;
 } 
 catch (IOException ex) 
 {
  System.out.println("upload fail");
  System.out.println(ex);
  return false;
 }
}
//移除上传后的文件
public static boolean move(File srcFile, File destPath)
{
    System.out.println(srcFile.toString());
    System.out.println(destPath);
    //这两个地方的输出不管是从main调用还是从upload调用都是:E:\workzxm\ftpandfilter\ftpdir\test.txt和E:\workzxm\ftpandfilter\ftpbak
    boolean success=false;
    try{
     File f=new File(destPath, srcFile.getName());
     System.out.println(f);
        //此处输出不管是从main()调用还是从upload()调用都是E:\workzxm\ftpandfilter\ftpbak\test.txt
     success = srcFile.renameTo(f);
    }
    catch(Exception ex)
    {
     System.out.println(ex);
    }
    return success;
}
public static void main(String agrs[]) 
{
  String s1="E:/workzxm/ftpandfilter/ftpdir/jaxen.jar";
  File file_in=new File(s1);
  String s2="E:/workzxm/ftpandfilter/ftpbak";
  File bak=new File(s2);
  FTPToServer.move(file_in,bak);//此处移动文件成功!什么原因???????????????请大家指点!感激不尽
  //也就是说从main调用能成功移动文件,但是从upload调用不能成功移动文件,但是在move函数里面输出的文件都是相同的!!呜呜.........
}
}

解决方案 »

  1.   

    搂住,FTPToServer.move(file_in,bak);//此处移动文件成功!FTPToServer是哪里来的呀? FileMove.move(file_in,bak);
      //此处移动文件失败!什么原因???????????????请大家指点!感激不尽
    这里是因为ftpbak这个文件夹不存在,所以会失败,我试过了,如果存在的话就可以了。给你个建议,在调用前可以先判断目标文件夹是否存在,不存在先mkdir()建好!
      

  2.   

    private void move(File srcFile, File destPath) throws Exception {
        File temFile = new File(destPath.getDirectory() + srcFile.getFileName());
        if (temFile.exists()) temFile.delete();
        sfile.moveTo(destPath);
    }
    沒試過,自己再試試
      

  3.   

    to tanxd39(谈笑风声) :
    FTPToServer就是FileMove,拷贝过来的时候忘改了,ftpbak这个文件夹存在,我确定。
    从main函数调用能够移动,但是从别的地方调用就不行了
      

  4.   

    是不是你的原文件已经被移动了,你没看清楚啊
    没有原来的文件了,所以success返回为false。
      

  5.   

    谢谢大家!
    特别感谢 tanxd39(谈笑风声),livan1038(李枫) ,olific() ,grant999(民) 
    解决了,问题出在:我在调用move的地方有其他的FileInputStream没有关闭。
    谢谢大家!