国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

詳解spring mvc(注解)上傳文件的簡(jiǎn)單例子

2019-11-26 13:14:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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è)半死~(版本沒(méi)什么要求)

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("開(kāi)始");     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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桐柏县| 弋阳县| 武乡县| 怀化市| 舟曲县| 永昌县| 惠州市| 昔阳县| 柘荣县| 上思县| 信阳市| 景德镇市| 阳西县| 海林市| 运城市| 陆河县| 宁国市| 六安市| 宁武县| 蕲春县| 甘孜| 枞阳县| 洛隆县| 安陆市| 承德县| 灵璧县| 全州县| 元阳县| 东港市| 虎林市| 天长市| 兴城市| 隆德县| 房山区| 陇西县| 增城市| 满洲里市| 寻乌县| 马关县| 庆城县| 临夏市|