SpringMVC 实现文件的上传和下载

SpringMVC 实现文件的上传和下载

文件上传

SpringMVC 的文件上传是基于commons-fileupload组件的文件上传。SpringMVC在原有文件上传组件做了进一步封装,简化了文件上传的代码实现。

mvaen添加依赖

  <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

基于表单的文件上传

​ upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/upload">
    <p>上传文件<input type="file" name="file"></p>
    <p><input type="submit" value="upload"></p>
</form>
</body>
</html>

要注意的是,基于表单的文件上传,需要设置enctype属性,并将它的值设置为multipart/form-data,同时将表单设置为post提交方式。
表单的enctype属性指定的是表单数据的编码方式,该属性有3个值:

  • application/x-www-form-urlencoded :默认编码方式,只处理表单域的value属性值。
  • multipart/form-data :该编码方式以二进制流的方式处理表单数据,并将文件域指定文件的内容封装到请求参数里。
  • text/plain :该编码方式只有在action属性值为mailto:URL的形式时才使用,主要适用于直接通过表单发送邮件的方式。

MultipartFile 接口

在SpringMVC中,上传文件时将文件的相关信息以及操作封装到了MultipartFile对象中,因此,只需要使用MultipartFile类型声明模型类的一个属性即可对被上传文件进行操作。

MultipartFile接口的相关方法如下:

  • byte[] getBytes() :以字节数组的形式返回文件内容。
  • String getContentType() :返回文件内容类型。
  • InputStream getInputStream() :返回一个InputStream,从中读取文件内容。
  • String getName() :返回请求参数的名称。
  • String getOriginalFilename() :返回上传文件的文件名。
  • long getSize() :返回文件的大小,单位为字节。
  • boolean isEmpty() :判断被上传文件是否为空。
  • void transferTo(File destination) :将上传文件保存到目标目录下。

配置

在上传文件时需要在配置文件中对org.springframework.web.multipart.commons.CommonsMultipartResolver类进行相关配置。

<!-- 使用Spring的commonsMultipartResovler配置MultipartResovler用于文件上传  -->
<bean  id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!--- 设置请求的编码格式, 默认为iso-8859-1 -->        
        <!-- 设置允许上传文件的最大值, 单位为 字节 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000"></property>
        <!-- 设置上传文件的临时路径 
		<property name="uploadTempDir" value="upload/temp"/>-->
    </bean>

实例

springMVC.xml配置

<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--   视图解析器-->
    <context:component-scan base-package="com.syf"></context:component-scan>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        文件前缀-->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <!--        文件后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <mvc:view-controller path="/up" view-name="upload"></mvc:view-controller>
    <mvc:view-controller path="/do" view-name="download"></mvc:view-controller>
    <!-- 使用Spring的commonsMultipartResovler配置MultipartResovler用于文件上传  -->
<bean  id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!--- 设置请求的编码格式, 默认为iso-8859-1 -->        
        <!-- 设置允许上传文件的最大值, 单位为 字节 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000"></property>
        <!-- 设置上传文件的临时路径 
		<property name="uploadTempDir" value="upload/temp"/>-->
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <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:springMVC.xml</param-value>
    </init-param>
    <!-- 表示容器再启动时立即加载servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <filter>
    <filter-name>CharacterEncoding</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>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

控制类UADConteoller.java

 //文件上传
    @PostMapping("upload")
    public String upload(MultipartFile file, HttpSession session)throws Exception{
        // 获取文件上传到具体文件夹的绝对路径D:\JAVA\workspace\Spring\Uploadanddownload\target\Uploadanddownload-1.0-SNAPSHOT\file
        String path=session.getServletContext().getRealPath("/file");
        // 判断指定文件夹uploadfiles是否存在,不存在就创建
        File fold=new File(path);
        if (!fold.exists()){
            fold.mkdirs();
        }
        // 获取上传的文件名
        String filename=file.getOriginalFilename();
        String filepath=fold+"/"+filename;
        // 上传文件
        file.transferTo(new File(filepath));
        System.out.println(filepath);
        return "redirect:/filelist";
    }
//显示上传文件
    @GetMapping("/filelist")
    public String filelast(HttpSession session, Map<String,Object> map){
        // 获取文件上传到具体文件夹的绝对路径D:\JAVA\workspace\Spring\Uploadanddownload\target\Uploadanddownload-1.0-SNAPSHOT\file
        String foldpath=session.getServletContext().getRealPath("/file");
        File fold=new File((foldpath));
        File[] fs=fold.listFiles();
        map.put("files",fs);
        return "filelist";
    }

上传页面upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
    <!--`multipart/form-data` :该编码方式以二进制流的方式处理表单数据,并将文件域指定文件的内容封装到请求参数里-->
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/upload">
    <p>上传文件<input type="file" name="file"></p>
    <p><input type="submit" value="upload"></p>
</form>
</body>
</html>

显示上传文件页面filelist.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:forEach items="${files}" var="f">
    <p><a href="${pageContext.request.contextPath}/down/${f.name}">${f.name}</a> </p>
</c:forEach>
</body>
</html>

上传

在这里插入图片描述
结果
在这里插入图片描述

文件下载

文件下载的实现有两种方式,一种是通过超链接实现下载,另一种是通过程序编码方式实现下载。前者虽然实现简单,但暴露了文件的真实位置,而且文件需要放在web程序的目录下才能下载。后者能够增加安全访问控制,还可以从任意位置提供下载数据,可以将文件存放web程序目录以外,也可以将文件存放在数据库中。

通过程序编码实现下载需要设置两个响应报头:

  • Content-Type :告诉浏览器其输出的内容不是普通文本文件或者HTML文件,而是一个保存到本地的文件,需要设置该报头的值为application/x-msdownload。在不知道文件的mime类型时,也可以设置值为application/octet-stream,该属性值表示任意的字节流。
  • Content-Disposition :告诉浏览器不直接处理相应实体内容,由用户选择将相应的实体内容保存到一个文件中,需要设置值为attachment;filename=xxx,指定接收程序处理数据内容的方式。在HTTP应用中,attachment是标准的方式,表示要求用户干预。attachment后面指定的filename参数表示服务器建议浏览器将内容保存到文件的文件名称(对于中文名称,需要通过编码转换,否则会出现乱码)。

下载代码从之前上传文件的文件夹下载文件

//下载
    @GetMapping("down/{filename}")
    public ResponseEntity<byte[]> download(@PathVariable() String filename, HttpSession session)throws Exception{
        // 获取文件上传到具体文件夹的绝对路径D:\JAVA\workspace\Spring\Uploadanddownload\target\Uploadanddownload-1.0-SNAPSHOT\file
        String foldpath =   session.getServletContext().getRealPath("/file");
        // 获取文件输入流
        InputStream inputStream = new FileInputStream(new File(foldpath+"/"+filename));
        byte[] bs=new byte[inputStream.available()];
        inputStream.read(bs);
        MultiValueMap<String,String> headers=new HttpHeaders();
        // 设置下载文件时的响应报头
        headers.add("Content-Disposition","attachment; filename="+new String(filename.getBytes("UTF-8"),"ISO-8859-1"));
        //ResponseEntity可以定义返回的HttpStatus(状态码)和HttpHeaders
        ResponseEntity<byte[]> entity=new ResponseEntity<byte[]>(bs,headers, HttpStatus.OK);
        return entity;
    }

结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值