当前位置: 首页 > article >正文

第十五章 文件上传

目录

一、文件上传注意点

二、JavaWeb上传文件的核心

 三、常规的JavaWeb上传实现

四、运行效果


一、文件上传注意点

1. 为保证服务器安全,上传文件应该放在外界无法直接访问的目录下,比如放于WEB-INF目录下。

2. 为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名。

3. 要限制上传文件的最大值

4. 可以限制上传文件的类型,在收到上传文件名时,判断后缀名是否合法。

二、JavaWeb上传文件的核心

JavaWeb文件上传的核心是使用Servlet API中的javax.servlet.http.Part接口,它代表了一个HTML表单中的文件字段或其他类型的数据字段,以下是简单的代码示例:

@WebServlet("/upload")
@MultipartConfig(maxFileSize = 16177215) // 设置最大文件大小
public class UploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Part filePart = request.getPart("file"); // 获取上传的文件
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // 获取文件名
 
        // 保存文件逻辑(这里需要自己实现)
        // 例如,保存到服务器的某个目录
        filePart.write(Paths.get("uploadDir", fileName).toString());
 
        // 响应
        response.getWriter().println("File " + fileName + " uploaded successfully");
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>UploadFile</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>UploadFile Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Apache Commons FileUpload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- Apache Commons IO -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.13.0</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>UploadFile</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.3.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.13.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.3.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 三、常规的JavaWeb上传实现

