codeomnitrix
codeomnitrix

Reputation: 4249

File upload in jsp

I want to upload any type of file(one file at a time) but i am facing the problem my **html form is returning only source filename not path. my html file: Select a file to upload :

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);
%>

Upvotes: 0

Views: 1248

Answers (1)

Harry Joy
Harry Joy

Reputation: 59694

html form is returning only source filename not path

You do not need to know file path for uploading a file.

You have to change/add enctype="multipart/formdata" in your form tag. This way you can access file no need to get file path.

Also look at apache commons fileupload.

Upvotes: 1

Related Questions