如何使用 Spring MVC 將多個文件上傳到特定位置? 使用 Java 代碼完成教程
已發表: 2013-06-22 這是另一個完整的 Spring MVC 教程,它接受上傳表單上的文件並在“提交”事件時將其複製到特定文件夾。 像往常一樣,我們dependency
於 Hello World Spring MVC Example。
因此,這些是我們需要在此示例中執行的添加/更改:
- 新文件:CrunchifyFileUploadController.java
- 新文件:CrunchifyFileUpload.java
- 新文件:uploadfile.jsp
- 新文件:uploadfilesuccess.jsp
- 修改文件:crunchify-servlet.xml
- 2 個新的 jar 文件:
commons-io-2.4.jar
和commons-fileupload-1.3.jar
有以下任何問題嗎? 那麼你來對地方了。
- Java 多文件上傳示例
- 上傳多個文件或文件夾
這是最終的項目結構,因此您將對添加文件的位置有所了解。
現在讓我們開始吧:
步驟 1) 先決條件:
https://crunchify.com/hello-world-example-spring-mvc-3-2-1/(在Tomcat上部署這個項目成功)
Maven依賴:
將以下新依賴項添加到項目的pom.xml
文件中。
1 2 3 4 5 6 7 8 9 10 |
< dependency > < groupId > commons - fileupload < / groupId > < artifactId > commons - fileupload < / artifactId > < version > 1.2 < / version > < / dependency > < dependency > < groupId > commons - io < / groupId > < artifactId > commons - io < / artifactId > < version > 1.4 < / version > < / dependency > |
Step-2) SpringController
創建一個基於 Spring 3 MVC 的控制器來處理文件上傳。 這個控制器有兩種方法:
-
crunchifyDisplayForm
– 它只是將請求轉發到 pageuploadfile.jsp -
crunchifySave
– 使用@ModelAttribute
註釋獲取表單並從中獲取文件內容。 它創建正在上傳的文件的文件名列表,並將此列表傳遞給成功頁面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com . crunchify . controller ; import com . crunchify . form . CrunchifyFileUpload ; import java . io . File ; import java . io . IOException ; import java . util . ArrayList ; import java . util . List ; import org . springframework . stereotype . Controller ; import org . springframework . ui . Model ; import org . springframework . web . bind . annotation . ModelAttribute ; import org . springframework . web . bind . annotation . RequestMapping ; import org . springframework . web . bind . annotation . RequestMethod ; import org . springframework . web . multipart . MultipartFile ; @Controller public class CrunchifyFileUploadController { @RequestMapping ( value = "/upload" , method = RequestMethod . GET ) public String crunchifyDisplayForm ( ) { return "uploadfile" ; } @RequestMapping ( value = "/savefiles" , method = RequestMethod . POST ) public String crunchifySave ( @ModelAttribute ( "uploadForm" ) CrunchifyFileUpload uploadForm , Model map ) throws IllegalStateException , IOException { String saveDirectory = "c:/crunchify/" ; List <MultipartFile> crunchifyFiles = uploadForm . getFiles ( ) ; List <String> fileNames = new ArrayList <String> ( ) ; if ( null ! = crunchifyFiles && crunchifyFiles.size() > 0) { for (MultipartFile multipartFile : crunchifyFiles) { String fileName = multipartFile.getOriginalFilename(); if ( ! "" . equalsIgnoreCase ( fileName ) ) { // Handle file content - multipartFile.getInputStream() multipartFile . transferTo ( new File ( saveDirectory + fileName ) ) ; fileNames . add ( fileName ) ; } } } map . addAttribute ( "files" , fileNames ) ; return "uploadfilesuccess" ; } } |
Step-3) 模型——表單對象
創建一個 Java bean,它充當我們 Spring 應用程序的模型/表單對象。 這個 bean 包含一個org.springframework.web.multipart.MultipartFile
對象的List
。 Spring 框架提供了一個有用的 MultipartFile 類,可用於獲取上傳文件的文件內容。 除了其內容,MultipartFile 對像還為您提供其他有用的信息,例如文件名、文件大小等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com . crunchify . form ; import java . util . List ; import org . springframework . web . multipart . MultipartFile ; public class CrunchifyFileUpload { private List <MultipartFile> crunchifyFiles ; public List <MultipartFile> getFiles ( ) { return crunchifyFiles ; } public void setFiles ( List <MultipartFile> files ) { this . crunchifyFiles = files ; } } |
步驟 4) JSP 視圖
現在為這個應用程序創建視圖頁面。 我們將需要兩個 JSP,一個用於顯示文件上傳表單,另一個用於顯示成功上傳的結果。
uploadfile.jsp
顯示帶有文件輸入的表單。 除此之外,我們還添加了單擊添加按鈕的小 jquery 片段。 這將在表單末尾添加一個新的文件輸入組件。 這允許用戶上傳任意數量的文件。
請注意,我們設置了<form>
標籤的enctype=”multipart/form-data”
屬性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form" %> < html > < head > < title > Crunchify - Spring MVC Upload Multiple Files Example < / title > <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" > </script> <script> $ ( document ) . ready ( function ( ) { //add more file components if Add is clicked $ ( '#addFile' ) . click ( function ( ) { var fileIndex = $ ( '#fileTable tr' ) . children ( ) . length - 1 ; $ ( '#fileTable' ) . append ( '<tr><td>' + ' <input type="file" name="files[' + fileIndex + ']" />' + '</td></tr>' ) ; } ) ; } ) ; </script> <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png' ) ; } </style> < / head > < body > < br > < br > < div align = "center" > < h1 > Crunchify - Spring MVC Upload Multiple Files Example < / h1 > < form : form method = "post" action = "savefiles.html" modelAttribute = "uploadForm" enctype = "multipart/form-data" > < p > Select files to upload . Press Add button to add more file inputs . < / p > < table id = "fileTable" > < tr > < td > < input name = "files[0]" type = "file" / > < / td > < / tr > < tr > < td > < input name = "files[1]" type = "file" / > < / td > < / tr > < / table > < br / > < input type = "submit" value = "Upload" / > < input id = "addFile" type = "button" value = "Add File" / > < / form : form > < br / > < / div > < / body > < / html > |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<% @ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> < html > < head > < title > Crunchify - Upload Multiple Files Example < / title > <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png' ) ; } </style> < / head > < body > < br > < br > < div align = "center" > < h1 > Crunchify - Spring MVC Upload Multiple Files Example < / h1 > < p > Awesome . . Following files are uploaded successfully . < / p > < ol > < c : forEach items = "${files}" var = "file" > - $ { file } < br > < / c : forEach > < / ol > < a href = "http://localhost:8080/CrunchifySpringMVC3.2.1/upload.html" > < input type = "button" value = "Go Back" / > < / a > < br / > < br / > < br / > < div style = "font-family: verdana; line-height: 25px; padding: 5px 10px; border-radius: 10px; border: 1px dotted #A4A4A4; width: 50%; font-size: 12px;" > Spring MVC Upload Multiple Files Example by < a href = 'https://crunchify.com' > Crunchify < / a > . Click < a href = 'https://crunchify.com/category/java-tutorials/' > here < / a > for all Java , Spring MVC , Web Development examples . < br > < / div > < / div > < / body > < / html > |
Step-5) 更新 Spring 配置
將下面的 bean 添加到crunchify-servlet.xml
文件中,就在<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
行的上方。
1 2 |
< bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" / > |
Step-6) 結賬結果
啟動 tomcat 並將瀏覽器指向此 URL:http://localhost:8080/CrunchifySpringMVC3.2.1/upload.html,您應該會看到與此類似的屏幕。
文件上傳後,您將看到這樣的成功消息。 您始終可以按照您想要的方式美化您的 .jsp 文件。
所有 Spring MVC 示例、Java 示例的列表。