Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

restrict upload to certain extensions...???

843840Sep 27 2007 — edited Jul 4 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 1 2008
Added on Sep 27 2007
13 comments
858 views