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

이 코드는 유령처럼 보입니다. 파일 업로드에 대해 살펴 보셨습니까? –

+0

관련 항목 : http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408 – BalusC

답변

1

HTML 양식을하지 경로 당신은 파일을 업로드 파일 경로를 알 필요가 없습니다

만 소스 파일 이름을 반환합니다.

양식 태그에 enctype="multipart/formdata"을 변경/추가해야합니다. 이렇게하면 파일 경로에 액세스 할 필요가 없습니다.

apache commons fileupload도 확인하십시오.

관련 문제