SpringMVC的文件上传、多文件上传

概念:上传/下载应用

对于上传功能,我们在项目中是经常会用到的,比如用户注册的时候,上传用户头像,这个时候就会使用到上传的功能。而对于下载,使用场景也很常见,比如我们项目中有个使用说明是是pdf版的,会提供给用户进行下载的功能。

使用 Commons-fileupload 组件实现文件上传,需要导入该组件相应的支撑 jar 包:Commons-fileupload 和commons-io。

1-文件上传的必要前提

文件上传客户端表单需要满足:

  1. 表单项type=“file”

  2. 表单的提交方式是post

  3. 表单的enctype属性是:enctype=“multipart/form-data”

    名称
    文件1

【扩展说明】

nctype就是encodetype就是编码类型的意思。

multipart/form-data是指表单数据有多部分构成,既有文本数据,又有文件等二进制数据的意思。

默认情况下,enctype的值是有三个: 1、application/x-www-form-urlencoded,只能上传文本格式的文件,用文本的传输和MP3等大型文件的时候,使用这种编码就显得 效率低下。

2、multipart/form-data,指定传输数据为二进制类型,比如图片、mp3、文件等才能完整的传递文件数据。可以实现多种类型的文件上传。

3、text/plain。纯文体的传输。空格转换为 “+” 加号,但不对特殊字符编码。

2-案例1:单文件上传

前缀:项目


      commons-fileupload
      commons-fileupload
      1.3.1
    
    
      commons-io
      commons-io
      2.3
    
步骤2:index.jsp添加form表单

Springmvc文件上传

用户名:
选择文件:
步骤3:配置多媒体解析器

    
          
        
        
        
        
    
步骤4:Control层编写方法:完成文件上传
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * SpringMVC文件上传
     * 使用fileupload组件完成文件上传
     * @return
     */
    @RequestMapping("/fileupload")
    public String fileuoload(String username,
            HttpServletRequest request, MultipartFile upload) throws Exception {
        System.out.println(username+":实现springmvc文件上传...");
        // 说明上传文件项
        // 获取上传文件的名称
        String filename = upload.getOriginalFilename();
        // 把文件的名称设置唯一值,uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid+"_"+filename;

        // 判断,该路径是否存在
        File file = new File("D:\\upload2",filename);
        if(!file.exists()){
            // 创建该文件夹
            file.mkdirs();
        }
        // 完成文件上传
        upload.transferTo(file);

        // 上传的位置(项目打包的位置)
        //String path = request.getSession().getServletContext().getRealPath("/uploads/");
        //文件上传的路径
       // System.out.println(path);
        return "success";
    }

}
步骤4:查看结果

SpringMVC的文件上传、多文件上传_第1张图片

3-案例2:多文件上传

多文件上传,只需要将页面修改为多个文件上传项,将方法参数MultipartFile类型修改为MultipartFile[]即可

步骤1:index.jsp添加以下代码

Springmvc多文件上传

名称
文件1
文件2
步骤2:Control层添加方法
/**
     * SpringMVC多文件上传
     */
    @RequestMapping("/fileupload2")
    public String fileuoload2(String username,MultipartFile[] uploadFile) throws Exception {
        System.out.println(username+":实现springmvc文件上传...");
        //多个文件循环上传
        for (MultipartFile multipartFile : uploadFile) {
            //获取文件名字
            String filename = multipartFile.getOriginalFilename();
            //给文件名添加唯一标识符
            String uuid = UUID.randomUUID().toString().replace("-", "");
            filename = uuid+"_"+filename;
            // 判断,该路径是否存在
            File file = new File("C:\\upload2",filename);
            if(!file.exists()){
                // 创建该文件夹
                file.mkdirs();
            }
            //直接上传
            multipartFile.transferTo(file);
        }
        return "success";
    }
步骤3:结课

SpringMVC的文件上传、多文件上传_第2张图片

你可能感兴趣的:(springboot,开发语言,java,spring,spring,boot,intellij-idea)