我们通常会采用开源的Apache Commons FileUpload来实现文件上传,其中有几个比较重要的类。ServletFileUpload类负责处理上传的文件数据,使用其parseRequest(HttpServletRequest)方法将表单中每个输入项(比如表单中每一个HTML标签提交的数据)封装成一个FileItem对象,然后以List列表的形式返回,在使用ServletFileUpload对象解析请求时需要DiskFileItemFactory对象。所以,我们需要在进行解析工作前构造好DiskFileItemFactory对象,通过ServletFileUpload对象的构造方法或setFileItemFactory方法设置ServletFileUpload对象的fileItemFactory属性。大致的原理图如下:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>UploadFile</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>UploadFile Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Apache Commons FileUpload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- Apache Commons IO -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.13.0</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>UploadFile</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.3.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.13.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.3.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
package servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class UploadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        // 判断上传的文件是普通表单还是带文件的表单
        if (!ServletFileUpload.isMultipartContent(request)) {
            return; // 普通表单直接返回
        }

        // 创建上传文件的路径,建议在WEB-INF路径下,比较安全,用户无法直接访问上传的文件
        String filePath = getServletContext().getRealPath("/WEB-INF/uploads");

        // 创建固定的文件上传目录
        File uploadFile = new File(filePath);
        if (!uploadFile.exists()) {
            uploadFile.mkdir();
        }

        // 缓存 临时文件
        // 假如文件超过预期大小,就把它放到一个临时文件中,设定指定时间后自动删除,或者提醒用户转存为永久
        String tmpPath = this.getServletContext().getRealPath("/WEB-INF/tmp");
        File tmpFile = new File(tmpPath);
        if (!tmpFile.exists()) {
            tmpFile.mkdir();
        }
        // 处理文件的上传,一般都需要通过流来获取,我们可以使用request.getInputStream(),原生的文件上传流来获取。
        // 项目中为了代码健壮性和便捷性,使用开源封装好的工具来实现。common-fileupload(它依赖与commons-io组件)

        // 1. 创建DiskFileItemFactory,处理文件上传路径或者大小限制
        DiskFileItemFactory factory = new DiskFileItemFactory(); //
        factory.setSizeThreshold(1024 * 1024); // 设置缓冲区大小为1M
        factory.setRepository(tmpFile); // 临时保存文件的目录

        // 2. 获取ServletFileUpload
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 监听上传文件进度:
        upload.setProgressListener(new ProgressListener() {
            // l:已经读取到的文件大小
            // l1:文件大小
            @Override
            public void update(long l, long l1, int i) {
                System.out.println("总大小:" + l1 + "已上传:" + l);
            }
        });
        // 处理乱码问题
        upload.setHeaderEncoding("UTF-8");
        // 设置单个文件最大值
        upload.setFileSizeMax(1024 * 1024 * 10);
        // 设置总共能够上传文件的大小 10MB
        upload.setSizeMax(1024 * 1024 * 10);

        // 3. 处理上传的文件
        // 把前端请求解析,封装成一个FileItem对象,需要从ServletFileUpload对象中获取
        List<FileItem> fileItems = null;
        try {
            fileItems = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        // fileItem 每一个表单对象
        for (FileItem fileItem: fileItems) {
            // 判断上传的文件是普通表单还是带文件的表单
            if (fileItem.isFormField()) {
                // getFiledName指的是前端表单控件的name
                String name = fileItem.getFieldName();
                String value = fileItem.getString("UTF-8");// 处理乱码
                System.out.println(name + ":" + value);
            } else {
                String uploadFileName = fileItem.getName();
                if (uploadFileName.trim().equals("") || uploadFileName == null) {
                    continue;
                }
                // 获取文件名
                String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
                // 获取文件后缀
                String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
                // 如果文件后缀名不是我们需要的,就不上传,该逻辑此处不做处理
                // 通过UUID生成一个唯一的路径
                String uuid = UUID.randomUUID().toString();
                // 文件真实存在的唯一路径
                String realPath = filePath + "/" + uuid;
                File realPathFile = new File(realPath);
                // 给每个文件创建一个文件夹
                if (!realPathFile.exists()) {
                    realPathFile.mkdir();
                }

                // 完整文件名 文件夹+文件名 /xx/xx/xx.jpg
                String completeFileName = realPath + "/" + fileName;
                // 获得文件上传流
                InputStream inputStream = fileItem.getInputStream();
                // 创建文件输出流
                FileOutputStream fos = new FileOutputStream(completeFileName);
                // 创建一个缓冲区
                byte[] buffer = new byte[1024 * 1024];
                int len = 0;
                // 判断文件是否读取完毕
                while ((len = inputStream.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                inputStream.close();

                // 输出文件流到上传文件路径下也可以这样写
                // fileItem.write(completeFileName);

                fileItem.delete(); //上传成功,删除临时文件
                request.setAttribute("msg", "文件上传成功");
                request.getRequestDispatcher("info.jsp").forward(request, response);
            }
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app
    version="4.0"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>uploadServlet</servlet-name>
    <servlet-class>servlet.UploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>/upload.do</url-pattern>
  </servlet-mapping>
</web-app>
<!DOCTYPE html>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<body>
<form action="${pageContext.servletContext.contextPath}/upload.do" method="post" enctype="multipart/form-data">
    选择文件:<input type="file" name="file">
    <input type="submit" value="上传">
</form>
</body>
</html>
<!DOCTYPE html>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<body>
${msg}
</body>
</html>

四、运行效果

 

 


http://www.kler.cn/news/318417.html

相关文章:

  • Linux 清空redis缓存及查询key值
  • 电子计算机科学中的运维技术:概念(内涵和外延)、历史、现状与展望?
  • 【C++二叉树】105.从前序与中序遍历序列构造二叉树
  • OpenAi assistant run always fails when called from PHP
  • Go unique包:突破字符串局限的通用值Interning技术实现
  • 【M-LOAM学习】
  • 【Java】关键字-abstract【主线学习笔记】
  • 《算法笔记》例题解析 第3章入门模拟--4日期处理(9题)2021-03-03
  • 直流负载箱的主要功能有哪些?
  • 数字化转型中的供应链管理优化
  • 鸭脖变“刺客”,啃不起了
  • Leetcode算法基础篇-贪心算法
  • 输入5个数,求中值,verilog实现
  • more、less 命令:阅读文本
  • 电商效果图渲染神器:轻松高效出图
  • [docker][软件]docker快速安装rabbitmq
  • 【Rust语言】std::collections::HashMap用法
  • Linux环境下安装部署MySQL8.0以上(内置保姆级教程) C语言
  • Oracle数据库expdp与impdp
  • 基于SpringBoot+Vue+MySQL的网上租赁系统
  • CVPR最牛图像评价算法!
  • webview2加载本地页面
  • 「JavaScript深入」一文吃透JS的基本数据类型 Symbol
  • 统信服务器操作系统【Cron定时任务服务】
  • 安装程序不用鼠标,Windows也玩程序包管理存储库
  • 敏感词过滤
  • uni-app 多环境配置
  • 项目实战 (15)--- 代码区块重构及相关技术落地
  • 8月份,AI图像生成领域web端产品排行榜及产品是做什么的
  • UniApp一句话经验: px -> rpx动态转换和动态元素区域的获取