2011-05-01 59 views
0

我想上傳任何類型的文件(一次一個文件),但我面臨的問題我**的HTML表格只返回源文件名不路徑。 我的html文件: 選擇要上傳的文件: 在jsp上的文件上傳

FileUpload.jsp :- 

<%@ page import="java.util.*,java.io.*"%> 

<% 
String path=request.getParameter("filename"); 
System.out.println(path); 
String newPath=""; 
int count=0; 

if(path!=null) 
{ 
String arr[]=new String[100]; 
StringTokenizer st=new StringTokenizer(path,"/"); 
while(st.hasMoreTokens()) 
{ 
    arr[count]=st.nextToken(); 
count++; 
} 
// create ur own path 

newPath="/home/saurabh/"+arr[count-1]; 
int c; 
try{ 
FileInputStream fis=new FileInputStream(path); 
FileOutputStream fos=new FileOutputStream(newPath); 
while((c=fis.read())!=-1) 
{ 
fos.write((char)c); 
} 
} 
catch(Exception e){e.printStackTrace();} 
} 
out.println("Thanks for using"); 
out.println("<br>"); 
out.println("<br>"); 
out.println("1.File1 Uploaded from :: "+path); 
out.println("<br>"); 
out.println("<br>"); 
out.println("2.Uploaded File1 is Saved in :: "+newPath); 
%> 
+2

這段代碼看起來很怪,你看看commons fileupload? – 2011-05-01 12:58:46

+0

相關:http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408 – BalusC 2011-05-02 13:55:06

回答

1

HTML表單只返回源文件名沒有路徑

你不需要知道文件路徑上傳文件。

您必須在您的表單標籤中更改/添加enctype="multipart/formdata"。這樣您就可以訪問文件,無需獲取文件路徑。

另請參見apache commons fileupload