Hi,
I have file upload program using apache commons file upload.
though i want to know, how to restrict the certain files to be upload
and display the messege (if extension of file selected is invalid )
while he click the upload button.
Since i have keep right now size restriction but exception is not catched
and whole my page display the tomcat error page.
mycode is below :
<%
//FILE UPLOAD USING APACHE COMMONS
//System.out.println(request.getHeader("accept").toString());
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart)
{
//Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096);
//getting the actual path to upload the file
String path2upload = application.getRealPath("/WEB-INF/uploads");
File file = new File(path2upload);
if(!file.exists()){ file.mkdirs(); }
if (!file.exists())
file.createNewFile();
factory.setRepository(file);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Parse the request
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if ((!item.isFormField())&&(item.getName().length()>1))
{
out.println("File Uploaded : "+item.getName()); out.println("<br>");
out.println("File Uploaded Length :"+item.getName().length());
out.println("<br>");
String fileName = item.getName().substring(item.getName().lastIndexOf("\\"),item.getName().length());
//out.println("File NAME GOT :"+fileName);
File uploadedFile = new File(file.getPath()+"\\"+fileName);
out.println("<br>");
//out.println("Writing to : "+uploadedFile.getAbsolutePath());
out.println("<br><br>");
item.write(uploadedFile);
item.getOutputStream().close();
response.sendRedirect("upload.jsp?msg=SUCCESS");
}
}//end while
}//end if
-----
I want to allow only extensions .jpg, .gif, .pdf , .doc
any help appreciated.
thanks in advance.
Ghanshyam