Click here to Skip to main content
15,885,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my jsp file

XML
<form method="post" action="UploadServlet" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />
</form>



and I have made a folder whenever there is new user

SQL
File dir = new File("D:\\nameoffolder2");
           dir.mkdir();



now how to store the uploaded files in a particular folder.??
Posted
Comments
Mohibur Rashid 10-Sep-15 20:37pm    
You need to find out two things:
1. How to upload anything with JSP.
2. How to copy/move them in the destination folder.

Google them

1 solution

write jsp code
XML
 <form method="post" action="upload" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="dataFile" id="fileChooser"/><br/><br/>
<input type="submit" value="Upload" />


write servlet code

first include jar file orielly.cos.jar

then import this
C#
import java.util.Enumeration;
import com.oreilly.servlet.MultipartRequest;


public class upload extends HttpServlet {

    private String webTempPath;
    
    @Override
    public void init()
    {
    webTempPath = getServletContext().getRealPath("/") + "data";
    }
    
    
    

    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet upload</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet upload at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        throw new ServerException("Get method used with" + getClass().getName() + ":Post method required");
      //  processRequest(request, response);
    }

  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        MultipartRequest mpr = new MultipartRequest(request, webTempPath,10*1024*1024);
        Enumeration enumr = mpr.getFileNames();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Server upload</title>");
        out.println("</head>");
        out.println("<body>");
        for(int i=1;enumr.hasMoreElements();i++)
        out.println("The name of the uploaded file" + i + "is" + mpr.getFilesystemName((String)enumr.nextElement()) + "<br><br>");
        out.println("</body>");
        out.println("</html>");
    
        
        
        
        
        processRequest(request, response);
    }

   
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900