springmvc上传和下载的简单实现

新建springmvc项目

需要使用的jar包有

配置springmvc.xml和web.xml文件

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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--开启自动扫描包-->
    <context:component-scan base-package="org.zmt.*"></context:component-scan>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <!--注入视图解析器-->
    <bean id="internalResourceViewResolver" 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="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="102400000000"></property>
    </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>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--<load-on-startup>1</load-on-startup>-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--过滤器-->
    <filter>
        <filter-name>fileencoding</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>fileencoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 在web下新建文件夹download和upload

 前端页面index.jsp

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

  <p><a href="download/20220301.rar">下载1</a></p>
  <p><a href="download/fj.png">下载2</a></p>
  <p><a href="download?fileName=fj.png">下载3</a></p>
  </body>
</html>

success.jsp中的内容 

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

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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(MultipartFile[] file, HttpServletRequest request) throws IOException {

        for (int i=0;i< file.length;i++) {
            //1.获得上传的路径
            String path = request.getServletContext().getRealPath("/upload/");
            //2.拿到从页面传过来的文件
            String name = file[i].getOriginalFilename();
            //3.对上传的文件改名
            String newName = new Date().getTime() + new Random(9999999).nextInt() + name;
            //4.文件上传路径
            File f = new File(path + newName);
            //5.上传
            file[i].transferTo(f);
        }
        return "success";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> downloads(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException {
        //1.下载路径
        String path = request.getServletContext().getRealPath("/download/");
        //2.下载的完整路径
        File f = new File(path+fileName);
        //3.转格式
        String name = new String(fileName.getBytes("utf-8"),"iso8859-1");
        //4.头部信息对象
        HttpHeaders hh = new HttpHeaders();
        hh.setContentDispositionFormData("attachment",name);
        hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);
    }
}

配置服务器测试项目

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月与清酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值