一、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>