Sunday, November 25, 2012
Everyone of you know that files are being uploaded to the server and you can easily download them. You must have thought of how these files are being uploaded to server. Today I am going to post how you can very easily create your own file upload server; Previously before Servlet3.0 technology you might have seen that it was a bit difficult process and you had to use third-party library like Apache-commons. But now it has become very easy.
  • The first thing you will have to do is to create the upload-form using HTML. In the form you will have to mention the enctype="multipart/form-data" .
  • In servlet there is an annotation called MultipartConfig . It has different params like location, maxFileSize and so on. The location should be a valid folder path on server where the file will be uploaded. Now write the part and also do string processing to get the name of the file ro use the same name while uploading it.
  • Now display the details of the file uploaded by request forwarding to a JSP.
--------------------------------------------------------------------------------------------------------------------------
HTML form and code
--------------------------------------------------------------------------------------------------------------------------
Select a file to upload

<html>
  <body>
     <h3>File Upload Form</h3>
      Select a file to upload
      <form action="./upload.html" method="post"
                enctype="multipart/form-data">
            <input type="file" name="file" size="50" />
            <br/>
            <input type="submit" value="Upload File" />
      </form>
  </body>
</html>
--------------------------------------------------------------------------------------------------------------------------
 Servlet and JSP Code
--------------------------------------------------------------------------------------------------------------------------
/* Servlet Code */
import java.io.IOException;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/upload.html")
@MultipartConfig(location="D:/ Documents",fileSizeThreshold=1024*1024,
maxFileSize=50*1024*1024,maxRequestSize=5*1024*1024*1024)

public final class Uploader extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Collection<Part> parts=request.getParts();
        StringBuffer sb=new StringBuffer();
        sb.append("<h2>Total "+parts.size()+" parts uploaded</h2>");
       
        for(Part p: parts){
            getDetails(sb, p);
            p.write(getFileName(p)); 
//writing file to server
        }
       
        request.setAttribute("partDetails",sb);
        RequestDispatcher view=request.getRequestDispatcher("result.jsp");
        view.forward(request, response); 
//forwarding to JSP
    }
   
    private String getFileName(Part p){
        String header=p.getHeader("content-disposition");
        String filename = header.substring(header.indexOf("filename=\"")).split("\"")[1]; 
//getting filename
       
        return filename;
    }
   
    private void getDetails(StringBuffer sb,Part p){
        sb.append("<p></p>");
        for(String header:p.getHeaderNames()){
            sb.append("<b>"+header+" : </b>");
            sb.append(p.getHeader(header)+"</br>");
        }
    }
}

/* JSP Code */

<html>
    <head><title>Upload Details</title></head>
    <% StringBuffer result=(StringBuffer)request.getAttribute("partDetails");

Output as seen in Chrome
     %>
    <body>
        <%= result %>
    </body>
</html>




--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD LINKS
--------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts