栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC文件上传和下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC文件上传和下载

一:文件上传

文件上传是项目开发中最常见的功能之一 ,springMVC 可以很好的支持文件上传,但是SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况下其不能处理文件上传工作。如果想使用Spring的文件上传功能,则需要在上下文中配置MultipartResolver。

第一步:

创建需要的manve项目

配置springmvc.xml中 的文件上传

 
    
        
        
        
        
        
    

第二部:

书写首页面,用它进行文件的上传

upload.jsp

<%--
  Created by IntelliJ IDEA.
  User: ZS
  Date: 2021/11/17
  Time: 15:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


第三部:

控制层Controller

@Controller
@RequestMapping("/upload")
public class uploadController {
    @RequestMapping("upload")
    
    @ResponseBody
    public String upload(MultipartFile uploadgo, HttpServletRequest request){
        String originalFilename = uploadgo.getOriginalFilename();//获取原始文件名
        System.out.println(originalFilename);
        //存储的内部路径
        String upload = request.getServletContext().getRealPath("upload");//存储文件名路径

        System.out.println(upload);
        //与目的地建立连接
        File file = new File(upload);
        if (!file.exists()) {//判断是否存在,不存在创建
            file.mkdirs();
        }
        //获取文件名后缀名
        String suffix = originalFilename.substring(originalFilename.indexOf("."));
        //构建文件具体上传路径
        String filename = upload.concat(File.separator).concat(UUID.randomUUID().toString()).concat(suffix);
        System.out.println(filename);
        try {
            uploadgo.transferTo(new File(上传按钮));
            return filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

这是Springmvc的文件上传,文件上传上传的属于idea的内部路径,运行就可以把文件上传到idea内部储存

二:文件下载

文件的下载与文件的上传其实并没与多大变化,唯一变化的就是Controller层

 文件下载的Controller使用储存的路径是内部,而文件的下载路径是自己创建的文件,给的是具体路径

控制层Controller

package cn.hp.ssm.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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Controller
@RequestMapping("/upload")
public class uploadController {
    @RequestMapping("upload")
    
    @ResponseBody
    public String upload(MultipartFile uploadgo, HttpServletRequest request){
        String originalFilename = uploadgo.getOriginalFilename();//获取原始文件名
        System.out.println(originalFilename);
        //文件下载的具体路径
        String upload="D:\upload";
        //内部路径
//        String upload = request.getServletContext().getRealPath("upload");//存储文件名路径

        System.out.println(upload);
        //与目的地建立连接
        File file = new File(upload);
        if (!file.exists()) {//判断是否存在,不存在创建
            file.mkdirs();
        }
        //获取文件名后缀名
        String suffix = originalFilename.substring(originalFilename.indexOf("."));
        //构建文件具体上传路径
        String filename = upload.concat(File.separator).concat(UUID.randomUUID().toString()).concat(suffix);
        System.out.println(filename);
        try {
            uploadgo.transferTo(new File(filename));
            return filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResponseEntity download(HttpServletRequest request,String filename){
        //文件地址在项目内部的目的地地址信息
//        String upload = request.getServletContext().getRealPath("upload");
        //目的仓库在项目之外
        String upload="D:\upload";
        //先拼接具体位置的位置路径,然后通过file进行建立连接
        File file = new File(upload+File.separator+filename);
        if (!file.exists()){//不存在文件,直接返回null
            return null;
        }
        //创建标头文件对象
        HttpHeaders headers = new HttpHeaders();
        //解决乱码问题
        try {
            String downloadfile = new String(filename.getBytes(StandardCharsets.UTF_8),"iso-8859-1");
            //以下载的方式打开文件
            headers.setContentDispositionFormData("attachment",filename);
            //二进制流
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
}


}

这就是我所理解的上传和下载,希望对你们有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530967.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号