springMvc之文件上传与下载

文件上传

项目结构
在这里插入图片描述
1.配置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>app</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>
        <multipart-config/>
    </servlet>
    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 编码问题之配置过滤器 -->
    <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>

2.新建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"
       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">


    <context:component-scan base-package="com.blb.controller"/>

    <!--视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

3.文件上传的controller

@Controller
public class UploadController {

    @RequestMapping("")
    public String upload() {
        return "download";
    }

    //    文件上传
    @PostMapping("show")
    public String show(MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            //        文件存放的路径
            /**
             * getOriginalFilename 获取文件的名字
             */
            File file1 = new File("D:\\fileUpload\\" + file.getOriginalFilename());

            if (!file1.getParentFile().exists()) {
                file1.getParentFile().mkdir();
            }
            file.transferTo(file1);
        }
        return "upload";

    }

4.文件上传的upload.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>

<form action="show" method="post" enctype="multipart/form-data">
    <input type="file" name="files" multiple="multiple">
    <input type="submit">
</form>
</body>
</html>

示例图片
在这里插入图片描述
在这里插入图片描述
5.文件的下载
(1)controller代码

//    文件的下载
    @RequestMapping("download/{file}")
    public ResponseEntity download(@PathVariable String file) throws IOException {
        File file1 = new File("D:\\fileUpload\\" + file);
        System.out.println(file1);
//        new 一个头部信息
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);  //告诉浏览器这是一个下载流
        return new ResponseEntity(FileCopyUtils.copyToByteArray(file1), httpHeaders, HttpStatus.OK);

    }

6.演示图片
在这里插入图片描述
7.在页面输出

  @RequestMapping("shows")
    public String shows(Model model){

        List list = new ArrayList();

        File file = new File("D:\\fileUpload\\");
//        在本地文件夹搜索文件
        File[] files = file.listFiles();
        for (File file1 : files) {
            list.add(file1.getName());

        }
        model.addAttribute("list",list);
        return "upload";

    }

8.jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@page isELIgnored="false" %>--%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
    <title>文件上传</title>
</head>
<body>
<h1>登录成功!!</h1>
<form action="show" method="post" enctype="multipart/form-data">
    <input type="file" name="files" multiple="multiple">
    <input type="submit">
</form>

<a href="shows">查看文件</a><br>
<c:forEach var="list" items="${list}">
    <a href="download/${list}">${list}</a><br>
</c:forEach>



</body>
</html>

9.在maven项目中引用EL表达式的依赖

<!--      el表达式的依赖-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值