SpringMVC文件下载与上传

4 篇文章 0 订阅
3 篇文章 0 订阅

SpringMVC文件下载与上传

1、文件下载

(1)服务器控制器

package com.ssm.controller;

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 javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.*;

/**
 * @Author: 阿芙乐尔
 * @Description: TODO
 * @DateTime: 2022/10/12 9:09
 **/
@Controller
public class FileUpAndDowController {
    @RequestMapping("/test/down")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
        //获取ServletContext对象
        ServletContext servletContext = session.getServletContext();
        //获取服务器中文件的真实路径
        String realPath = servletContext.getRealPath("image/");

        //文件具体路径(文件分隔符)
        realPath  = realPath+ File.separator+"ddp.jpg";

        //创建输入流
        InputStream is = new FileInputStream(realPath);
        //创建字节数组
        byte[] bytes = new byte[is.available()];
        //将流读到字节数组中
        is.read(bytes);
        //创建HttpHeaders对象设置响应头信息
        MultiValueMap<String, String> headers = new HttpHeaders();
        //设置要下载方式以及下载文件的名字
        headers.add("Content-Disposition", "attachment;filename=ddp1.jpg");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
        //创建ResponseEntity对象
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers,
                statusCode);
        //关闭输入流
        is.close();
        return responseEntity;
    }
}

(2)客户端连接


<a th:href="@{/test/down}">下载图片</a>

2、文件上传

(1)客户端链接

<form th:action="@{/test/up}" method="post" enctype="multipart/form-data">
        头像:<input type="file" name="photo"><br>
        <input type="submit" value="上传">
    </form>

(2)服务器控制器

@RequestMapping("/test/up")
    public String testUp(MultipartFile photo, HttpSession session) throws IOException {
        //获取上传的文件名
        String fileName = photo.getOriginalFilename();

        /*避免文件重名
        * 1、取文件名后缀名
        * 2、获取uuid
        * 3、拼接新的文件名
        * */
        String substring = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        fileName = uuid+substring;

        //ServletContext对象
        ServletContext servletContext = session.getServletContext();

        //获取当前项目的静态资源真实路径
        String photoPath = servletContext.getRealPath("image");

        //创建photo所对应的File对象
        File file = new File(photoPath);

        //判断file所对应的路径是否存在
        if (file.exists()) {
            file.mkdir();
        }
        String finaPath = photoPath + File.separator + fileName;

        //上传文件
        photo.transferTo(new File(finaPath));
        return "success";
    }

(3)SpringMVC配置文件

<!--  配置文件上传解析器  -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--  设置文件上传的编码格式-->
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

(4)pom.xml文件配置

<!--   https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值