本地打开文件显示路径下文件不存在

hubinjisu 2008-06-12 10:35:45
程序是在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;
}
}

我实在找不到错误的原因,请大家帮忙,谢谢了!!!
...全文
969 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
laorer 2008-06-13
  • 打赏
  • 举报
回复
etErrorMessage(file.getAbsolutePath());//来得到file的实际地址,然后你看看有没有什么解决方案
水瘦山寒 2008-06-13
  • 打赏
  • 举报
回复
先把你要找的文件路径贴出来看看
再手动在这个地址下去找找这个文件有没有
ilrxx 2008-06-13
  • 打赏
  • 举报
回复
程序就不仔细看了,我认为是你原先在本机测试时候,欲打开的文件用的是绝对路径,然后你传到服务器上,肯定就是不存在了,建议用相对路径试试
我看你用的就是相对路径:如果File file = new File("a.txt");这样就应该没问题,你看下这个jsp和你要读的文件是不是在一个路径里面,不是的话肯定找不到.
要不你就换成Url访问,比如你原先要访问的文件在202.68.74.196上面,那就
URL url = new URL("http://202.68.74.196(:端口号)/?.?");
如果是外网访问就要加端口号了,内网就不需要了.
laorer 2008-06-13
  • 打赏
  • 举报
回复

这样吧,
File file = new File(fileName);
setErrorMessage(file.getPath());//来得到file的实际地址,然后你看看有没有什么解决方案

if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "' does not exists.");
return null;
}
lixq2000 2008-06-13
  • 打赏
  • 举报
回复
吸取经验
baobao28 2008-06-13
  • 打赏
  • 举报
回复
你试试用我这种方法取路径
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"));
老紫竹 2008-06-13
  • 打赏
  • 举报
回复
你去确认那个目录,那个文件真的有吗?
tavor 2008-06-13
  • 打赏
  • 举报
回复
jsp代码执行都是在服务器上执行,它找的路径也是服务器上的,所以是不能操作用户端的文件的
hubinjisu 2008-06-13
  • 打赏
  • 举报
回复
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.
yami251139 2008-06-12
  • 打赏
  • 举报
回复
那个你参考下,我不知道你是怎么放的
yami251139 2008-06-12
  • 打赏
  • 举报
回复
<!-- String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
-->
hubinjisu 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 laorer 的回复:]
fileName换成绝对地址试试,看看存不存在?
然后最好是 getContext().getpath()之类的,再加上你的相对路径看下
[/Quote]
fileName是存在的,因为每次错误信息里都能把文件名显示出来。另外不知道你说的加上相对路径方法程序上应该怎么改?谢谢
laorer 2008-06-12
  • 打赏
  • 举报
回复
fileName换成绝对地址试试,看看存不存在?
然后最好是 getContext().getpath()之类的,再加上你的相对路径看下
hubinjisu 2008-06-12
  • 打赏
  • 举报
回复
补充一下,打开文件时提示的错误就是函数getContent里的错误信息:"Error: The file '" + fileName + "' does not exists."

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