① java怎样获取当前目录路径
很多朋友都想知道java如何获取当前目录路径?下面就一起来了解一下吧~
1、利用System.getProperty()函数获取当前路径:
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
2、使用File提供的函数获取当前路径:
File directory = new File("");//设定为当前文件夹 try{ System.out.println(directory.getCanonicalPath());//获取标准的路径 System.out.println(directory.getAbsolutePath());//获取绝对路径 }catch(Exceptin e){} File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。 # 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹 # 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径 # 至于getPath()函数,得到的只是你在new File()时设定的路径 比如当前的路径为 C:/test : File directory = new File("abc"); directory.getCanonicalPath(); //得到的是C:/test/abc directory.getAbsolutePath(); //得到的是C:/test/abc direcotry.getPath(); //得到的是abc File directory = new File("."); directory.getCanonicalPath(); //得到的是C:/test directory.getAbsolutePath(); //得到的是C:/test/. direcotry.getPath(); //得到的是. File directory = new File(".."); directory.getCanonicalPath(); //得到的是C:/ directory.getAbsolutePath(); //得到的是C:/test/.. direcotry.getPath(); //得到的是.. 另外:System.getProperty()中的字符串参数如下: System.getProperty()参数大全 # java.version Java Runtime Environment version # java.vendor Java Runtime Environment vendor # java.vendor.url Java vendor URL # java.home Java installation directory # java.vm.specification.version Java Virtual Machine specification version # java.vm.specification.vendor Java Virtual Machine specification vendor # java.vm.specification.name Java Virtual Machine specification name # java.vm.version Java Virtual Machine implementation version # java.vm.vendor Java Virtual Machine implementation vendor # java.vm.name Java Virtual Machine implementation name # java.specification.version Java Runtime Environment specification version # java.specification.vendor Java Runtime Environment specification vendor # java.specification.name Java Runtime Environment specification name # java.class.version Java class format version number # java.class.path Java class path # java.library.path List of paths to search when loading libraries # java.io.tmpdir Default temp file path # java.compiler Name of JIT compiler to use # java.ext.dirs Path of extension directory or directories # os.name Operating system name # os.arch Operating system architecture # os.version Operating system version # file.separator File separator ("/" on UNIX) # path.separator Path separator (":" on UNIX) # line.separator Line separator ("/n" on UNIX) # user.name User’s account name # user.home User’s home directory # user.dir User’s current working directory
JAVA中获取路径 关键字: java中获取路径
1、jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 结果:/TEST (3)得到当前页面所在目录下全名称:request.getServletPath() 结果:如果页面在jsp目录下 /TEST/jsp/test.jsp (4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") 结果:D:/resin/webapps/TEST/test.jsp (5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 结果:D:/resin/webapps/TEST
2、在类中取得路径: (1)类的绝对路径:Class.class.getClass().getResource("/").getPath() 结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ (2)得到工程的路径:System.getProperty("user.dir") 结果:D:/TEST
② JSP获得根目录路径
使用以下方式,来如果获自取不到,那么说明你的服务器原本就不在E:盘,那么建议你使用绝对路径来解决这个问题,直接定义路径,而不要获取路径。 使用<%=request.getRealPath(".")%>即可得到根目录,必须调用以下几个包:<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="java.io.*" %>
③ JSP如何取得<input type="file"> 文件路径
问题补充:我使用strFile = request.getParameter("file")只能取得文件名,会发生I/O异常,提示找不到文件路径; <br />用new File(strFile).getAbsolutePath()得到了路径,但是得到的路径形式为:%Tomcat_dir%\bin\XX,XX为文件名 问题补充:<div class="quote_title">lyyf 写道</div><div class="quote_div">1.用request.getParameter(String str) <br /><input type="file" name="fileName" <br />后台用String fileName=request.getParameter("fileName")获取,试试 <br />2.直接获取服务器上的内容 <br /> FileItemFactory factory = new DiskFileItemFactory(); <br /> ServletFileUpload upload = new ServletFileUpload(factory); <br /> List<FileItem list = upload.parseRequest(request); <br />不知道对不对,希望对你有帮助 <br /> <br /> <br /> <br /></div> <br />String fileName=request.getParameter("fileName")这个我试过了只能得到文件名,浏览器我用IE9、firefox 4试过都不行;我仅仅是想获得<input type="file" name="fileName"的路径 问题补充:<div class="quote_title">maxm 写道</div><div class="quote_div">1.是以post方式提交的的吗 <br />2.有无 :enctype= "multipart/form-data " <br />3.可以用隐藏iframe的方式</div> <br /> <br /> <br />是post提交,如果有enctype= "multipart/form-data "的话,程序就出错,运行起来没反应,我在IE6试了下,可以取得路径(不过此时我的程序能正常运行,但是有乱码出现) 问题补充:<div class="quote_title">maxm 写道</div><div class="quote_div">贴出异常信息</div> <br /> <br />Path is:F:\apache-tomcat-6.0.29\bin\log.txt(这是我使用new File(strFile).getAbsolutePath();取得的路径) <br />javax.mail.MessagingException: IOException while sending message; <br /> nested exception is: <br /> java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />Caused by: java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> ... 20 more <br />java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />
④ jsp怎么获取当前页面的url
当一个url过来时,如:http://localhost:8080/demo/hello.jsp,在hello.jsp页面,我们可以这样得到url:
代码如下:
<%Stringbasepath
=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
StringPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"+request.getContextPath()+"/";
Stringuri=request.getRequestURI();
uri=uri.substring(uri.lastIndexOf("/")+1);//获得是最后的hello.jsp
%>
(上例中,Path路径就是图中的)
说明:
1.request.getContextPath() 返回站点的根目录,如:‘’/demo‘’
2.request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径
3.request.getScheme() 等到的是协议名称,默认是http
4.request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost ..com 等等
5.request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等
OK,满意的话请好评!O(∩_∩)O~
⑤ jsp中 input file选择文件 怎么得到文件的路径啊 浏览器是ie8
<script type="text/javascript">
//FX获取文件路径方法
function readFileFirefox(fileBrowser) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e) {
alert('无法访问本地文件,由于浏览器安全设置。为了克服这一点,请按照下列步骤操作:(1)在地址栏输入"about:config";(2) 右键点击并选择 New->Boolean; (3) 输入"signed.applets.codebase_principal_support" (不含引号)作为一个新的首选项的名称;(4) 点击OK并试着重新加载文件');
return;
}
var fileName=fileBrowser.value; //这一步就能得到客户端完整路径。下面的是否判断的太复杂,还有下面得到ie的也很复杂。
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
try {
// Back slashes for windows
file.initWithPath( fileName.replace(/\//g, "\\\\") );
}
catch(e) {
if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.");
return;
}
if ( file.exists() == false ) {
alert("File '" + fileName + "' not found.");
return;
}
return file.path;
}
//根据不同浏览器获取路径
function getvl(){
//判断浏览器
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
var file_url="";
if(Sys.ie<="6.0"){
//ie5.5,ie6.0
file_url = document.getElementById("file").value;
}else if(Sys.ie>="7.0"){
//ie7,ie8
var file = document.getElementById("file");
file.select();
file_url = document.selection.createRange().text;
}else if(Sys.firefox){
//fx
//file_url = document.getElementById("file").files[0].getAsDataURL();//获取的路径为FF识别的加密字符串
file_url = readFileFirefox(document.getElementById("file"));
}
//alert(file_url);
document.getElementById("text").innerHTML="获取文件域完整路径为:"+file_url;
}
</script>
<h1>JS获取文件域完整路径的方法,兼容不同浏览器</h1>
<div id="text" style="color:#f00;"></div>
<input type="file" id="file" />
<input name="" type="button" value="获取" onClick="getvl();">
⑥ jsp怎么实现文件夹选择并获取文件夹路径
<input type="file"> 用value来获取文件夹的 路径。
⑦ jsp页面内选择文件夹并获得路径
document.getElementById(path).value =Folder;
改为
document.getElementById("path").value =Folder;
你的path应该加双引号!
50分赚到手了,哈哈回~~答~
⑧ 如何获取项目绝对路径
用获取、用Java类获取或用servlet获取项目绝对路径。