2012-03-09 131 views
0

我有一个Web应用程序,我试图返回一个布尔值的.zip文件,该文件在服务器上获取(成功)生成。Java和JSF,检查服务器上是否存在文件

本地我在Eclipse上使用Tomcat在Windows上运行此操作,但是我将它部署到Linux机器上的Tomcat服务器上。

 //.zip is generated successfully on the SERVER by this point 
     File file1 = new File("/Project/zip/theZipFile.zip"); 

     boolean exists = file1.exists();// used to print if the file exists, returns false 
     if (exists) 
      fileLocation = "YES"; 
     else 
      fileLocation = "No"; 

当我这样做的时候,当我打印出来的时候,它在调试器和我的页面上一直返回false。我确信它与文件路径有关,但我不确定。

一旦我得到确认的zip的存在,我可以很容易地使用File.length()来得到我需要的。

假设所有获取者和设置者都存在,并打印JSF。它主要是Java后端,我遇到了一些小问题。

感谢您的帮助! :)使用

回答

0

调试这一说法

System.out.println("Actual file location : " + file1.getAbsolutePath()); 

这会告诉你该文件的绝对路径,它是寻找

1

你的路并不好。尝试删除前导/解决问题 请参阅测试代码以获得解释:

import java.io.*; 
public class TestFile { 

public static void main(String[] args) { 

try { 
//Creates a myfile.txt file in the current directory 
File f1 = new File("myfile.txt"); 
f1.createNewFile(); 
System.out.println(f1.exists()); 

//Creates a myfile.txt file in a sub-directory 
File f2 = new File("subdir/myfile.txt"); 
f2.createNewFile(); 
System.out.println(f2.exists()); 

//Throws an IOException because of the leading/
File f3 = new File("/subdir/myfile.txt"); 
f3.createNewFile(); 
System.out.println(f3.exists()); 

} catch(IOException e) { 
System.out.println(e.getMessage()); 
} 
} 
} 
相关问题