JSP中怎样判断一个已知url的文件是否存在?急!

li_yadan 2002-03-20 10:44:52
具体情况是:
if(url 存在)
{
直接给出url链接;
}
else
{
读取数据库中存储的文件内容;
在web服务端生成相应url的文件;
给出url链接;
}

我想知道 怎么判断url 的文件已经存在?
通过读取http头信息能做到吗?
...全文
450 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlq 2002-03-20
  • 打赏
  • 举报
回复
上面"<TITLE>404 Not Found</TITLE>"根据SERVER的出错信息而定.
zlq 2002-03-20
  • 打赏
  • 举报
回复
URL url;
InputStream iss;
int i;
long lSize;
byte[] byteArr;
String ss;
try
{
url = new URL("http://htgzln059.htgz.com/qms/test/getTime.jsp");
iss = url.openStream();
byteArr = new byte[5000];

iss.read(byteArr,0,5000);
ss = new String(byteArr);
if(ss.indexOf("<TITLE>404 Not Found</TITLE>")>=0)
System.out.println ("not found");

}catch(Exception e)
{
System.out.println (e.toString());
}
li_yadan 2002-03-20
  • 打赏
  • 举报
回复
怎么在 那个if语句中写啊?

或者怎么让程序去得到返回的信息?
cosmo 2002-03-20
  • 打赏
  • 举报
回复
connect to the server,
HEAD the document, if no error message, the document exists
skyyoung 2002-03-20
  • 打赏
  • 举报
回复
Check if a page exists
[JDK1.1] import java.net.*;
import java.io.*;
import java.net.*;

public class Check {

public static void main(String s[]) {
System.out.println(exists("http://www.csdn.net/index.html"));
System.out.println(exists("http://www.csdn.com/pagenotfound.html"));
}

static boolean exists(String URLName){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con =
(HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

zlq 2002-03-20
  • 打赏
  • 举报
回复
不会.因为你这个是HTTP协议,刚才我说那个是FILE协议.

url=new URL("http://localhost:8080/***/xyz.doc")
处理方法和上面例子同理.
li_yadan 2002-03-20
  • 打赏
  • 举报
回复
那url=new URL("http://localhost:8080/***/xyz.doc")
也可以抛出异常吗?
zlq 2002-03-20
  • 打赏
  • 举报
回复
url = new URL("file:///c://autoexec.bat");
找不到会:
java.io.FileNotFoundException
li_yadan 2002-03-20
  • 打赏
  • 举报
回复
谢谢!
我把URL转换成物理路径,在程序中判断那个路径的文件存不存在是不是也可以?
呵呵

81,122

社区成员

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

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