SpringBoot文件上传

1.引入web依赖

 
       org.springframework.boot
       spring-boot-starter-web

2.设置文件上传的相关参数,一般设置以下两项即可

#文件上传
#单个文件最大内存
spring.servlet.multipart.max-file-size=10MB
#上传请求最大内存
spring.servlet.multipart.max-request-size=10MB

注意:单位MB必须写对,不然会报以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.servlet.multipart.max-file-size' to org.springframework.util.unit.DataSize:

    Property: spring.servlet.multipart.max-file-size
    Value: 10M
    Origin: class path resource [application.properties]:12:40
    Reason: failed to convert java.lang.String to org.springframework.util.unit.DataSize

3.单文件上传

3.1前端页面

此处需要注意的是,Form表单中的method必须为post请求,并且加上enctype="multipart/form-data",以及inpunt中加上name="file"属性。



    
        
        文件上传
    
    
        

文件上传

请选择文件:

3.2.后台页面

@Controller
public class UploadController {
    //跳转到上传页面
    @RequestMapping("/toupload")
    public String toUpload(){
        return "upload";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam MultipartFile file){
            //判断文件是否为空
        if (file.isEmpty()){
            return "please choices a file";
        }
        Calendar calendar=Calendar.getInstance();
        String rootPath="E:\\upload";
        //创建年月文件夹
        File dateDirs=new File(calendar.get(Calendar.YEAR)
                +File.separator+(calendar.get(Calendar.MONTH)+1));
        //目标文件
        File descFile=new File(rootPath+File.separator+dateDirs
                +File.separator+file.getOriginalFilename());
        //判断目标文件夹是否存在
        if (!descFile.getParentFile().exists()){
            descFile.getParentFile().mkdirs();
        }
        //保存文件到本地
        try {
            file.transferTo(descFile);
        }catch (Exception e){
            e.printStackTrace();
            return "upload failure";
        }
        return "upload success";
    }
}

3.3测试

SpringBoot文件上传_第1张图片

4.多文件上传

4.1前端页面,通过js动态添加input



    
        
        多文件上传
    
    
        

文件上传



请选择文件:

4.2后台处理

@Controller
public class MulUploadController {
    @RequestMapping("/tomulupload")
    public String toMulUpload(){
        return "mulupload";
    }

    @RequestMapping("/mulupload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile[] files){
            //判断文件是否为空
        if (files==null||files.length==0){
            return "please choices a file";
        }
        Calendar calendar=Calendar.getInstance();
        String rootPath="E:\\upload";
        //创建年月文件夹
        File dateDirs=new File(rootPath+File.separator+calendar.get(Calendar.YEAR)
                +File.separator+(calendar.get(Calendar.MONTH)+1));
        if (!dateDirs.exists()){
            dateDirs.mkdirs();
        }

        for (MultipartFile file : files) {
             if (file.isEmpty()){continue;}
            //目标文件
            File descFile=new File(dateDirs
                    +File.separator+file.getOriginalFilename());
            //保存文件到本地
            try {
                file.transferTo(descFile);
            }catch (Exception e){
                e.printStackTrace();
                return "upload failure";
            }
        }
        return "upload success";
    }
}

4.3 测试

SpringBoot文件上传_第2张图片

SpringBoot文件上传_第3张图片

 

你可能感兴趣的:(java,springboot学习)