Posts Tagged ‘web.xml’

A Simple File Uploader Servlet

The HTML form

<form action="FileUpload?artifact=endpoints&type=items" enctype="multipart/form-data" method="post">
    <input type="file"/>
    <button type="submit">Upload</button>
</form>

 

The …./WEB-INF/web.xml

<web-app>
        .
        .
        .
  <servlet>
     <servlet-name>FileUpload</servlet-name>
     <servlet-class>org.adroitlogic.ultraesb.zabbix.FileUpload</servlet-class>
  </servlet>

  <servlet-mapping>
     <servlet-name>FileUpload</servlet-name>
     <url-pattern>FileUpload</url-pattern>
  </servlet-mapping>
        .
        .
        .
</web-app>

The form action value should be similar to the url-pattern of servlet-mapping in web.xml, i.e FileUpload. The rest of the string in form action specifies the url parameters. The question mark indicates the start of url parameters. Two url parameters are defined in this form action, artifact and type. The value of artifact is endpoints and the value of type is items.

The values of these url parameters can be retrieved in the servlet by request.getParameter(“urlParameter”) method. (servlet lines 24,25)

 

The Servlet

The third party jars used : Apache Commons File Upload and Apache Commons IO

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

public class FileUpload extends HttpServlet {

    private static final Logger logger = 
        LoggerFactory.getLogger(FileUpload.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse 
        response) {

        StringBuilder basePath = new StringBuilder();
        basePath.append("conf").append(File.separator).append("zabbix").
            append(File.separator).append(request.getParameter("artifact")).
            append(File.separator).append(request.getParameter("type")).
            append(File.separator);

        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        ServletFileUpload uploadHandler = new 
            ServletFileUpload(fileItemFactory);
        
        String destination = "the destination url that you want to send the 
            redirect to";
        response.setContentType("text/plain");

        try {
            List<FileItem> items = (List<FileItem>) 
                uploadHandler.parseRequest(request);
            for (FileItem item : items) {

                if (item.isFormField()) {

                    String fileName = item.getString();

                    int separatorIndex = fileName.lastIndexOf(File.separator);
                    if (separatorIndex > 0) {
                        fileName = fileName.substring(separatorIndex + 1);
                    }
                    basePath.append(fileName);
                    File file = new File( basePath.toString());
                    item.write(file);
                }                                       
           }
            response.sendRedirect(response.encodeURL(destination));
        } catch (Exception e) {
             logger.warn("Fault in uploading file", e);
        }
    }
}
Advertisements