程序是在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;
}
} 我实在找不到错误的原因,请大家帮忙,谢谢了!!!
......
<% 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;
}
} 我实在找不到错误的原因,请大家帮忙,谢谢了!!!
然后最好是 getContext().getpath()之类的,再加上你的相对路径看下
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
-->
我看你用的就是相对路径:如果File file = new File("a.txt");这样就应该没问题,你看下这个jsp和你要读的文件是不是在一个路径里面,不是的话肯定找不到.
要不你就换成Url访问,比如你原先要访问的文件在202.68.74.196上面,那就
URL url = new URL("http://202.68.74.196(:端口号)/?.?");
如果是外网访问就要加端口号了,内网就不需要了.
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"));
这样吧,
File file = new File(fileName);
setErrorMessage(file.getPath());//来得到file的实际地址,然后你看看有没有什么解决方案if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "' does not exists.");
return null;
}
The content of the file 'C:\bank\bank\images\bg.jpg ' : Error: The file 'C:\bank\bank\images\bg.jpg' does not exists.