在Java中涉及到訪問文件路徑或是引用某個(gè)文件時(shí),經(jīng)常要用到獲取當(dāng)前項(xiàng)目上下文路徑問題,有時(shí)上傳圖片或文件后需要下載文件,又涉及到文件的下載路徑,于是將在Java或jsp里文件路徑獲取方法總結(jié)如下:
DATE:2010-07-15 hzg
?????String?basePath?=?request.getContextPath();?????basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+basePath+"/";?????String?servletPath?=??request.getServletPath();?????String?realPath=request.getRealPath("")+"";?????//根據(jù)上傳時(shí)間,生成不同的文件夾??String?currFoder?=??m1.createFolder(realPath,uploadDate);?????//文件保存目錄路徑?String?savePath?=?realPath+currFoder;?????//文件下載路徑??String?fileName?=???dto.getFileName("fileName");??String?downLoadPath?=?basePath+currFoder+"/"+fileName;????//文件輸出路徑:savePath========>>D:Tomcat5.5webappstestProjectupload/2010-07-15/2010-07-15第1次.docdownLoadPath===>>http://localhost:8081/testProject/upload/2010-07-15/2010-07-15第1次.doc??????public?String?createFolder(String?realPath,String?uploadDate){?????String?currFoder?=?"upload/"+uploadDate;?????String?fileFoder?=?realPath+currFoder;????????FileOperate?op?=?new?FileOperate();????op.newFolder(fileFoder);????//調(diào)用newFolder()方法創(chuàng)建文件夾?????return?currFoder;????}
以上就是北大青鳥長(zhǎng)沙麓谷校區(qū)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“初學(xué)教程:Java文件上傳下載路徑”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。