本文實(shí)例為大家分享了Spring MVC多文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過十多年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行成都網(wǎng)站建設(shè)、做網(wǎng)站、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
1)創(chuàng)建工程并導(dǎo)入JAR包
2)創(chuàng)建多文件選擇頁面
在 WebContent 目錄下創(chuàng)建 JSP 頁面 multiFiles.jsp,在該頁面中使用表單上傳多個(gè)文件,具體代碼如下:
<%@ 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="${pageContext.request.contextPath }/multifile" method="post" enctype="multipart/form-data"> 選擇文件1:<input type="file" name="myfile"><br> 文件描述1:<input type="text" name="description"><br /> 選擇文件2:<input type="file" name="myfile"><br> 文件描述2:<input type="text" name="description"><br /> 選擇文件3:<input type="file" name="myfile"><br> 文件描述3:<input type="text" name="description"><br /> <input type="submit" value="提交"> </form> </body> </html>
3)創(chuàng)建POJO類
package pers.zhang.pojo; import java.util.List; import org.springframework.web.multipart.MultipartFile; public class MultiFileDomain { private List<String> description; private List<MultipartFile> myfile; public List<String> getDescription() { return description; } public void setDescription(List<String> description) { this.description = description; } public List<MultipartFile> getMyfile() { return myfile; } public void setMyfile(List<MultipartFile> myfile) { this.myfile = myfile; } }
4)創(chuàng)建多文件上傳處理方法
/** * 多文件上傳 */ @RequestMapping("/multifile") public String multiFileUpload(@ModelAttribute MultiFileDomain multiFileDomain,HttpServletRequest request) { String realpath = request.getServletContext().getRealPath("uploadfiles"); File targetDir = new File(realpath); if (!targetDir.exists()) { targetDir.mkdirs(); } List<MultipartFile> files = multiFileDomain.getMyFile(); for (int i = 0; i < files.size(); i++) { MultipartFile file = files.get(i); String fileName = file.getOriginalFilename(); File targetFile = new File(realpath, fileName); // 上傳 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } } logger.info("成功"); return "showMulti"; }
5)創(chuàng)建成功顯示頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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> <table> <tr> <td>詳情</td> <td>文件名</td> </tr> <!-- 同時(shí)取兩個(gè)數(shù)組的元素 --> <c:forEach items="${multiFileDomain.description}" var="description" varStatus="loop"> <tr> <td>${description}</td> <td>${multiFileDomain.myfile[loop.count-1].originalFilename}</td> </tr> </c:forEach> <!-- fileDomain.getMyfile().getOriginalFilename() --> </table> </body> </html>
6)測(cè)試文件上傳
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:SpringMVC實(shí)現(xiàn)多文件上傳
網(wǎng)站地址:http://muchs.cn/article0/ihdjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、云服務(wù)器、移動(dòng)網(wǎng)站建設(shè)、微信公眾號(hào)、App開發(fā)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)