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";
    }
}
相关推荐
zzb158011 天前
ios基础-MVC-UIView
ios·mvc·cocoa
秋雨梧桐叶落莳13 天前
iOS——QQ音乐仿写项目总结
学习·macos·ui·ios·mvc·objective-c·xcode
mikasa66714 天前
关于Spring MVC 基于 AOP 实现的全局控制器统一处理方案@ControllerAdvice
java·spring·mvc
仍然.14 天前
Spring MVC(2)--- 介绍响应数据,具体案例和三层架构
mvc
仍然.15 天前
Spring MVC(1)---介绍Spring MVC 和 请求数据
java·spring·mvc
摇滚侠17 天前
Spring MVC 不是一个单独的框架,是 Spring 框架的一个模块
java·spring·mvc
我登哥MVP18 天前
Spring Boot 从“会用”到“精通”:SpringBoot MVC 请求处理全流程
java·spring boot·后端·spring·mvc·maven·intellij-idea
摇滚侠20 天前
JavaWeb 全套教程 MVC 模式 93
mvc
代码的小搬运工20 天前
【iOS】MVC架构
ios·架构·mvc
qq_25183645722 天前
基于MVC的学校食堂点餐管理系统的设计与实现
mvc