springMVC之文件上传

1.简介
基本知识点就是在表单中使用file,进行文件的提交,然后在Controller中使用Multipartfile跟part进行文件的处理,上传。

2.part跟Multipartfile也有不同点:

part是servlet提供的,路径自己写
multipartFile是spring提供的,路径自行配置

Part获取原始文件名: getSubmittedFileName();
MultipartFile获取原始文件名是:getOriginalFileName();
  1. 音乐类型文件无法进行上传 自测!

4.单文件上传
MultipartFile之springMVC方法的单文件上传:

(1)multipartfile在web.xml中进行配置



ssm

    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp



 
    contextConfigLocation
    /WEB-INF/applicationContext.xml
 



    org.springframework.web.context.ContextLoaderListener




    ds
    
    org.springframework.web.servlet.DispatcherServlet
    
    1
    
    
    e:/
    
    5242880
    
    10485760
    
    0
    
    


    
    ds
    
    *.do



    
    e:/
    
    5242880
    
    10485760
    
    0
    

(2)form表单:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here



(3)Controller控制器

package com.gb.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.gb.pojo.UserRegister;
@Controller
@RequestMapping("file")
public class FileController {

@RequestMapping("upload")
public ModelAndView upload(MultipartFile photo, String uname, String note) {
    
    String fileName = photo.getOriginalFilename();
    String contentType = photo.getContentType();
    
    String destFile = UUID.randomUUID() + fileName;
    
    File file = new File(destFile); 
    
    ModelAndView mv = new ModelAndView();
    try {
        photo.transferTo(file);
        mv.addObject("success","上传成功");
    } catch (IllegalStateException | IOException e) {
        mv.addObject("fail","上传失败");
        e.printStackTrace();
    }
    mv.setView(new MappingJackson2JsonView());
    
    return mv;
}

(4)如果参数比较多的话可以使用pojo进行参数的传递(设置原型避免单例)

package com.gb.pojo;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(scopeName="prototype")
public class UserRegister {

private String uname;
private String note;
public String getUname() {
    return uname;
}
public void setUname(String uname) {
    this.uname = uname;
}
public String getNote() {
    return note;
}
public void setNote(String note) {
    this.note = note;
}}

        @RequestMapping("file")
    public ModelAndView file(MultipartFile photo, UserRegister user) {
        
        String fileName = photo.getOriginalFilename();
        String contentType = photo.getContentType();
        
        String destFile = UUID.randomUUID() + fileName;
        
        File file = new File(destFile); 
        
        ModelAndView mv = new ModelAndView();
        try {
            photo.transferTo(file);
            mv.addObject("success","上传成功");
        } catch (IllegalStateException | IOException e) {
            mv.addObject("fail","上传成功");
            e.printStackTrace();
        }
        mv.setView(new MappingJackson2JsonView());
        
    return mv;
}

单文件上传之part形式:
part是servlet提供的,路径不需要配置的,所以也不用在web.xml配置文件中进行路径以及阈值等的配置

Controller控制器中的代码:

    @RequestMapping("uploadpart")
public ModelAndView upload(Part photo, String uname, String note) {
    
    // 获取源文件名
    String fileName = photo.getSubmittedFileName();
    String contentType = photo.getContentType();
    
    String destFile = UUID.randomUUID() + fileName;
    
    
    ModelAndView mv = new ModelAndView();
    try {
        photo.write("f:/" + destFile);
        mv.addObject("success","上传成功");
    } catch (IllegalStateException | IOException e) {
        mv.addObject("fail","上传失败");
        e.printStackTrace();
    }
    mv.setView(new MappingJackson2JsonView());
    
    return mv;
}
这里使用的是 getSubmittedFileName()方法来获取的源文件名,但是这个方法在tomcat7.x中是没有的。

5.多文件上传
使用Multipartfile数组形式

 @RequestMapping("uploadmulti")
public ModelAndView uploadMulti(MultipartFile[] photo, UserRegister ur) {
    
    
    ModelAndView mv = new ModelAndView();
    try {
        for(MultipartFile mf:photo) {
            String destFile = UUID.randomUUID() + mf.getOriginalFilename();
            File file = new File(destFile); 
            mf.transferTo(file);
        }
        
        mv.addObject("success","上传成功");
    } catch (IllegalStateException | IOException e) {
        mv.addObject("fail","上传失败");
        e.printStackTrace();
    }
    mv.setView(new MappingJackson2JsonView());
    
    return mv;
}

原理很简单,就是form表单中加入多个file,提交的时候提交多个文件到Multipartfile数组中,再在控制器中进行foreach遍历添加UUID路径等处理,最后transferTo发送一下就ok了。

也可以使用part数组形式。。。

你可能感兴趣的:(springMVC之文件上传)