Loading
Upload a file to Servlet

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How you upload a file to Servlet ?


Ans)

There are several ways of doing this, below are some of them.

By using commons-fileupload

protected void doPost(
HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 try {    
 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);  
     for (FileItem item : items) {  
 if (item.isFormField()) {   
String fieldname = item.getFieldName();               
 String fieldvalue = item.getString();              
   // Write Your logic


 } else {              
 // Process form file field (input type="file").           
 String fieldname = item.getFieldName();          
 String filename = FilenameUtils.getName(item.getName()); 
 InputStream filecontent = item.getInputStream();        
   // Write Your logic

 }      
 }  
 } catch (FileUploadException e) {
 throw new ServletException("Cannot parse multipart request.", e); 
 }  
 }

By using Servlet 3.0 MultipartConfig

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
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(urlPatterns = "/MultipartUploadServlet", name = "MultipartUploadServlet")
@MultipartConfig(location = "/tmp", maxFileSize = 10485760L)
public class MultipartYourUploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                try {
                        Enumeration<String> hn = req.getHeaderNames();
                        while (hn.hasMoreElements()) {
                                String n = hn.nextElement();
                                System.out.println(n + " [" + req.getHeader(n) + "]");
                        }
                        Collection<Part> requestParts = req.getParts();

                        InputStream is = req.getInputStream();
                        int charRead = 0;
                        System.out.println("[");
                        while ((charRead = is.read()) != -1) {
                                System.out.print((char) charRead);
                        }
                        is.close();
                        System.out.println("]");
                } catch (Exception excp) {
                        excp.printStackTrace();
                }
        }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Upload a file to Servlet


Upload a file to Servlet

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free