kg12.Ex12_2.java Source code

Java tutorial

Introduction

Here is the source code for kg12.Ex12_2.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kg12;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author C0114112
 */
@WebServlet(name = "Ex12_2", urlPatterns = { "/Ex12_2" })
public class Ex12_2 extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Ex12_2</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>???</h1>");

            // multipart/form-data ??
            if (ServletFileUpload.isMultipartContent(request)) {
                out.println("???<br>");
            } else {
                out.println("?????<br>");
                out.close();
                return;
            }

            // ServletFileUpload??
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sfu = new ServletFileUpload(factory);

            // ???
            int fileSizeMax = 1024000;
            factory.setSizeThreshold(1024);
            sfu.setSizeMax(fileSizeMax);
            sfu.setHeaderEncoding("UTF-8");

            // ?
            String format = "%s:%s<br>%n";

            // ???????
            FileItemIterator fileIt = sfu.getItemIterator(request);

            while (fileIt.hasNext()) {
                FileItemStream item = fileIt.next();

                if (item.isFormField()) {
                    //
                    out.print("<br>??<br>");
                    out.printf(format, "??", item.getFieldName());
                    InputStream is = item.openStream();

                    // ? byte ??
                    byte[] b = new byte[255];

                    // byte? b ????
                    is.read(b, 0, b.length);

                    // byte? b ? "UTF-8" ??String??? result ?
                    String result = new String(b, "UTF-8");
                    out.printf(format, "", result);
                } else {
                    //
                    out.print("<br>??<br>");
                    out.printf(format, "??", item.getName());
                    InputStream is = item.openStream();

                    String fileName = item.getName();
                    int len = 0;
                    byte[] buffer = new byte[fileSizeMax];

                    FileOutputStream fos = new FileOutputStream(
                            "D:\\NetBeansProjects\\ap2_www\\web\\kg12\\" + fileName);
                    try {
                        while ((len = is.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    } finally {
                        fos.close();
                    }

                }
            }
            out.println("</body>");
            out.println("</html>");
        } catch (FileUploadException e) {
            out.println(e + "<br>");
            throw new ServletException(e);
        } catch (Exception e) {
            out.println(e + "<br>");
            throw new ServletException(e);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}