[刘阳Java]_SpringMVC文件上传第2季_第11讲

这篇文章重点介绍基于SpringMVC的多文件上传,代码实现难度不大。只是我们相对于SpringMVC文件上传第1季中加入异常处理的判断

1. 实现案例的截图

 

2. 文件上传功能需求

  • 可以选择多个文件上传
  • 如果没有选择文件就上传需要异常处理
  • 如果服务器上已经有相同的文件,需要异常处理

3. 功能实现步骤

  • 添加支持文件上传的解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.springmvc.controller"></context:component-scan>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5242880"></property>
        <property name="maxInMemorySize" value="4096"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
    
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Throwable">error</prop>
            </props>
        </property>
    </bean>
        
</beans>
  • 编写文件上传的控制器代码
package com.springmvc.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/upload")
public class FileUploadController {
    
    @RequestMapping(value="/multifileupload", method=RequestMethod.POST)
    public void upload(@RequestParam(value="myfiles") MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws IOException {
        long start = System.currentTimeMillis();
        String savePath = request.getServletContext().getRealPath("/") + "upload";
        savePath = savePath.replaceAll("\\", "/");
        File dirs = new File(savePath); //保存路径
        if (!dirs.exists()) {
            dirs.mkdirs();
        }
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        for (MultipartFile myfile : myfiles) { //遍历多个上传文件
            String originalFileName = myfile.getOriginalFilename(); //上传文件名称
            long fileSize = myfile.getSize(); //上传文件大小
            String fileContentType = myfile.getContentType(); //上传文件类型
            myfile.transferTo(new File(dirs, originalFileName)); //开始上传文件
            long end = System.currentTimeMillis();
            out.println("上传文件的名称 = " + originalFileName);
            out.println("<p>");
            out.println("保存文件的路径 = " + savePath);
            out.println("<p>");
            out.println("上传文件的大小 = " + fileSize);
            out.println("<p>");
            out.println("上传文件的类型 = " + fileContentType);
            out.println("<p>");
            out.println("上传成功的时间 = " + String.valueOf(end - start) + "ms");
            out.println("<p>");
            out.flush();
        }
        out.close();
    }
}
  • 编写文件上传的html代码,注意表单中需要加入enctype="multipart/form-data"
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload/multifileupload.do" method="post" enctype="multipart/form-data">
<fieldset>
    <legend>多个文件上传</legend>
    <input type="file" name="myfiles">
    <input type="file" name="myfiles">
    <input type="submit">
</fieldset>
</form>
</body>
</html>

 源码下载地址:https://pan.baidu.com/s/1eSDZwFg

原文地址:https://www.cnblogs.com/liuyangjava/p/6856652.html