利用MultipartFile來進行文件上傳


 這個例子實在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";
        
    }
}

 

 

                                    

多文件上傳

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[ ]來接收前端傳來的文件,然后循環處理每個文件,每個文件的上傳和單個文件的上傳一樣


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM