“Servlet 文件上传”的版本间差异

本页内容
(创建页面,内容为“{{DISPLAYTITLE:Servlet 文件上传}}15 = Servlet 文件上传 = Servlet 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。 本文使用到的文件有: * upload.jsp : 文件上传表单。 * message.jsp : 上传成功后跳转页面。 * UploadServlet.java : 上传处理 Servlet。 * 需要引入的 jar 文件:commons-fileupl…”)
 
Neo讨论 | 贡献
无编辑摘要
 
第28行: 第28行:
upload.jsp 文件代码如下:
upload.jsp 文件代码如下:


<pre>
<sample title="" desc="" lang="java" hererun="1">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding=&quot;UTF-8&quot;%&gt;
     pageEncoding="UTF-8"%>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
     "http://www.w3.org/TR/html4/loose.dtd">
&lt;html&gt;
<html>
&lt;head&gt;
<head>
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
&lt;title&gt;文件上传实例 - 小白教程&lt;/title&gt;
<title>文件上传实例 - 小白教程</title>
&lt;/head&gt;
</head>
&lt;body&gt;
<body>
&lt;h1&gt;文件上传实例 - 小白教程&lt;/h1&gt;
<h1>文件上传实例 - 小白教程</h1>
&lt;form method=&quot;post&quot; action=&quot;/TomcatTest/UploadServlet&quot; enctype=&quot;multipart/form-data&quot;&gt;
<form method="post" action="/TomcatTest/UploadServlet" enctype="multipart/form-data">
     选择一个文件:
     选择一个文件:
     &lt;input type=&quot;file&quot; name=&quot;uploadFile&quot; /&gt;
     <input type="file" name="uploadFile" />
     &lt;br/&gt;&lt;br/&gt;
     <br/><br/>
     &lt;input type=&quot;submit&quot; value=&quot;上传&quot; /&gt;
     <input type="submit" value="上传" />
&lt;/form&gt;
</form>
&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
</pre>
</sample>


== 编写后台 Servlet ==
== 编写后台 Servlet ==
第64行: 第64行:
UploadServlet 的源代码 如下所示:
UploadServlet 的源代码 如下所示:


<pre>
<sample title="" desc="" lang="java" hererun="1">
package com.xiaobai.test;
package com.xiaobai.test;


第85行: 第85行:
  * Servlet implementation class UploadServlet
  * Servlet implementation class UploadServlet
  */
  */
@WebServlet(&quot;/UploadServlet&quot;)
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
public class UploadServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;


     // 上传文件存储目录
     // 上传文件存储目录
     private static final String UPLOAD_DIRECTORY = &quot;upload&quot;;
     private static final String UPLOAD_DIRECTORY = "upload";


     // 上传配置
     // 上传配置
第106行: 第106行:
             // 如果不是则停止
             // 如果不是则停止
             PrintWriter writer = response.getWriter();
             PrintWriter writer = response.getWriter();
             writer.println(&quot;Error: 表单必须包含 enctype=multipart/form-data&quot;);
             writer.println("Error: 表单必须包含 enctype=multipart/form-data");
             writer.flush();
             writer.flush();
             return;
             return;
第116行: 第116行:
         factory.setSizeThreshold(MEMORY_THRESHOLD);
         factory.setSizeThreshold(MEMORY_THRESHOLD);
         // 设置临时存储目录
         // 设置临时存储目录
         factory.setRepository(new File(System.getProperty(&quot;java.io.tmpdir&quot;)));
         factory.setRepository(new File(System.getProperty("java.io.tmpdir")));


         ServletFileUpload upload = new ServletFileUpload(factory);
         ServletFileUpload upload = new ServletFileUpload(factory);
第127行: 第127行:


         // 中文处理
         // 中文处理
         upload.setHeaderEncoding(&quot;UTF-8&quot;);
         upload.setHeaderEncoding("UTF-8");


         // 构造临时路径来存储上传的文件
         // 构造临时路径来存储上传的文件
         // 这个路径相对当前应用的目录
         // 这个路径相对当前应用的目录
         String uploadPath = request.getServletContext().getRealPath(&quot;./&quot;) + File.separator + UPLOAD_DIRECTORY;
         String uploadPath = request.getServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;


         // 如果目录不存在则创建
         // 如果目录不存在则创建
第141行: 第141行:
         try {
         try {
             // 解析请求的内容提取文件数据
             // 解析请求的内容提取文件数据
             @SuppressWarnings(&quot;unchecked&quot;)
             @SuppressWarnings("unchecked")
             List&lt;FileItem&gt; formItems = upload.parseRequest(request);
             List<FileItem> formItems = upload.parseRequest(request);


             if (formItems != null &amp;&amp; formItems.size() &gt; 0) {
             if (formItems != null && formItems.size() > 0) {
                 // 迭代表单数据
                 // 迭代表单数据
                 for (FileItem item : formItems) {
                 for (FileItem item : formItems) {
第156行: 第156行:
                         // 保存文件到硬盘
                         // 保存文件到硬盘
                         item.write(storeFile);
                         item.write(storeFile);
                         request.setAttribute(&quot;message&quot;,
                         request.setAttribute("message",
                             &quot;文件上传成功!&quot;);
                             "文件上传成功!");
                     }
                     }
                 }
                 }
             }
             }
         } catch (Exception ex) {
         } catch (Exception ex) {
             request.setAttribute(&quot;message&quot;,
             request.setAttribute("message",
                     &quot;错误信息: &quot; + ex.getMessage());
                     "错误信息: " + ex.getMessage());
         }
         }
         // 跳转到 message.jsp
         // 跳转到 message.jsp
         request.getServletContext().getRequestDispatcher(&quot;/message.jsp&quot;).forward(
         request.getServletContext().getRequestDispatcher("/message.jsp").forward(
                 request, response);
                 request, response);
     }
     }
}
}
</pre>
</sample>
message.jsp 文件代码如下:
message.jsp 文件代码如下:


<pre>
<sample title="" desc="" lang="java" hererun="1">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding=&quot;UTF-8&quot;%&gt;
     pageEncoding="UTF-8"%>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
     "http://www.w3.org/TR/html4/loose.dtd">
&lt;html&gt;
<html>
&lt;head&gt;
<head>
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
&lt;title&gt;文件上传结果&lt;/title&gt;
<title>文件上传结果</title>
&lt;/head&gt;
</head>
&lt;body&gt;
<body>
     &lt;center&gt;
     <center>
         &lt;h2&gt;${message}&lt;/h2&gt;
         <h2>${message}</h2>
     &lt;/center&gt;
     </center>
&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
</pre>
</sample>


== 编译和运行 Servlet ==
== 编译和运行 Servlet ==
第195行: 第195行:
编译上面的 Servlet UploadServlet,并在 web.xml 文件中创建所需的条目,如下所示:
编译上面的 Servlet UploadServlet,并在 web.xml 文件中创建所需的条目,如下所示:


<pre>
<sample title="" desc="" lang="java" hererun="1">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
<?xml version="1.0" encoding="UTF-8"?>
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&gt;
     id="WebApp_ID" version="2.5">
   &lt;servlet&gt;
   <servlet>
     &lt;display-name&gt;UploadServlet&lt;/display-name&gt;
     <display-name>UploadServlet</display-name>
     &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
     <servlet-name>UploadServlet</servlet-name>
     &lt;servlet-class&gt;com.xiaobai.test.UploadServlet&lt;/servlet-class&gt;
     <servlet-class>com.xiaobai.test.UploadServlet</servlet-class>
   &lt;/servlet&gt;
   </servlet>


   &lt;servlet-mapping&gt;
   <servlet-mapping>
     &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
     <servlet-name>UploadServlet</servlet-name>
     &lt;url-pattern&gt;/TomcatTest/UploadServlet&lt;/url-pattern&gt;
     <url-pattern>/TomcatTest/UploadServlet</url-pattern>
   &lt;/servlet-mapping&gt;
   </servlet-mapping>
&lt;/web-app&gt;
</web-app>
</pre>
</sample>
现在尝试使用您在上面创建的 HTML 表单来上传文件。当您在浏览器中访问:http://localhost:8080/TomcatTest/upload.jsp ,演示如下所示:
现在尝试使用您在上面创建的 HTML 表单来上传文件。当您在浏览器中访问:http://localhost:8080/TomcatTest/upload.jsp ,演示如下所示:

2022年8月17日 (三) 20:27的最新版本

Servlet 文件上传

Servlet 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。

本文使用到的文件有:

  • upload.jsp : 文件上传表单。
  • message.jsp : 上传成功后跳转页面。
  • UploadServlet.java : 上传处理 Servlet。
  • 需要引入的 jar 文件:commons-fileupload-1.3.2、commons-io-2.5.jar。

结构图如下所示:

注意:Servlet3.0 已经内置了文件上传这一特性,开发者不再需要将 Commons FileUpload 组件导入到工程中去。

接下来我们详细介绍。

创建一个文件上传表单

下面的 HTML 代码创建了一个文件上传表单。以下几点需要注意:

  • 表单 method 属性应该设置为 POST 方法,不能使用 GET 方法。
  • 表单 enctype 属性应该设置为 multipart/form-data.
  • 表单 action 属性应该设置为在后端服务器上处理文件上传的 Servlet 文件。下面的实例使用了 UploadServlet Servlet 来上传文件。
  • 上传单个文件,您应该使用单个带有属性 type="file" 的 <input .../> 标签。为了允许多个文件上传,请包含多个 name 属性值不同的 input 标签。输入标签具有不同的名称属性的值。浏览器会为每个 input 标签关联一个浏览按钮。

upload.jsp 文件代码如下:


示例

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传实例 - 小白教程</title>
</head>
<body>
<h1>文件上传实例 - 小白教程</h1>
<form method="post" action="/TomcatTest/UploadServlet" enctype="multipart/form-data">
    选择一个文件:
    <input type="file" name="uploadFile" />
    <br/><br/>
    <input type="submit" value="上传" />
</form>
</body>
</html>

编写后台 Servlet

以下是 UploadServlet 的源代码,同于处理文件上传,在这之前我们先确保依赖包已经引入到项目的 WEB-INF/lib 目录下:

你可以直接下载本站提供的两个依赖包:

UploadServlet 的源代码 如下所示:


示例

package com.xiaobai.test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // 上传文件存储目录
    private static final String UPLOAD_DIRECTORY = "upload";

    // 上传配置
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB

    /**
     * 上传数据及保存文件
     */
    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        // 检测是否为多媒体上传
        if (!ServletFileUpload.isMultipartContent(request)) {
            // 如果不是则停止
            PrintWriter writer = response.getWriter();
            writer.println("Error: 表单必须包含 enctype=multipart/form-data");
            writer.flush();
            return;
        }

        // 配置上传参数
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        // 设置临时存储目录
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);

        // 设置最大文件上传值
        upload.setFileSizeMax(MAX_FILE_SIZE);

        // 设置最大请求值 (包含文件和表单数据)
        upload.setSizeMax(MAX_REQUEST_SIZE);

        // 中文处理
        upload.setHeaderEncoding("UTF-8");

        // 构造临时路径来存储上传的文件
        // 这个路径相对当前应用的目录
        String uploadPath = request.getServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;

        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }

        try {
            // 解析请求的内容提取文件数据
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(request);

            if (formItems != null && formItems.size() > 0) {
                // 迭代表单数据
                for (FileItem item : formItems) {
                    // 处理不在表单中的字段
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);
                        // 在控制台输出文件的上传路径
                        System.out.println(filePath);
                        // 保存文件到硬盘
                        item.write(storeFile);
                        request.setAttribute("message",
                            "文件上传成功!");
                    }
                }
            }
        } catch (Exception ex) {
            request.setAttribute("message",
                    "错误信息: " + ex.getMessage());
        }
        // 跳转到 message.jsp
        request.getServletContext().getRequestDispatcher("/message.jsp").forward(
                request, response);
    }
}

message.jsp 文件代码如下:


示例

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传结果</title>
</head>
<body>
    <center>
        <h2>${message}</h2>
    </center>
</body>
</html>

编译和运行 Servlet

编译上面的 Servlet UploadServlet,并在 web.xml 文件中创建所需的条目,如下所示:


示例

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
  <servlet>
    <display-name>UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>com.xiaobai.test.UploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/TomcatTest/UploadServlet</url-pattern>
  </servlet-mapping>
</web-app>

现在尝试使用您在上面创建的 HTML 表单来上传文件。当您在浏览器中访问:http://localhost:8080/TomcatTest/upload.jsp ,演示如下所示:

此页面最后编辑于2022年8月17日 (星期三) 20:27。