Spring-MVC-文件上传下载

依赖

xml 复制代码
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

xml配置

xml 复制代码
<!--文件上传配置-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--请求的编码格式,必须和jsp的pageEncoding属性一致-->
    <property name="defaultEncoding" value="utf-8"/>
    <!--上传文件最大字节数,10M-->
    <property name="maxUploadSize" value="10485760"/>
    <property name="maxInMemorySize" value="40960"/>
</bean>

controller

java 复制代码
@RestController
public class FileController {
    // 文件上传,从请求中读取文件路径
    @RequestMapping("/upload")
    // @RequestParam("file")会将name=file的控件上传的文件封装为CommonsMultipartFile对象,数组实现批量上传
    public String uploadFile(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();
        if ("".equals(filename)){
            return "redirect:/index.jsp";
        }
        // 上传路径保存设置
        String realPath = request.getServletContext().getRealPath("/upload");
        File realFile = new File(realPath);
        if (!realFile.exists()){
            realFile.mkdir();
        }
        // out/artifacts/SSM_war_exploded/uploadFile
        System.out.println(realFile);
        // 文件读写
        file.transferTo(new File(realFile + "/" + filename));
        return "redirect:/index.jsp";
    }
    // 文件下载,写入响应输出流
    @RequestMapping("/downLoad")
    public String downLoadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 要下载的文件地址
        String realPath = request.getServletContext().getRealPath("/uploadFile");
        String fileName = "test.txt";
        // 设置响应头
        response.reset();// 清空缓存
        response.setCharacterEncoding("UTF-8");// 设置字符编码
        response.setContentType("multipart/form-data");// 二进制传输数据
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));
        // 文件读写
        InputStream in = new FileInputStream(new File(realPath,fileName));
        OutputStream out = response.getOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len=in.read(buffer)) != -1){
            out.write(buffer,0,len);
            out.flush();
        }
        out.close();
        in.close();
        return "ok";
    }
}
相关推荐
面朝大海,春不暖,花不开19 小时前
Spring Boot MVC自动配置与Web应用开发详解
前端·spring boot·mvc
残*影3 天前
什么是MVC?
java·spring·mvc
保持学习ing4 天前
黑马Java面试笔记之框架篇(Spring、SpringMvc、Springboot)
java·笔记·spring·面试·mvc·mybatis·springboot
超级无敌永恒暴龙战士4 天前
SpringBoot-配置Spring MVC
spring boot·spring·mvc
二进制小甜豆4 天前
Spring MVC
java·后端·spring·mvc
Rocky4014 天前
SpringMVC的注解
spring boot·后端·mvc
杨-羊羊羊4 天前
MVVM、MVC的区别、什么是MVVM
mvc
添砖java_8577 天前
Spring MVC 框架
java·spring·mvc
代码老y7 天前
SpringMVC核心原理与前后端数据交互机制详解
java·mvc·交互
劲爽小猴头7 天前
企业级Spring MVC高级主题与实用技术讲解
java·spring boot·后端·spring·mvc