重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這就是相對路徑
創新互聯專業為企業提供玉泉網站建設、玉泉做網站、玉泉網站設計、玉泉網站制作等企業網站建設、網頁設計與制作、玉泉企業網站模板建站服務,十年玉泉做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
指的是相對于工程文件的位置而言
在eclipse的結構圖中的位置
在windows的文件夾里的位置
在查看屬性里的絕對路徑的位置
代碼來找文件路徑
public?class?Test?{
public?static?void?main(String[]?args)?throws?Exception?{
System.out.println("當前目錄的路徑\t"+new?File(".").getCanonicalPath());//?"."表示當前目錄
File?file?=?new?File("Buffered.txt");
if(!file.exists()){//如果不存在,就新建該文件
file.createNewFile();
}
System.out.println("Buffered.txt的絕對路徑\t"+file.getCanonicalPath());
System.out.println("Buffered.txt的相對路徑\t"+file.getPath());
}
}
輸出
當前目錄的路徑 D:\space\workspace\Demo
Buffered.txt的絕對路徑 D:\space\workspace\Demo\Buffered.txt
Buffered.txt的相對路徑 Buffered.txt
1絕對路徑:加上盤符,就是絕對正確的地址,一般通過我的電腦打開到那個位置,復制地址就可以。
2相對路徑:
a;同級目錄: 直接填寫文件名稱;
b;下級鏈接:帶上文件夾,寫上文件名稱;
c:上級鏈接:"..\文件名稱"
第一個?。骸?C:\\mydoc\\aa.doc" , 這個用雙斜線
第二個?。骸?C:/mydoc/aa.doc" ,這個單斜線就行了
我建議你用
String path = "C:"+File.separator+"my.doc" ;
System.out.println(path);
File.separator 這是用你所用的系統默認的文件分割符,,
File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\baidu");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之處在于,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\baidu
G:\xhuoj\konw\src\baidu
文件從本地到服務器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到服務器的固定目錄,從而方便項目獲取文件,進而使程序支持EXCEL批量導入數據。
java中文件上傳到服務器的指定路徑的代碼:
在前臺界面中輸入:
form method="post" enctype="multipart/form-data" ?action="../manage/excelImport.do"
請選文件:input type="file" ?name="excelFile"
input type="submit" value="導入" onclick="return impExcel();"/
/form
action中獲取前臺傳來數據并保存
/**
* excel 導入文件
* @return
* @throws IOException
*/
@RequestMapping("/usermanager/excelImport.do")
public String excelImport(
String filePath,
MultipartFile ?excelFile,HttpServletRequest request) throws IOException{
log.info("action:{} Method:{} start","usermanager","excelImport" );
if (excelFile != null){
String filename=excelFile.getOriginalFilename();
String a=request.getRealPath("u/cms/www/201509");
SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服務器的路徑
}
log.info("action:{} Method:{} end","usermanager","excelImport" );
return "";
}
/**
* 將MultipartFile轉化為file并保存到服務器上的某地
*/
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
{ ? ?
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
System.out.println("------------"+path + "/"+ savefile);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}