一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解spring mvc(注解)上傳文件的簡單例子

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

2020-07-27 14:37@ 小浩 Java教程

本篇文章主要介紹了spring mvc(注解)上傳文件的簡單例子,具有一定的參考價值,有興趣的可以了解一下。

spring mvc(注解)上傳文件的簡單例子。

這有幾個需要注意的地方

1.form的enctype=”multipart/form-data” 這個是上傳文件必須的

2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 關于文件上傳的配置不能少

3、親這兩個jar包不能少哦,之前就是因為少了這個兩個jar,一直報一些奇葩的錯,給我累了個半死~(版本沒什么要求)

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar 

大家可以看具體代碼如下:

web.xml

?
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
<?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結尾的請求 -->
  <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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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">
 
  <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->
  <bean class="orgspringframeworkwebservletmvcannotationAnnotationMethodHandlerAdapter" lazy-init="false" />
 
  <!-- 另外最好還要加入DefaultAnnotationHandlerMapping,不然會被 XML或其它的映射覆蓋! -->
  <bean class="orgspringframeworkwebservletmvcannotationDefaultAnnotationHandlerMapping" />
 
  <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 -->
  <bean class="orgspringframeworkwebservletviewInternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix="jsp" />
 
  <!-- 支持上傳文件 -->
  <bean id="multipartResolver" class="orgspringframeworkwebmultipartcommonsCommonsMultipartResolver"/>
 
</beans>

codeifAction.xml

?
1
2
3
4
5
6
7
8
9
10
<?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

?
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
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

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
<%@ page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上傳結果</title>
</head>
<body>
<img id="codetool">

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/xiohao/p/5273102.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国模大胆一区二区三区 | 羞羞影院午夜男女爽爽影院网站 | 久久国产热视频99rev6 | 亚洲男女天堂 | 四虎精品成人免费观看 | 亚洲网站在线看 | 99久久国产综合精品网成人影院 | 俄罗斯美女破苞 | 3p文两男一女办公室高h | 日本xx高清视频免费观看 | 国产一区二区在线观看视频 | 精品一区久久 | 人人干国产 | 国产精品亚洲片在线va | 成年人在线观看视频免费 | 操尼姑| 无罩看奶禁18 | 色老大在线 | 精品日本三级在线观看视频 | 日本人在线看片 | 人人爽人人草 | 美女张开腿让我了一夜 | 色噜噜狠狠狠综合曰曰曰88av | 91精品国产人成网站 | 明星乱亚洲 | 亚洲第一男人网站 | japan孕妇孕交 | 好大好硬好深好爽想要小雪 | 倩女还魂在线观看完整版免费 | 精品久久成人 | 亚洲a视频在线 | 久久亚洲高清观看 | avove本人照片 | 亚洲国产99在线精品一区69堂 | 波多野结衣中文字幕在线 | 精品久久久久久久久久久久久久久 | 青青草原国产视频 | 亚州成人| 欧美日韩一区二区综合在线视频 | 亚洲精品国产在线观看 | 涩涩五月天 |
<var id="oktqw"></var>

    <strike id="oktqw"></strike>