title: how to upload excel sheet in servlet using browser
body: <p>I want to upload my excel sheet in my local system using servlet API but i am getting  exception </p>
  
  <p><strong>java.io.FileNotFoundException: D:\Core-Java_Eclipse.metadata.plugins<br>
  \org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadMenu\uploaded (Access is denied)</strong></p>
  
  <p>here i am using two jar 1. commons-fileupload-1.2.1.jar   2. commons-io-1.4.jar
  i dont want to use third party implementation jar </p>
  
  <p>Please see my code which is written in servlet </p>
  
  <pre><code>import java.io.*;
  import java.util.Iterator;
  import java.util.List;
  import java.util.StringTokenizer;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.commons.fileupload.FileItem;
  import org.apache.commons.fileupload.disk.DiskFileItem;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  
  
   public class UploadServlet extends HttpServlet {
  
  
    protected void service(HttpServletRequest request, HttpServletResponse 
    response)        throws ServletException, IOException {
      String ctxPath=request.getRealPath("/");
      File dir=new File(ctxPath,"uploaded");
      if(!dir.exists()){
          dir.mkdir();
      }
      Writer out=response.getWriter();
      boolean uploadData=ServletFileUpload.isMultipartContent(request);
      if(uploadData){
          DiskFileItemFactory factory=new DiskFileItemFactory();
          factory.setSizeThreshold(1024*1024*10);
          factory.setRepository(new File("c:\\temp"));
          ServletFileUpload upload=new ServletFileUpload(factory);
          upload.setSizeMax(1024*1024*50);
          try{
              List&lt;FileItem&gt; fileItems=upload.parseRequest(request);
              Iterator&lt;FileItem&gt; i=fileItems.iterator();
              while(i.hasNext()){
                  FileItem fi=i.next();
                  if(!fi.isFormField()){
                      String fieldName=fi.getFieldName();
                      String fileName=fi.getName();
                      String contentType=fi.getContentType();
                      boolean isInMemory=fi.isInMemory();
                      long sizeInByte=fi.getSize();
                      StringTokenizer tok=new 
                                StringTokenizer(fileName,"/");
                      String fileToWrite="";
                      while(tok.hasMoreTokens()){
                           fileToWrite=tok.nextToken();
                      }
                      File file=new File(dir,fileToWrite);
                      fi.write(file);
                  }
              }
          }catch(Exception e){
              e.printStackTrace();
          }
           out.write("&lt;h1&gt;File Uploaded in &lt;br/&gt;"+dir.getAbsolutePath());
      }else{
          out.write("No File Uploaded");
      }
  
  }
  }
  </code></pre>
  
  <p>please tell me how to resolve my problem and how to upload this file is user 
  define    directry means (D:// menu) folder
  thanx is Advance </p>