【Servlet标准解读】@MultipartConfig

我们先看一下api文档怎么描述@MultipartConfig的:

/**
 * Annotation that may be specified on a {@link javax.servlet.Servlet}
 * class, indicating that instances of the Servlet expect requests
 * that conform to the multipart/form-data MIME type.
 *
 * 

Servlets annotated with MultipartConfig may retrieve the * {@link javax.servlet.http.Part} components of a given * multipart/form-data request by calling * {@link javax.servlet.http.HttpServletRequest#getPart getPart} or * {@link javax.servlet.http.HttpServletRequest#getParts getParts}. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MultipartConfig { /** * The directory location where files will be stored(译:设置上传文件的临时保存目录) */ String location() default ""; /** * The maximum size allowed for uploaded files.(译:设置上传文件的最大文件大小,单位: 字节) * *

The default is -1L, which means unlimited. */ long maxFileSize() default -1L; /** * The maximum size allowed for multipart/form-data(译:设置上传请求中最大的总大小限制,单位: 字节) * requests * *

The default is -1L, which means unlimited. */ long maxRequestSize() default -1L; /** * The size threshold after which the file will be written to disk(译:如果文件的大小超过指定的阈值,文件将会被写出到磁盘,单位: 字节) */ int fileSizeThreshold() default 0; }

通过上面的定义我们可以知道@MultipartConfig主要用来指明servlet的请求当中包含文件,文件我们通过以下的方式获取:
Part part = req.getPart("filename");

下面是一个简单的示例:

1 前台文件上传页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>upload your logotitle>
head>
<body>

<form action="/multipart" method="post" enctype="multipart/form-data">
    Please choose your logo: <input type="file" name="logo"><br>
    <input type="submit" value="submit">
form>
body>
html>

效果:
这里写图片描述

2 Servlet代码片段

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.PrintWriter;

@WebServlet(urlPatterns = {"/multipart"})
@MultipartConfig(location = "G:\\maven-practice\\sample-web\\files\\",
        maxFileSize = 2, maxRequestSize = 200)
public class MultipartServletDemo extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part part = req.getPart("logo");
        PrintWriter out = resp.getWriter();
        out.println("file name: " + part.getName());
        out.println("file size: " + part.getSize());
        out.println("file content type: " + part.getContentType());
        out.println("upload successfully");
        out.flush();
        out.close();
    }
}

这里我们配置请求的最大长度为200bytes, 文件本身最大值为2bytes;

3 执行结果

当我们尝试上传大文件(超过配置的200)时,执行结果如下:
【Servlet标准解读】@MultipartConfig_第1张图片

当我们试图上传大文件(大于限制的2bytes)

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.PrintWriter;

@WebServlet(urlPatterns = {"/multipart"})
@MultipartConfig(location = "G:\\maven-practice\\sample-web\\files\\",
        maxFileSize = 2)
public class MultipartServletDemo extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part part = req.getPart("logo");
        PrintWriter out = resp.getWriter();
        out.println("file name: " + part.getName());
        out.println("file size: " + part.getSize());
        out.println("file content type: " + part.getContentType());
        out.println("upload successfully");
        out.flush();
        out.close();
    }
}

效果如下:
【Servlet标准解读】@MultipartConfig_第2张图片

正常情况下,运行结果会是:
这里写图片描述

你可能感兴趣的:(servlet标准解读,servlet,标准)