关于spring:SpringMVC文件的上传与下载

5次阅读

共计 5901 个字符,预计需要花费 15 分钟才能阅读完成。

一、后期筹备

开发环境

  • JDK:1.8
  • Tomcat:9.0.3
  • 编译器:IntelliJ IDEA 2019
  • maven:3.6.3
  • spring:5.2.8
  • 留神:肯定须要 commons-fileupload 和 commons-io 包,在 maven 中我曾经配置了,如果不应用 Maven 的能够到 maven repository 下来下载

pom.xml 配置

<properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <maven.compiler.source>1.7</maven.compiler.source>
     <maven.compiler.target>1.7</maven.compiler.target>
     <spring.version>5.2.8.RELEASE</spring.version>
     <slf4j.version>1.6.6</slf4j.version>
     <log4j.version>2.13.3</log4j.version>
</properties>
<dependencies> 
 <!-- spring -->
 <!--    beans--> 
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <!--    core-->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>${spring.version}</version>
 </dependency> <!-- aop 相干的技术 -->
 <dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
     <version>1.6.8</version>
 </dependency>
 <!-- aop -->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aop</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <!-- context 容器 -->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <dependency> 
     <groupId>org.springframework</groupId>
     <artifactId>spring-context-support</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <!-- web -->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <!-- webmvc -->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>${spring.version}</version>
 </dependency>
 <!-- spring 测试 -->
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.version}</version>
 </dependency>
<!-- 文件的上传与下载 -->
 <dependency>
     <groupId>commons-fileupload</groupId>
     <artifactId>commons-fileupload</artifactId>
     <version>1.4</version>
 </dependency>
 <dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.6</version>
 </dependency>
 <dependency> 
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     <version>1.2</version>
 </dependency>
</dependencies>
 <build> 
    <finalName>TestSSM</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.1.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.0.2</version>
             </plugin> 
             <plugin> 
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.8.0</version>
             </plugin> 
             <plugin> 
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.22.1</version>
             </plugin> 
             <plugin> 
                 <artifactId>maven-war-plugin</artifactId>
                 <version>3.2.2</version>
             </plugin> 
             <plugin> 
                 <artifactId>maven-install-plugin</artifactId>
                 <version>2.5.2</version>
             </plugin> 
             <plugin> 
                 <artifactId>maven-deploy-plugin</artifactId>
                 <version>2.8.2</version>
             </plugin> 
         </plugins> 
     </pluginManagement> 
 </build>

二、上传与下载

文件的上传

  • 须要在 spring 中配置一个文件解析器
<?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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
    <!-- 开启 spring 注解驱动 -->
     <context:component-scan base-package="com.cjh"/>
    <!-- 开启 mvc 注解驱动 -->
     <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置文件解析器 -->
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 配置上传文件的大小 -->
        <property name="maxUploadSize" value="20480000"/>
     </bean>
</beans>
  • JSP:
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>cai jin hong</title>
</head>
<body>
 <form action="upload.do" method="post" enctype="multipart/form-data">
 <input type="text" name="text" value="">
 <input type="file" name="upload" value="">
 <input type="submit" value="submit">
 </form></body>
</html>
  • 具体 Controller 类:
@Controller
public class UserController {
     // 文件的上传
     /**
     * @param text 类型为 text 的 input 标签输入框内容,与 input 标签的 name 属性的名字统一
     * @param upload 文件对象,与类型为 file 的 input 标签中的 name 属性的名字统一
     * @return // 跳转到一个新的页面
     * @throws IOException
     */ @RequestMapping("upload.do")
     public String upload(String text, MultipartFile upload) throws IOException {System.out.println(text);
         // 获取文件名字
         String fileName = upload.getOriginalFilename();
         // 传输到指定地位
         upload.transferTo(new File("D://test//", fileName));
         return "welcome.jsp";
    }
}

文件的下载

  • JSP:
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>cai jin hong</title>
</head>
<body>
 <a href="download.do?fileName= 订单列表.xls"> 文件下载 </a>
</body>
</html>
  • 具体的 Controller 类:
@Controller
public class UserController {
    // 文件的下载
 @RequestMapping("download.do")
    public ResponseEntity<byte[]> upload(String fileName) throws IOException {System.out.println("fileName:" + fileName);
         // 获取文件对象
         File file = new File("D://test//" + fileName);
         // 解决中文字符集
        //        fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
         // 设置响应头信息
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
         headers.setContentDispositionFormData("attachment", fileName);
         byte[] b = FileUtils.readFileToByteArray(file);
         return new ResponseEntity(b, headers, HttpStatus.CREATED);
         }
}
正文完
 0