我碰到了一个很奇怪的问题,不知道能不能说清楚,先谢谢了!
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函数里面输出的文件都是相同的!!呜呜.........
}
}
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函数里面输出的文件都是相同的!!呜呜.........
}
}
解决方案 »
- Integer.class 和 Integer.TYPE,越想越糊涂,求高人指点
- 关于Scanner获得输入字符的问题
- 简单的JAVA小问题 请帮忙
- 关于java中import的小问题
- applet中的监听器
- shell.getDisplay().asyncExec(new Runnable(){}) 具体是怎么用的啊?为什么我的代码执行老是程序无响应
- 请教关于往.jar中写入xml文件的问题
- Properties中,如何根据key读取value
- 字符串按固定格式输出问题请教
- 装JBUILDER5 个人版后,改装企业版,为什么装完后还是在个人版状态!很多控件用不了!
- 为什么不能运行??????
- 字符串replace方法遇到一个奇怪的问题!
//此处移动文件失败!什么原因???????????????请大家指点!感激不尽
这里是因为ftpbak这个文件夹不存在,所以会失败,我试过了,如果存在的话就可以了。给你个建议,在调用前可以先判断目标文件夹是否存在,不存在先mkdir()建好!
File temFile = new File(destPath.getDirectory() + srcFile.getFileName());
if (temFile.exists()) temFile.delete();
sfile.moveTo(destPath);
}
沒試過,自己再試試
FTPToServer就是FileMove,拷贝过来的时候忘改了,ftpbak这个文件夹存在,我确定。
从main函数调用能够移动,但是从别的地方调用就不行了
没有原来的文件了,所以success返回为false。
特别感谢 tanxd39(谈笑风声),livan1038(李枫) ,olific() ,grant999(民)
解决了,问题出在:我在调用move的地方有其他的FileInputStream没有关闭。
谢谢大家!