SpringMVC中实现文件下载和上传

相较servlet阶段,springmvc实现文件上传和下载更任意一些

依赖

配置springmvc文件上传解析器 类容是固定的

 File上传下载简单实现页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文件上传和修改</title>
</head>
<body>
<div>
    <div>
        <img th:src="@{/static/img/mm.jpg}" border="1px"
         width="50%px" height="200px"
        >
    </div>
    欢迎下载:
    <span> <a th:href="@{/filedown}">图片下载</a></span></p>
</div>
<div>
    <div>
        <img th:src="@{/static/img/mm.jpg}" border="1px"
             width="50%px" height="200px"
        >
    </div>
    文件上传:
    <span> <form  th:action="@{/fileup}" method="post" enctype="multipart/form-data">
          头像:<input type="file" name="photo">
          <input type="submit" value="文件上传">
    </form>
    </span></p>
</div>

</body>
</html>

文件上传下载控制器(这里代码块也相较固定,没有太大的更改性)

package com.atchengdu.contrller;

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.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 FileupandowmController {

    @RequestMapping("/filedown")
    public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws
            IOException {
          //获取ServletContext对象
        ServletContext servletContext = session.getServletContext();
        //获取服务器中文件的真实路径
        String realPath = servletContext.getRealPath("/static/img/mm.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=mm.jpg");
        //设置响应状态码
        HttpStatus statusCode = HttpStatus.OK;
         //创建ResponseEntity对象
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers,
                statusCode);
         //关闭输入流
        is.close();
        return responseEntity;
    }

    @RequestMapping("/fileup")
    //获取对应的图片信息  但是它会出现同名文件覆盖
    public  String testfileup(MultipartFile photo,HttpSession session) throws IOException {
        //photo.getOriginalFilename() 获取文件名称
        String filename = photo.getOriginalFilename();
        //通过处理文件名来解决文件名重复
        String suffixname=filename.substring(filename.lastIndexOf("."));//文件的最后一个.开始
        String uuid= UUID.randomUUID().toString();
        filename=uuid+suffixname;
        //获取文件上传路径
        ServletContext context = session.getServletContext();
        String realPath = context.getRealPath("photo");
        File file=new File(realPath);
        if(!file.exists()){
            file.mkdir();
        }
        String finalpath=realPath+File.separator+filename;
        //上传的方法 参数是需要存放的位置
        photo.transferTo(new File(finalpath));
        return "file";
    }
}

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值