一聚教程网:一个值得你收藏的教程网站

热门教程

SpringMVC实现文件上传下载的全过程代码示例

时间:2022-06-29 01:59:02 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下SpringMVC实现文件上传下载的全过程代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一、通用配置

pom.xml

    
        
        
            org.springframework
            spring-webmvc
            5.3.13
        

        
        
            ch.qos.logback
            logback-classic
            1.2.3
        

        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        

        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        


        
        
            org.springframework
            spring-context
            5.3.11
        

        
            junit
            junit
            4.12
            test
        
        

        
            org.springframework
            spring-test
            5.3.13
        
        
        
        
            mysql
            mysql-connector-java
            8.0.24
        
        
        
        
            com.alibaba
            druid
            1.2.8
        
        
        
        
            org.springframework
            spring-context
            5.3.11
        
        

        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        

        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.3
        
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.1
        
        
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
    

上传功能的关键jar

web.xml



    
    
    
    
        字符集过滤器
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            字符集编码
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    

    
    
        characterEncodingFilter
        /*
    


    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    
    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    

    
        DispatcherServlet
        /
    

springMVC.xml




    
    

    
    
        
        
        

            
                
                    

                        
                        

                        
                        
                        
                        
                    
                
            
        
    

    
    
        
        
            
                text/html
                application/json
            
        
    

    
    
    
    

    
    

实现页面跳转,vue文件解析,上传内容解析的关键

file.html内容demo


下载1.jpg
头像:

二、实现文件下载,上传功能

package com.vector.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Controller
public class FileUpAndDownController {

    @RequestMapping("/testDown")
    public ResponseEntity testResponseEntity(HttpSession session) throws IOException {
        //获取ServletContext对象
        ServletContext servletContext = session.getServletContext();
        //获取服务器中文件的真实路径
        String realPath = servletContext.getRealPath("/static/img/1.jpg");
        //创建输入流
        InputStream is = new FileInputStream(realPath);
        //创建字节数组
        byte[] bytes = new byte[is.available()];
        //将流读到字节数组中
        is.read(bytes);
        //创建HttpHeaders对象设置响应头信息
        MultiValueMap headers = new HttpHeaders();
        //设置要下载方式以及下载文件的名字
        //Content-Disposition 固定回复内容格式  attachment以附件形式下载  filename=1.jpg文件名
        //可以将filename拼接为动态命名
        headers.add("Content-Disposition", "attachment;filename=1.jpg");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
        //创建ResponseEntity对象
        ResponseEntity responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
        //关闭输入流
        is.close();
        return responseEntity;
    }
    @RequestMapping("/testUp")
    public String testUp(@Value("multipartResolver") MultipartFile photo,HttpSession session) throws IOException {
        //获取上传的文件的文件名
        String fileName = photo.getOriginalFilename();
        //处理文件重名问题
        //重名问题是java.io中写入同一文件默认覆盖原文件内容导致图片被覆盖.
        //获取文件名后缀
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //将UUID作为文件名  uuid是32位随机数,几乎不可能会重复
        fileName = UUID.randomUUID().toString() + suffixName;
        //获取服务器中photo目录的路径
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("photo");
        File file = new File(photoPath);
        //判断服务器是否存在该路径
        if(!file.exists()){
            file.mkdir();
        }
        String finalPath = photoPath + File.separator + fileName;
        //实现上传功能
        photo.transferTo(new File(finalPath));
        return "success";
    }
}

下载功能测试

上传功能测试

热门栏目