詳解spring mvc(注解)上傳文件的簡(jiǎn)單例子
spring mvc(注解)上傳文件的簡(jiǎn)單例子。
這有幾個(gè)需要注意的地方
1.form的enctype=”multipart/form-data” 這個(gè)是上傳文件必須的
2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 關(guān)于文件上傳的配置不能少
3、親這兩個(gè)jar包不能少哦,之前就是因?yàn)樯倭诉@個(gè)兩個(gè)jar,一直報(bào)一些奇葩的錯(cuò),給我累了個(gè)半死~(版本沒什么要求)
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
大家可以看具體代碼如下:
web.xml
<?xml version="0" encoding="UTF-8"?> <web-app xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns="http://javasuncom/xml/ns/javaee" xmlns:web="http://javasuncom/xml/ns/javaee/web-app_2_xsd" xsi:schemaLocation="http://javasuncom/xml/ns/javaee http://javasuncom/xml/ns/javaee/web-app_2_xsd" id="WebApp_ID" version="5"> <display-name>webtest</display-name> <listener> <listener-class>orgspringframeworkwebcontextContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/applicationContextxml /WEB-INF/config/codeifActionxml </param-value> </context-param> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>orgspringframeworkwebservletDispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/codeifActionxml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 攔截所有以do結(jié)尾的請(qǐng)求 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>indexdo</welcome-file> </welcome-file-list> </web-app>
applicationContext.xml
<?xml version="0" encoding="UTF-8"?> <beans xmlns="http://wwwspringframeworkorg/schema/beans" xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p" xmlns:context="http://wwwspringframeworkorg/schema/context" xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd" default-lazy-init="true"> <!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 --> <bean class="orgspringframeworkwebservletmvcannotationAnnotationMethodHandlerAdapter" lazy-init="false" /> <!-- 另外最好還要加入DefaultAnnotationHandlerMapping,不然會(huì)被 XML或其它的映射覆蓋! --> <bean class="orgspringframeworkwebservletmvcannotationDefaultAnnotationHandlerMapping" /> <!-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> <bean class="orgspringframeworkwebservletviewInternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix="jsp" /> <!-- 支持上傳文件 --> <bean id="multipartResolver" class="orgspringframeworkwebmultipartcommonsCommonsMultipartResolver"/> </beans>
codeifAction.xml
<?xml version="0" encoding="UTF-8"?> <beans xmlns="http://wwwspringframeworkorg/schema/beans" xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:context="http://wwwspringframeworkorg/schema/context" xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd" default-lazy-init="true"> <bean id="uploadAction" class="comcodeifactionUploadAction" /> </beans>
UploadAction.java
package comcodeifaction; import javaioFile; import javautilDate; import javaxservlethttpHttpServletRequest; import orgspringframeworkstereotypeController; import orgspringframeworkuiModelMap; import orgspringframeworkwebbindannotationRequestMapping; import orgspringframeworkwebbindannotationRequestParam; import orgspringframeworkwebmultipartMultipartFile; @Controller public class UploadAction { @RequestMapping(value = "/uploaddo") public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { Systemoutprintln("開始"); String path = requestgetSession()getServletContext()getRealPath("upload"); String fileName = filegetOriginalFilename(); // String fileName = new Date()getTime()+"jpg"; Systemoutprintln(path); File targetFile = new File(path, fileName); if(!targetFileexists()){ targetFilemkdirs(); } //保存 try { filetransferTo(targetFile); } catch (Exception e) { eprintStackTrace(); } modeladdAttribute("fileUrl", requestgetContextPath()+"/upload/"+fileName); return "result"; } }
index.jsp
<%@ page pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上傳圖片</title> </head> <body> <form action="uploaddo" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Submit" /></form> </body> </html>
WEB-INF/jsp/下的result.jsp
<%@ page pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上傳結(jié)果</title> </head> <body> <img alt="" src="${fileUrl }" /> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成Dubbo啟用gRPC協(xié)議
這篇文章主要介紹了SpringBoot集成Dubbo啟用gRPC協(xié)議,以及與原生 gRPC 在代碼編寫過程中的區(qū)別。感興趣的同學(xué)可以參考閱讀2023-04-04Java使用EasyExcel進(jìn)行單元格合并的問題詳解
項(xiàng)目中需要導(dǎo)出并合并指定的單元格,下面這篇文章主要給大家介紹了關(guān)于java評(píng)論、回復(fù)功能設(shè)計(jì)與實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06SpringBoot使用Kafka來優(yōu)化接口請(qǐng)求的并發(fā)方式
這篇文章主要介紹了SpringBoot使用Kafka來優(yōu)化接口請(qǐng)求的并發(fā)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07MyBatisPlus 一對(duì)多、多對(duì)一、多對(duì)多的完美解決方案
這篇文章主要介紹了MyBatisPlus 一對(duì)多、多對(duì)一、多對(duì)多的完美解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11java對(duì)象與json對(duì)象之間互相轉(zhuǎn)換實(shí)現(xiàn)方法示例
這篇文章主要介紹了java對(duì)象與json對(duì)象之間互相轉(zhuǎn)換實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java對(duì)象與json對(duì)象相互轉(zhuǎn)換實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10java中靜態(tài)變量和實(shí)例變量的區(qū)別詳細(xì)介紹
本篇文章介紹了,java中靜態(tài)變量和實(shí)例變量的區(qū)別。需要的朋友參考下2013-05-05springboot + swagger 實(shí)例代碼
本篇文章主要介紹了springboot + swagger 實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Java如何實(shí)現(xiàn)實(shí)體類轉(zhuǎn)Map、Map轉(zhuǎn)實(shí)體類
這篇文章主要介紹了Java 實(shí)現(xiàn)實(shí)體類轉(zhuǎn)Map、Map轉(zhuǎn)實(shí)體類的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringMVC實(shí)現(xiàn)返回響應(yīng)的項(xiàng)目實(shí)踐
本文主要介紹了SpringMVC實(shí)現(xiàn)返回響應(yīng)的項(xiàng)目實(shí)踐,包含返回靜態(tài)頁面,返回?cái)?shù)據(jù),返回html片段等實(shí)例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02