利用MultipartFile来进行文件上传

时间:2022-03-23 21:34:37

 这个例子实在SpringMVC的基础上完成的,因此在web.xml中需要配置

web.xml

<!-- 配置Spring MVC的入口 DispatcherServlet,把所有的请求都提交到该Servlet -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

在springmvc-servlet.xml中配置

<!-- 为了上传文件引入 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
   

单个文件上传

firstUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一种上传方式</title>
</head>
<body>
 <form action="firstUploadController" method="post" enctype="multipart/form-data">
   <h2>文件上传</h2>
                文件:<input type="file" name="uploadFile"/><br/><br/>
      <input type="submit" value="上传"/>
   </form>
</body>
</html>
enctype="multipart/form-data"

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

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

需要注意的是:默认情况下,enctype的值是application/x-www-form-urlencoded,不能用于文件上传,只有使用了multipart/form-data,才能完整的传递文件数据。

application/x-www-form-urlencoded不是不能上传文件,是只能上传文本格式的文件,multipart/form-data是将文件以二进制的形式上传,这样可以实现多种类型的文件上传。

FirstUploadController.java

package controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FirstUploadController {
    @RequestMapping("firstUploadController")
    public String doFirst(@RequestParam("uploadFile") MultipartFile uploadFile, HttpSession session) throws IllegalStateException, IOException {
        String filename = uploadFile.getOriginalFilename();
        
        String leftPath = session.getServletContext().getRealPath("/image");
        System.out.println(leftPath);
        File file = new File(leftPath, filename);
        
        uploadFile.transferTo(file);
        return "index";
        
    }
}

 

 

利用MultipartFile来进行文件上传

利用MultipartFile来进行文件上传                                    利用MultipartFile来进行文件上传

多文件上传

secondUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="secondUploadController" method="post" enctype="multipart/form-data">
   <h2>文件上传</h2>
                文件1:<input type="file" name="uploadFile"/><br/><br/>
                文件2:<input type="file" name="uploadFile"/><br/><br/>
                文件3:<input type="file" name="uploadFile"/><br/><br/>
      <input type="submit" value="上传"/>
   </form>
</body>
</html>

SecondUplodController.java

package controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class SecondUploadController {
    @RequestMapping("secondUploadController")
    public String doFirst(@RequestParam("uploadFile") MultipartFile[] uploadFile, HttpSession session) throws IllegalStateException, IOException {
        for(MultipartFile item:uploadFile) {
            if(item.getSize()>0) {
                String filename = item.getOriginalFilename();
            
                String leftPath = session.getServletContext().getRealPath("/image");
                //System.out.println(leftPath);
                File file = new File(leftPath, filename);
                
                item.transferTo(file);
            }
            
        }
        return "index";
        
    }
}

和单个文件上传的区别就是用MultipartFile[ ]来接收前端传来的文件,然后循环处理每个文件,每个文件的上传和单个文件的上传一样