当前位置: 技术问答>java相关
(关于URL)为什么我这样写是错误的?
来源: 互联网 发布时间:2015-02-12
本文导语: 在JSP中 ===================================================== String myUrl=request.getRequestURI(); URL url=new URL(/tech-qa-java/myUrl/index.html); String myPath=url.getPath(); =============================================== 我这样写是错误的? 出错信息: ======================================...
在JSP中
=====================================================
String myUrl=request.getRequestURI();
URL url=new URL(/tech-qa-java/myUrl/index.html);
String myPath=url.getPath();
===============================================
我这样写是错误的?
出错信息:
========================================================
java.net.MalformedURLException: no protocol: /mail/modules/mailproject/maillist.jsp
at java.net.URL.(URL.java:473)
at java.net.URL.(URL.java:376)
at java.net.URL.(URL.java:330)
at modules.mailproject._0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0._jspService(_0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0.java:105)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:282)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)
==================================================================
=====================================================
String myUrl=request.getRequestURI();
URL url=new URL(/tech-qa-java/myUrl/index.html);
String myPath=url.getPath();
===============================================
我这样写是错误的?
出错信息:
========================================================
java.net.MalformedURLException: no protocol: /mail/modules/mailproject/maillist.jsp
at java.net.URL.(URL.java:473)
at java.net.URL.(URL.java:376)
at java.net.URL.(URL.java:330)
at modules.mailproject._0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0._jspService(_0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0.java:105)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:282)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)
==================================================================
|
加一个处理异常过程:
try{
String myUrl=request.getRequestURI();
URL url=new URL(/tech-qa-java/myUrl/index.html);
String myPath=url.getPath();
}
catch(MalformedURLException e){}
try{
String myUrl=request.getRequestURI();
URL url=new URL(/tech-qa-java/myUrl/index.html);
String myPath=url.getPath();
}
catch(MalformedURLException e){}
|
myUrl的型式应该是http://www.csdn.net/expert
|
myUrl 应该写全 如 http://java.sun.com/index.html
|
同意楼上,给你一个方法,用它来转化
如果用url调硬盘上的一个文件也需要用file://...
public static final URL createURL(/tech-qa-java/String fileName/index.html) throws Exception
{
URL url = null;
try
{
url = new URL(/tech-qa-java/fileName/index.html);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
String path = f.getAbsolutePath();
String fs = System.getProperty("file.separator");
if (fs.length() == 1)
{
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(/tech-qa-java/path/index.html);
}
catch (MalformedURLException e)
{
System.out.println("Cannot create url for: " + fileName);
throw new Exception(e.getMessage());
}
}
return url;
}
如果用url调硬盘上的一个文件也需要用file://...
public static final URL createURL(/tech-qa-java/String fileName/index.html) throws Exception
{
URL url = null;
try
{
url = new URL(/tech-qa-java/fileName/index.html);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
String path = f.getAbsolutePath();
String fs = System.getProperty("file.separator");
if (fs.length() == 1)
{
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(/tech-qa-java/path/index.html);
}
catch (MalformedURLException e)
{
System.out.println("Cannot create url for: " + fileName);
throw new Exception(e.getMessage());
}
}
return url;
}
|
myUrl="http://" + myUrl