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 />