2011-05-01 62 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