• (续)SSM整合之springmvc笔记(文件上传和下载)(P159-163)


    一 .文件下载

    ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文

    使用 ResponseEntity 实现下载文件的功能

    1. 搞一张图片

     2. index.html

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

    3 .创建控制器FileUpAndDownController

    1. @RequestMapping("/test/down")
    2. public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
    3. //获取ServletContext对象
    4. ServletContext servletContext = session.getServletContext();
    5. //获取服务器中文件的真实路径
    6. String realPath = servletContext.getRealPath("img");
    7. realPath = realPath + File.separator + "1.jpg";
    8. //创建输入流
    9. InputStream is = new FileInputStream(realPath);
    10. //创建字节数组,is.available()获取输入流所对应文件的字节数
    11. byte[] bytes = new byte[is.available()];
    12. //将流读到字节数组中
    13. is.read(bytes);
    14. //创建HttpHeaders对象设置响应头信息
    15. MultiValueMap headers = new HttpHeaders();
    16. //设置要下载方式以及下载文件的名字
    17. headers.add("Content-Disposition", "attachment;filename=1.jpg");
    18. //设置响应状态码
    19. HttpStatus statusCode = HttpStatus.OK;
    20. //创建ResponseEntity对象
    21. ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
    22. //关闭输入流
    23. is.close();
    24. return responseEntity;
    25. }

    二 .文件上传

    1. index.html

    1. "@{/test/up}" method="post" enctype="multipart/form-data">
    2. 头像:"file" name="photo">
    3. "submit" value="上传">

    2 .创建控制器FileUpAndDownController

    1. @RequestMapping("/test/up")
    2. public String testUp(MultipartFile photo, HttpSession session) throws IOException {
    3. //获取上传的文件的文件名
    4. String fileName = photo.getOriginalFilename();
    5. //获取上传的文件的后缀名
    6. String hzName = fileName.substring(fileName.lastIndexOf("."));
    7. //获取uuid
    8. String uuid = UUID.randomUUID().toString();
    9. //拼接一个新的文件名
    10. fileName = uuid + hzName;
    11. //获取ServletContext对象
    12. ServletContext servletContext = session.getServletContext();
    13. //获取当前工程下photo目录的真实路径
    14. String photoPath = servletContext.getRealPath("photo");
    15. //创建photoPath所对应的File对象
    16. File file = new File(photoPath);
    17. //判断file所对应目录是否存在
    18. if(!file.exists()){
    19. file.mkdir();
    20. }
    21. String finalPath = photoPath + File.separator + fileName;
    22. //上传文件
    23. photo.transferTo(new File(finalPath));
    24. return "success";
    25. }

    3. spring.xml 里面配置文件上传解析器

    1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    4 .添加依赖

    
    
        commons-fileupload
        commons-fileupload
        1.3.1
    

    总结:

    * 文件上传的要求:
    * 1、form表单的请求方式必须为post
    * 2、form表单必须设置属性enctype="multipart/form-data"

  • 相关阅读:
    【CSS】笔记4-浮动
    [Display嵌入式]SDL播放 mjpeg 檔案到屏幕上
    【leetcode】【2022/8/27】662. 二叉树最大宽度
    linux免密
    【线性代数】行列式和矩阵
    跟我看 Microsoft Build 2023
    HTTP响应
    Hive、SparkSQL是如何决定写文件的数量的?
    数据结构之智能指针类
    前后端分离项目,vue+uni-app+php+mysql电影院售票系统设计与实现(H5移动项目)
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127957073