程序是在linux服务器上的,我在本地打开一个本地文件进行操作,可是总是说当前路径下文件不存在,原来程序在本机上时候调试没有一点问题,发到服务器上后就出现了问题。我的jsp里几行关于打开文件代码如下:
......
<% if (file_reader.getFileName() != "") { %> <b>The content of the file '</b><% out.println(file_reader.getFileName()); %><b>' : </b>
<br><br> <% if (file_reader.getContent() != null) { %> <Form enctype="multipart/form-data"> 
<TEXTAREA rows="15" cols="50" id=textarea1 name=textarea1> <% out.println(file_reader.getContent()); %> </TEXTAREA> 
</Form> 
......
<form enctype="multipart/form-data" method=get> 
<b>FileName: </b><input type=file name=fileName> 
<input type=submit class="button" value="Show it!"> 
</form>这个jsp调用了.java里一个获取打开文件内容的函数:public String getContent() {
String content = "";
//System.out.println("Filename: " + fileName);
File file = new File(fileName);
if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "' does not exists.");
return null;

else if (file != null) {
try {
// Create an BufferedReader so we can read a line at the time.
BufferedReader reader = new BufferedReader(new FileReader(file));
String inLine = reader.readLine();
while (inLine != null) {
if (inLine.length() + 1 > columns)
columns = inLine.length() + 1;
content += (inLine + System.getProperty("line.separator"));
inLine = reader.readLine();
rowCount++;
}
return content;
}
catch (IOException e) {
setErrorMessage("Error reading the file: " + e.getMessage());
return null;
}
}
else {
setErrorMessage("Unknown error!");
return null;
}
} 我实在找不到错误的原因,请大家帮忙,谢谢了!!!

解决方案 »

  1.   

    补充一下,打开文件时提示的错误就是函数getContent里的错误信息:"Error: The file '" + fileName + "' does not exists."
      

  2.   

    fileName换成绝对地址试试,看看存不存在?
    然后最好是  getContext().getpath()之类的,再加上你的相对路径看下
      

  3.   

    fileName是存在的,因为每次错误信息里都能把文件名显示出来。另外不知道你说的加上相对路径方法程序上应该怎么改?谢谢
      

  4.   

    <!-- String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    -->
      

  5.   

    程序就不仔细看了,我认为是你原先在本机测试时候,欲打开的文件用的是绝对路径,然后你传到服务器上,肯定就是不存在了,建议用相对路径试试
    我看你用的就是相对路径:如果File file = new File("a.txt");这样就应该没问题,你看下这个jsp和你要读的文件是不是在一个路径里面,不是的话肯定找不到.
    要不你就换成Url访问,比如你原先要访问的文件在202.68.74.196上面,那就
    URL url = new URL("http://202.68.74.196(:端口号)/?.?");
    如果是外网访问就要加端口号了,内网就不需要了.
      

  6.   

    你试试用我这种方法取路径
    String fpath = this.getClass().getClassLoader()
    .getResource("/").getPath();
    fpath = fpath.substring(1, fpath.length());// 因为上面取回来的路径的第一个字符是"/",所以把第一个字符去掉
    fpath = fpath.replaceAll("%20", " ");// 如果得到的路径中有空格,则将JSP路径中的20%转换成"
    fpath = fpath.substring(0, fpath.indexOf("/WEB-INF"));
      

  7.   


    这样吧,
    File file = new File(fileName);
     setErrorMessage(file.getPath());//来得到file的实际地址,然后你看看有没有什么解决方案if (!file.exists()) {
    setErrorMessage("Error: The file '" + fileName + "' does not exists.");
    return null;
    }
      

  8.   

    etErrorMessage(file.getAbsolutePath());//来得到file的实际地址,然后你看看有没有什么解决方案 
      

  9.   

    fileName其实就是程序获取到的本地文件绝对路径,从出错信息上可以看出来,我用打开一个C盘文件做测试,出现的错误是:
    The content of the file 'C:\bank\bank\images\bg.jpg ' : Error: The file 'C:\bank\bank\images\bg.jpg' does not exists. 
      

  10.   

    jsp代码执行都是在服务器上执行,它找的路径也是服务器上的,所以是不能操作用户端的文件的