Uploading file using Servlet/JSP with the help of apache commons-fileupload library

Here is the example to upload the file using commons fileupload library.

package imagesvideos.webcontroller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

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

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        String fileName=request.getParameter("fileName");
        writer.write("<br/>File Name : "+fileName);
        //Processing file upload //////////////////////////////////////////
        DiskFileItemFactory  fileItemFactory = new DiskFileItemFactory ();
        fileItemFactory.setSizeThreshold(5*1024*1024);
        File fileLocation=File.createTempFile("MyCMS""UplaodedFile");
        fileItemFactory.setRepository(fileLocation);
        ServletFileUpload fileUpload=new ServletFileUpload(fileItemFactory);
        try {
            List items = fileUpload.parseRequest(request);
            for (Object object : items) {
                FileItem item=(FileItem) object;
                if(item.isFormField()){
                    writer.write("<br/> isFormField : "+item.getFieldName()+" = "+item.getString());
                    fileName=item.getString();
                }else{
                    writer.write("<br/>notFormField  : "+item.getFieldName()+" = "+item.getName());
                    File file=new File("f:/mycmsdata",item.getName());
                    try {
                        item.write(file);
                    } catch (Exception e) {
                        e.printStackTrace(System.out);
                    }
                }
            }
            
        } catch (FileUploadException e) {
            e.printStackTrace(System.out);
        }
        ////////////////////////////////////////////////////////////////////
        writer.write("File uploaded");
        
    }

}

Form code :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="UploadImageVideo" method="post" enctype="multipart/form-data" >
File name : <input type="text" name="fileName"/><br/>
File : <input type="file" name="filename"/><br/>
<input type="submit"/>
</form>
</body>
</html>

No comments:

Popular Posts