第一.jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
第二步.web.xml和主配置文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--springmvc的过滤器 防止中文乱码-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?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" 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">
<!--1.配置注解扫描位置-->
<context:component-scan base-package="com.hw.controller"/>
<!--2.配置处理器映射,通过注解查找-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!--3.配置适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</bean>
<!--4.配置资源视图解析-->
<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="17367648787"></property>
<!--上传文件的编码-->
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
准备作为测试用的jsp页面
<%--
Created by IntelliJ IDEA.
Date: 2019/1/4
Time: 0:22
To change this template use File | Settings | File Templates.
--%>
<%@ page pageEncoding="utf-8" isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="up.do" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>请选择文件:</td>
<td><input type="file" name="file"></td>
<td><input type="file" name="file"></td>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td>开始上传</td>
<td><input type="submit" value="上传"></td>
</tr>
</table>
</form>
<%--这里只是模拟下载所以用的死数据,项目中肯定是要在数据库中取值的--%>
<a href="/download.do?filename=IDEA.txt">
下载
</a>
</body>
</html>
然后就是controller了
package com.hw.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
/**
* @program: Maven
* @description:
* @author: hw
* @create: 2019-01-04 00:22
**/
@Controller
public class Upload {
@RequestMapping("/upload")
public String hello() {
System.out.println(1);
return "upload";
}
@RequestMapping("/up")
public String save(@RequestParam("file") MultipartFile [] files,
HttpServletRequest request) {
//可能为批量文件所以用数组接收
for (MultipartFile file : files) {
if (!file.isEmpty()) {
String contextPath = request.getContextPath();//"/SpringMvcFileUpload"
String servletPath = request.getServletPath();//"/up"
String scheme = request.getScheme();//"http"
//存放我们上传的文件路径
String storePath = "E:\\Project\\Maven\\SpringMVC3\\src\\main\\webapp\\upload";
//获得上传文件名
String fileName = file.getOriginalFilename();
File filepath = new File(storePath, fileName);
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();//如果目录不存在,创建目录
}
try {
//把文件写入目标文件地址
file.transferTo(new File(storePath + File.separator + fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 返回页面
return "index";
}
@RequestMapping("download")
public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename,
Model model) throws IOException {
String downloadFilePath="E:\\Project\\Maven\\SpringMVC3\\src\\main\\webapp\\upload";//从我们的上传文件夹中去取
File file = new File(downloadFilePath+File.separator+filename);//新建一个文件
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
}
}
点赞或者评论是我最大的动力,有问题欢迎留言或者联系q:1559810637