java文件中获得路径
Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 获得当前类所在路径
System.getProperty("user.dir") // 获得项目根目录的绝对路径
System.getProperty("java.class.path") //得到类路径和包路径
打印输出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar
2、 JSP中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getRequestURI()
文件的绝对路径 :application.getRealPath(request.getRequestURI());
当前web应用的绝对路径 :application.getRealPath("/");
取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()
3.1 JSP中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getRequestURI()
文件的绝对路径 :application.getRealPath(request.getRequestURI());
当前web应用的绝对路径 :application.getRealPath("/");
取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()
3、Servlet中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())
② jsp怎么实现文件夹选择并获取文件夹路径
<input type="file"> 用value来获取文件夹的 路径。
③ jsp内获取java web中文件路径
只能得到根目录下的路径,绝对路径可以通过classloader获取再拼接相对路径
④ jsp上传下载文件的路径问题
jsp上传下载文件的路径是在服务器建立指定路径如下:
//接收上传文件内容中临时文件的文件名
String tempFileName = new String("tempFileName");
//tempfile 对象指向临时文件
File tempFile = new File("D:/"+tempFileName);
//outputfile 文件输出流指向这个临时文件
FileOutputStream outputStream = new FileOutputStream(tempFile);
//得到客服端提交的所有数据
InputStream fileSourcel = request.getInputStream();
//将得到的客服端数据写入临时文件
byte b[] = new byte[1000];
int n ;
while ((n=fileSourcel.read(b))!=-1){
outputStream.write(b,0,n);
}
//关闭输出流和输入流
outputStream.close();
fileSourcel.close();
//randomFile对象指向临时文件
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
//读取临时文件的第一行数据
randomFile.readLine();
//读取临时文件的第二行数据,这行数据中包含了文件的路径和文件名
String filePath = randomFile.readLine();
//得到文件名
int position = filePath.lastIndexOf('\\');
CodeToString codeToString = new CodeToString();
String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
//重新定位读取文件指针到文件头
randomFile.seek(0);
//得到第四行回车符的位置,这是上传文件数据的开始位置
long forthEnterPosition = 0;
int forth = 1;
while((n=randomFile.readByte())!=-1&&(forth<=4)){
if(n=='\n'){
forthEnterPosition = randomFile.getFilePointer();
forth++;
}
}
//生成上传文件的目录
File fileupLoad = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file","upLoad");
fileupLoad.mkdir();
//saveFile 对象指向要保存的文件
File saveFile = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename);
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
//找到上传文件数据的结束位置,即倒数第四行
randomFile.seek(randomFile.length());
long endPosition = randomFile.getFilePointer();
int j = 1;
while((endPosition>=0)&&(j<=4)){
endPosition--;
randomFile.seek(endPosition);
if(randomFile.readByte()=='\n'){
j++;
}
}
//从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中
randomFile.seek(forthEnterPosition);
long startPoint = randomFile.getFilePointer();
while(startPoint<endPosition){
randomAccessFile.write(randomFile.readByte());
startPoint = randomFile.getFilePointer();
}
//关闭文件输入、输出
randomAccessFile.close();
randomFile.close();
tempFile.delete();
jsp文件下载选择路径:
//要下载的文件
File fileload = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename);
⑤ 在jsp该如何获取file的路径
你在servlet里用String接受就可以了。
String
filepath
=
request.getParameter("file");
这样应该可以的。
⑥ jsp中通过<input type="file">选取文件得到文件路径,怎么样才可以获取到这个路径,用到其他地方
通常只需要从POST过来的数据里取得文件数据即可,最多带个文件名,通常浏览器在上传文件的时候是不会发送路径的,IE可以设置是否在上传文件时包含路径,具体看下图:
⑦ 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页面所在的项目的路径
在jsp和class文件中调用的相对路径不同。 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。
⑨ 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 />