2011-05-01 5 views
0

Je veux télécharger n'importe quel type de fichier (un fichier à la fois), mais je suis confronté au problème mon formulaire ** html ne renvoie que le nom de fichier source pas le chemin. mon fichier html: Sélectionnez un fichier à télécharger: Télécharger le fichier dans 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

Ce code a l'air effrayant, avez-vous jeté un oeil sur les communs fileupload? –

+0

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

Répondre

1

formulaire HTML retourne uniquement le nom de fichier source non chemin

Vous n'avez pas besoin de connaître le chemin du fichier pour le téléchargement d'un fichier.

Vous devez changer/ajouter enctype="multipart/formdata" dans votre formulaire. De cette façon, vous pouvez accéder au fichier pas besoin d'obtenir le chemin du fichier.

Regardez également apache commons fileupload.