com.seer.datacruncher.services.HttpFileUpload.java Source code

Java tutorial

Introduction

Here is the source code for com.seer.datacruncher.services.HttpFileUpload.java

Source

/*
 * Copyright (c) 2015  www.see-r.com
 * All rights reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.seer.datacruncher.services;

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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/*
This servlet can be used as test to receive datastream sent from the "uploading" functionality
(generation stream -> http client)
*/

public class HttpFileUpload extends HttpServlet {
    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.write("Success");
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            FileItemFactory factory = new DiskFileItemFactory();

            ServletFileUpload upload = new ServletFileUpload(factory);

            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();

                if (!item.isFormField()) {
                    String fileName = item.getName();

                    String str = getServletContext().getRealPath("/");
                    fileName = fileName.substring(0, fileName.indexOf(".")) + ".txt";
                    File uploadedFile = new File(str + fileName);
                    item.write(uploadedFile);

                    out.println("<Status>Success: File has been uploaded at " + uploadedFile.getAbsolutePath()
                            + "</Status>");
                    out.flush();
                    out.close();

                    return;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        out.println("<Status>Failure</Status>");
        out.flush();
        out.close();
    }

    public void init() throws ServletException {
    }
}
/*
-------------------------------------- WEB.XML content --------------------------------------
    
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HttpFileUpload</servlet-name>
<servlet-class>com.test.HttpFileUpload</servlet-class>
  </servlet>
    
  <servlet-mapping>
<servlet-name>HttpFileUpload</servlet-name>
<url-pattern>/FileUploadServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
    
-------------------------------------- index.jsp content --------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
   <meta http-equiv="pragma" content="no-cache">
   <meta http-equiv="cache-control" content="no-cache">
   <meta http-equiv="expires" content="0">
   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
   <meta http-equiv="description" content="This is my page">
  </head>
    
  <body>
This is my JSP page. <br>
  </body>
</html>
    
*/