SpringMVC實(shí)現(xiàn)文件上傳下載功能
今天遇到文件上傳的問題,使用Ajax方式進(jìn)行提交,服務(wù)器一直報(bào)錯(cuò)The current request is not a multipart request
,看了網(wǎng)上很多方法,最后才找到,我在表單提交的時(shí)候使用了序列化$('#postForm').serialize()
,但是這種方式,只能傳遞一般的參數(shù),上傳文件的文件流是無法被序列化并傳遞的。所以一直在報(bào)錯(cuò)。后來就直接使用submint()
,放棄使用Ajax。不過還是想記下來,以備不時(shí)之需。
接下來進(jìn)入正題:在SSM框架下實(shí)現(xiàn)文件上傳和下載
導(dǎo)入需要的依賴包
pom.xml
<!-- 上傳組件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- SpringMVC的前端控制器 --> <servlet> <servlet-name>MyDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 設(shè)置自己定義的控制器xml文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springMVC-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC配置文件結(jié)束 --> <!-- 攔截設(shè)置 --> <servlet-mapping> <servlet-name>MyDispatcher</servlet-name> <!-- 由SpringMVC攔截所有請求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 把標(biāo)記了@Controller注解的類轉(zhuǎn)換為bean --> <context:component-scan base-package="com.mucfc" /> <!-- 視圖解析器 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp"></property> </bean> <!-- 上傳文件的設(shè)置 ,maxUploadSize=-1,表示無窮大。uploadTempDir為上傳的臨時(shí)目錄 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" <!-- one of the properties available; the maximum file size in bytes --> <!-- 1KB= 1024 byte 1MB=1024KB 1GB=1024MB--> <!-- 默認(rèn)編碼 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10000000" /> <!-- 內(nèi)存中的最大值 --> <property name="maxInMemorySize" value="40960" /> /> </beans>
一、單個(gè)文件上傳
JSP,這個(gè)頁面是用來上傳又用來顯示上傳后的圖片的頁面fileUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>用戶上傳圖片頁面</title> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <center> <form action="file/onefile" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上 傳" /> </form> <h5>上傳結(jié)果:</h5> <img alt="暫無圖片" src="${fileUrl}" /> </center> </body> </html>
Conroller層
1、方法一
@Controller @RequestMapping("/file") public class FileController { @RequestMapping("/toFile") public String toFileUpload() { return "fileUpload"; } @RequestMapping("/toFile2") public String toFileUpload2() { return "fileUpload2"; } /** * 方法一上傳文件 */ @RequestMapping("/onefile") public String oneFileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response) { // 獲得原始文件名 String fileName = file.getOriginalFilename(); System.out.println("原始文件名:" + fileName); //避免文件名重復(fù)使用uuid來避免,產(chǎn)生一個(gè)隨機(jī)的uuid字符,并賦予一個(gè)新的文件名 String newFileName = UUID.randomUUID() + fileName; // 獲得項(xiàng)目的路徑 ServletContext servletContext = request.getSession().getServletContext(); // 上傳位置 String path=servletContext.getRealPath("/upload")+"/"; //設(shè)定文件保存的目錄 File f = new File(path); if (!f.exists()) f.mkdirs(); if (!file.isEmpty()) { try { FileOutputStream fos = new FileOutputStream(path + newFileName); InputStream in = file.getInputStream(); int b = 0; while ((b = in.read()) != -1) { fos.write(b); } fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("上傳圖片到:" + path + newFileName); // 保存文件地址,用于JSP頁面回顯 model.addAttribute("fileUrl", path + newFileName); return "fileUpload"; }
運(yùn)行后的效果:
原始文件名:1522330092612.jpg
上傳位置:D:\tomcat-8.5.43\webapps\telecomsystem\upload
效果圖:
上傳前
上傳后
2、方法二:使用文件流的方式來上傳
/** * 方法二上傳文件,一次一張 */ @RequestMapping("/onefile2") public String oneFileUpload2(HttpServletRequest request, HttpServletResponse response) throws Exception { CommonsMultipartResolver cmr = new CommonsMultipartResolver( request.getServletContext()); if (cmr.isMultipart(request)) { MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request); Iterator<String> files = mRequest.getFileNames(); while (files.hasNext()) { MultipartFile mFile = mRequest.getFile(files.next()); if (mFile != null) { String fileName = UUID.randomUUID() + mFile.getOriginalFilename(); String path = "d:/upload/" + fileName; File localFile = new File(path); mFile.transferTo(localFile); request.setAttribute("fileUrl", path); } } } return "fileUpload"; }
jsp部分代碼:
<center> <form action="file/onefile2" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上 傳" /> </form> <h5>上傳結(jié)果:</h5> <img alt="暫無圖片" src="${fileUrl}" /> </center>
方法二指定上傳到了本地E盤的upload文件夾
二、多個(gè)文件上傳
多個(gè)文件上傳其實(shí)就是將單文件上傳的方法一中修改成循環(huán)就可以了
JSP顯示頁面fileUpload2.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>用戶上傳圖片頁面</title> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <center> <form action="file/threeFile" method="post" enctype="multipart/form-data"> <input type="file" name="file" /><br /> <input type="file" name="file" /><br /> <input type="file" name="file" /><br /> <input type="submit" value="上 傳" /> </form> <h5>上傳結(jié)果:</h5> <c:forEach items="${fileList}" var="imagename"> <img alt="暫無圖片" src="${imagename}" /> </c:forEach> </center> </body> </html>
Controller層
/** * 一次上傳多張圖片 */ @RequestMapping("/threeFile") public String threeFileUpload( @RequestParam("file") CommonsMultipartFile files[], HttpServletRequest request, ModelMap model) { List<String> list = new ArrayList<String>(); // 獲得項(xiàng)目的路徑 ServletContext sc = request.getSession().getServletContext(); // 上傳位置 String path = sc.getRealPath("/img") + "/"; // 設(shè)定文件保存的目錄 File f = new File(path); if (!f.exists()) f.mkdirs(); for (int i = 0; i < files.length; i++) { // 獲得原始文件名 String fileName = files[i].getOriginalFilename(); System.out.println("原始文件名:" + fileName); // 新文件名 String newFileName = UUID.randomUUID() + fileName; if (!files[i].isEmpty()) { try { FileOutputStream fos = new FileOutputStream(path + newFileName); InputStream in = files[i].getInputStream(); int b = 0; while ((b = in.read()) != -1) { fos.write(b); } fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("上傳圖片到:" + path + newFileName); list.add(path + newFileName); } // 保存文件地址,用于JSP頁面回顯 model.addAttribute("fileList", list); return "fileUpload2"; }
三、上傳文件列表顯示
jsp文件listFile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML> <html> <head> <title>下載文件顯示頁面</title> </head> <body> <!-- 遍歷Map集合 --> <c:forEach var="me" items="${fileNameMap}"> <c:url value="/file/downFile" var="downurl"> <c:param name="filename" value="${me.key}"></c:param> </c:url> ${me.value}<a href="${downurl}" rel="external nofollow" >下載</a> </c:forEach> </body> </html>
Controller層
/** * 列出所有的圖片 */ @RequestMapping("/listFile") public String listFile(HttpServletRequest request, HttpServletResponse response) { // 獲取上傳文件的目錄 ServletContext sc = request.getSession().getServletContext(); // 上傳位置 String uploadFilePath = sc.getRealPath("/img") + "/"; // 設(shè)定文件保存的目錄 // 存儲要下載的文件名 Map<String, String> fileNameMap = new HashMap<String, String>(); // 遞歸遍歷filepath目錄下的所有文件和目錄,將文件的文件名存儲到map集合中 listfile(new File(uploadFilePath), fileNameMap);// File既可以代表一個(gè)文件也可以代表一個(gè)目錄 // 將Map集合發(fā)送到listfile.jsp頁面進(jìn)行顯示 request.setAttribute("fileNameMap", fileNameMap); return "listFile"; }
四、文件下載
Controller層
@RequestMapping("/downFile") public void downFile(HttpServletRequest request, HttpServletResponse response) { System.out.println("1"); // 得到要下載的文件名 String fileName = request.getParameter("filename"); System.out.println("2"); try { fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8"); System.out.println("3"); // 獲取上傳文件的目錄 ServletContext sc = request.getSession().getServletContext(); System.out.println("4"); // 上傳位置 String fileSaveRootPath = sc.getRealPath("/img"); System.out.println(fileSaveRootPath + "\\" + fileName); // 得到要下載的文件 File file = new File(fileSaveRootPath + "\\" + fileName); // 如果文件不存在 if (!file.exists()) { request.setAttribute("message", "您要下載的資源已被刪除??!"); System.out.println("您要下載的資源已被刪除?。?); return; } // 處理文件名 String realname = fileName.substring(fileName.indexOf("_") + 1); // 設(shè)置響應(yīng)頭,控制瀏覽器下載該文件 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8")); // 讀取要下載的文件,保存到文件輸入流 FileInputStream in = new FileInputStream(fileSaveRootPath + "\\" + fileName); // 創(chuàng)建輸出流 OutputStream out = response.getOutputStream(); // 創(chuàng)建緩沖區(qū) byte buffer[] = new byte[1024]; int len = 0; // 循環(huán)將輸入流中的內(nèi)容讀取到緩沖區(qū)當(dāng)中 while ((len = in.read(buffer)) > 0) { // 輸出緩沖區(qū)的內(nèi)容到瀏覽器,實(shí)現(xiàn)文件下載 out.write(buffer, 0, len); } // 關(guān)閉文件輸入流 in.close(); // 關(guān)閉輸出流 out.close(); } catch (Exception e) { } }
注:這里是通過文件流的方式來下載圖片的。
以上就是SpringMVC實(shí)現(xiàn)文件上傳下載功能的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC文件上傳下載的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringMVC實(shí)現(xiàn)文件上傳與下載
- springmvc實(shí)現(xiàn)文件上傳功能
- SpringMVC 通過commons-fileupload實(shí)現(xiàn)文件上傳功能
- IDEA實(shí)現(xiàn) springmvc的簡單注冊登錄功能的示例代碼
- Spring+SpringMVC+JDBC實(shí)現(xiàn)登錄的示例(附源碼)
- Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
- springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理
- Spring MVC實(shí)現(xiàn)文件上傳及優(yōu)化案例解析
相關(guān)文章
詳解Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么
java服務(wù)啟動(dòng)的時(shí)候,都會(huì)指定一些參數(shù),下面這篇文章主要給大家介紹了關(guān)于Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07JavaWeb Servlet實(shí)現(xiàn)網(wǎng)頁登錄功能
這篇文章主要為大家詳細(xì)介紹了JavaWeb Servlet實(shí)現(xiàn)網(wǎng)頁登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Java-Io-RandomAccessFile任意位置讀寫數(shù)據(jù)的操作小結(jié)
RandomAccessFile類支持隨機(jī)訪問方式,可以跳轉(zhuǎn)到文件的任意位置讀寫數(shù)據(jù),這個(gè)類在文件隨機(jī)讀取時(shí)有很大的優(yōu)勢,可利用多線程完成對一個(gè)大文件的讀寫,本文給大家介紹Java-Io-RandomAccessFile(任意位置讀寫數(shù)據(jù))的相關(guān)知識,需要的朋友可以參考下2022-05-05SpringBoot中application.properties、application.yaml、applicati
本文主要介紹了SpringBoot中application.properties、application.yaml、application.yml區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04Dubbo Service Mesh基礎(chǔ)架構(gòu)組件改造
Service Mesh這個(gè)“熱”詞是2016年9月被“造”出來,而今年2018年更是被稱為service Mesh的關(guān)鍵之年,各家大公司都希望能在這個(gè)思潮下領(lǐng)先一步2023-03-03Java PriorityQueue優(yōu)點(diǎn)和缺點(diǎn)面試精講
這篇文章主要為大家介紹了Java面試中PriorityQueue的優(yōu)點(diǎn)和缺點(diǎn)及使用注意詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10