美文网首页
番外-上传下载

番外-上传下载

作者: 通灵路耳 | 来源:发表于2020-08-11 16:48 被阅读0次

    SpringBoot上传下载

    1、启动类配置
    //排除原有的Multipart配置
    @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
    2、上传大小配置类
    package com.mf.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.multipart.MultipartResolver;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    
    
    @Configuration
    public class UploadConfig {
        //显示声明CommonsMultipartResolver为mutipartResolver
        @Bean(name = "multipartResolver")
        public MultipartResolver multipartResolver() {
            CommonsMultipartResolver resolver = new CommonsMultipartResolver();
            resolver.setDefaultEncoding("UTF-8");
            //resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
            resolver.setResolveLazily(true);
            resolver.setMaxInMemorySize(40960);
            //上传文件大小 5M 5*1024*1024
            resolver.setMaxUploadSize(5 * 1024 * 1024);
            return resolver;
        }
    
    }
    
    3、前端
    
            <form action="/upload/test" method="post" enctype="multipart/form-data">
                <input type="file" name="fileTest"/>
                <input type="submit" value="上传"/>
            </form>
    
    4、controller
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import java.io.File;
    import java.io.IOException;
    /**
     * 作业
     */
    @Controller
    @RequestMapping("/upload")
    public class ZuoyeController {
        private static final Logger LOGGER = LoggerFactory.getLogger(ZuoyeController.class);
    
        @GetMapping("/upload")
        public String upload() {
            return "upload";
        }
    
        @PostMapping("/test")
        @ResponseBody
        public String upload(@RequestParam("fileTest") MultipartFile fileTest) {
            String fileName = fileTest.getOriginalFilename();
            System.out.println(fileName);
            String filePath = "d:\\upload\\";
            File dest = new File(filePath + fileName);
            try {
                fileTest.transferTo(dest);
                LOGGER.info("上传成功");
                return "上传成功";
            } catch (IOException e) {
                LOGGER.error(e.toString(), e);
            }
            return "上传失败!";
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:番外-上传下载

          本文链接:https://www.haomeiwen.com/subject/tueydktx.html