本文实例讲述了jsp中获取当前目录的实现方法,分享给大家供大家参考。具体实现方法如下:
* z) ^5 B! v& ~6 M: n1 Q1、利用system.getproperty()函数获取当前路径:% t7 W" @1 j, q8 d& z
代码如下:3 L* z2 V: R' R
system.out.println(system.getproperty(user.dir));//user.dir指定了当前的路径: a2 D- q H, ^. F' K6 G
2、使用file提供的函数获取当前路径:5 a( y+ k4 e6 @
代码如下:
, {. y# A) o! g& q7 i2 |file directory = new file();//设定为当前文件夹
: Y7 J. E. U$ K& @- k3 Y% j% U3 I% qtry{
" U: P' k) N- F! I; }system.out.println(directory.getcanonicalpath());//获取标准的路径7 m! s# i" }3 Z+ q2 k2 J
system.out.println(directory.getabsolutepath());//获取绝对路径
- L7 b8 u8 U, T' s6 T}catch(exceptin e){}
/ u) s, G' X1 w3 P |8 s0 s+ p afile.getcanonicalpath()和file.getabsolutepath()大约只是对于new file(.)和new file(..)两种路径有所区别。% ?& R+ s7 D0 X' I+ A/ J' X$ n
# 对于getcanonicalpath()函数,“.就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹) ^7 T- r+ A' `; y. F
# 对于getabsolutepath()函数,则不管”.”、“..”,返回当前的路径加上你在new file()时设定的路径; d' f5 p" B* [$ o, v/ R0 i: G
# 至于getpath()函数,得到的只是你在new file()时设定的路径& Q& q! a( X- |/ Q
比如当前的路径为 c:test :; b& S O6 U3 l Q2 h i9 r/ p! x
代码如下:# ?9 C& I# t0 t( b
file directory = new file(abc);- q- ~& a/ b3 o! k+ P9 [
directory.getcanonicalpath(); //得到的是c:testabc9 E& _0 y8 I! p! q: X+ w3 L
directory.getabsolutepath(); //得到的是c:testabc
A' }& Z- _9 Qdirecotry.getpath(); //得到的是abc
8 k. G5 |' J {: C( M- vfile directory = new file(.);3 F0 x# h) o+ y
directory.getcanonicalpath(); //得到的是c:test* H6 T, K" B% G2 c& Z6 m5 G, ?
directory.getabsolutepath(); //得到的是c:test.+ Z0 h+ a0 G( k @# ?
direcotry.getpath(); //得到的是.
, w% @; c4 N/ x: i$ B, d$ J# v; }file directory = new file(..);& D, q8 O3 K( B: Q ]
directory.getcanonicalpath(); //得到的是c:8 k9 ~+ W4 D8 P1 N$ s
directory.getabsolutepath(); //得到的是c:test..0 h! x7 g# ]6 a0 p
direcotry.getpath(); //得到的是..! p$ E+ n- z: ^9 Y- H5 p+ n2 z4 |6 [
获取 java 程序当前的工作目录7 [+ ~+ k( u8 f% ~; H
代码如下:% \' y8 _, M( ], a$ ~- I# a# b2 F, F
file file = new file(t.tmp);
4 M$ D* M( R: H+ u2 {string fullpath = file.getabsolutepath();
0 q1 v8 b X9 M. k① request.getrealpath:- l& d) F! a6 G5 j
方法:request.getrealpath(/)0 \ P% Y% y' O8 o% U; R. p1 w1 w
得到的路径:c:program filesapache software foundationtomcat 5.5webappsstrutstest
2 I# n" l7 b. I( n' q方法:request.getrealpath(.): _' s3 P& y' }0 G0 @# @4 S8 K
得到的路径:c:program filesapache software foundationtomcat 5.5webappsstrutstest.: D8 O% ]) M9 ?# v, `7 H$ y
方法:request.getrealpath(); V% z9 a# Y$ c' X
得到的路径:c:program filesapache software foundationtomcat 5.5webappsstrutstest2 i+ t2 d% v, C) \- H* ]
方法:request.getrealpath(web.xml)
) @! l+ {: b% _- m, O得到的路径:c:program filesapache software foundationtomcat 5.5webappsstrutstestweb.xml* C9 ~! b1 D2 S) C
② request.getparameter();/ K- A! z4 R1 a3 h) M9 n( f, P( y
actionform.getmyfile();, w3 b0 S. d: X; a( L2 B7 y4 v) t" s; h: O
方法:string filepath = request.getparameter(myfile);) E1 @8 ^+ z, ]* r9 @! }
得到的路径:d:vss安装目录users.txt
5 p. G; P! I4 r. ^* ~( k方法:string filepath = actionform.getmyfile();, |6 o. ] p% c) H5 c) a
得到的路径:d:vss安装目录users.txt1 U6 H( d' r8 p, k7 V l% l/ ]
+ D/ r5 d; a- _# m希望本文所述对大家的jsp程序设计有所帮助。
3 s2 I' f" q+ t9 q5 f8 k5 V3 F2 X& w6 V! ?
更多网页制作信息请查看: 网页制作 |
|