com.sishuok.chapter4.web.servlet.UploadServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.sishuok.chapter4.web.servlet.UploadServlet.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sishuok.chapter4.web.servlet;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

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;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-6-22 ?3:04
 * <p>Version: 1.0
 */
@MultipartConfig(location = "", //? javax.servlet.context.tempdir mvn jetty:run chapter4\target\tmp
        maxRequestSize = 1024 * 1024 * 2, //?? ?? -1??
        maxFileSize = 1024 * 1024 * 1, //??-1??
        fileSizeThreshold = 1024 * 1024 * 10 //??
)
@WebServlet(name = "uploadServlet1", urlPatterns = "/upload")
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {

        try {

            //servlet?Partnull
            //9.0.4.v20130625 ??
            System.out.println(req.getParameter("name"));

            //?Part
            System.out.println("\n\n==========file1");
            Part file1Part = req.getPart("file1"); //?? ?Part
            InputStream file1PartInputStream = file1Part.getInputStream();
            System.out.println(IOUtils.toString(file1PartInputStream));
            file1PartInputStream.close();

            // ??
            System.out.println("\n\n==========file2");
            Part file2Part = req.getPart("file2");
            InputStream file2PartInputStream = file2Part.getInputStream();
            System.out.println(IOUtils.toString(file2PartInputStream));
            file2PartInputStream.close();

            System.out.println("\n\n==========parameter name");
            //????
            System.out.println(IOUtils.toString(req.getPart("name").getInputStream()));
            //Part??? jettyparameters??
            System.out.println(req.getParameter("name"));

            //?? ? req.getInputStream(); ??

            System.out.println("\n\n=============all part");
            for (Part part : req.getParts()) {
                System.out.println("\n\n=========name:::" + part.getName());
                System.out.println("=========size:::" + part.getSize());
                System.out.println("=========content-type:::" + part.getContentType());
                System.out
                        .println("=========header content-disposition:::" + part.getHeader("content-disposition"));
                System.out.println("=========file name:::" + getFileName(part));
                InputStream partInputStream = part.getInputStream();
                System.out.println("=========value:::" + IOUtils.toString(partInputStream));

                //
                partInputStream.close();
                // ? ? ?
                part.delete();

            }

        } catch (IllegalStateException ise) {
            //
            ise.printStackTrace();
            String errorMsg = ise.getMessage();
            if (errorMsg.contains("Request exceeds maxRequestSize")) {
                //?
            } else if (errorMsg.contains("Multipart Mime part file1 exceeds max filesize")) {
                //? ??
            } else {
                //
            }
        }

    }

    //?????API?content-disposition??
    //servlet 3.1 Part.getSubmittedFileName????
    private String getFileName(final Part part) {
        if (part == null) {
            return null;
        }

        if (part.getContentType() == null) {
            return null;
        }

        String contentDisposition = part.getHeader("content-disposition");

        if (StringUtils.isEmpty(contentDisposition)) {
            return null;
        }
        // form-data; name="file1"; filename="TODO.txt"
        return StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");

    }
}