SSM 集成 commons-fileupload 单多文件上传

一、pom.xml

二、springmvc-config.xml

三、FileController.java

四、index.jsp


一、pom.xml

<!--引用上传与下载依赖-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

二、spring-mvc.xml

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>

三、FileController.java

package cn.kaxlm6.mybatis.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
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;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 *
 * @author xlm
 * description:
 * path: mybatisProject-cn.kaxlm6.mybatis.controller-FileController
 * date: 2018/10/19 16:48
 * version: 02.06
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class FileController {

    Logger logger = LogManager.getLogger();

    @Resource
    ServletContext context;

    @RequestMapping(value = "/fileUpload")
    public void fileUpload(@RequestParam("uploadFile") MultipartFile[] file, HttpServletRequest request) throws Exception {

        for (int i = 0; i < file.length; i++) {

            //判断文件是否为空
            if (!file[i].isEmpty()) {

                //获得原文件名
                String fileName = file[i].getOriginalFilename();
                //File.separator表示在 UNIX 系统上,此字段的值为 /;在 Windows 系统上,它为 \,如:C:\tmp\test.txt和tmp/test.txt
                String filePath = context.getRealPath("") + "upload" + File.separator;

                //获得当前日期
                Calendar ca = Calendar.getInstance();
                //拼接日期文件夹
                filePath += ("" + ca.get(Calendar.YEAR) + (ca.get(Calendar.MONTH) + 1) + ca.get(Calendar.DATE));
                File dateDir = new File(filePath);

                //判断当前日期文件夹是否存在,不存在创建
                if (!dateDir.exists()) {
                    dateDir.mkdirs();
                }

                //文件名由客户端IP地址+系统当前毫秒数组成
                filePath += File.separator + request.getRemoteAddr().replace(":", "") + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

                // 复制本地文件到服务器
                FileCopyUtils.copy(file[i].getBytes(), new File(filePath));

            } else {

                logger.error("文件上传异常");

            }

        }

    }

}

四、index.jsp

<%--
  Created by IntelliJ IDEA.
  User: xlm
  Date: 2018/9/29
  Time: 9:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>

<html>
<head>
    <title>Title</title>
</head>

<body>

    <!-- enctype="multipart/form-data" 以二进制提交 -->
    <form action="/fileUpload.xlm" method="post" enctype="multipart/form-data">
        <!-- multiple="multiple" 设置可以同时选择多个文件 -->
        <input type="file" name="uploadFile" id="uploadFile" multiple="multiple" />
        <input type="submit" value="提交">
    </form>

</body>

</html>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值