0

Possible Duplicate:
How to upload files in JSP/Servlet?

In my JSP page I have a field for choosing files, as follows:

   <input type="file" name="uploadFile" id="uploadFile">

And in my action page, I am getting this file as follows:

   File fileUpload=request.getAttribute("fileUpload");

   String fileName="uploadedfile.jpg";
   File target=new File("D:\\",fileName);
   FileUtils.copyFile(file,target);

by using the above code I can upload the file in the named uploadedfile.jpg.But i want to get the file name and type of the uploaded file dynamically from the chosen file.How can i get it?

Community
  • 1
  • 1
Kamalam
  • 1,254
  • 6
  • 18
  • 32
  • I should point out that the code in your question doesn't upload anything. In fact, all it appears to be doing is copying a file from one place on the server to another place on the server. (Without doing any checks ... which makes it a security hole.) – Stephen C Mar 28 '12 at 01:28

1 Answers1

2

Apache Commons FileUpload can do what you want to, and more:

http://commons.apache.org/fileupload/

Some relevant methods in the FileItem class:

getSize() 
          Returns the size of the file item.

getContentType() 
          Returns the content type passed by the browser or null if not defined.

http://commons.apache.org/fileupload/apidocs/index.html

Vicente Plata
  • 3,370
  • 1
  • 19
  • 26