1. 程式人生 > >spring之文件上傳

spring之文件上傳

exce work 方案 pac -- body let cti ssp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7
<title>Insert title here</title> 8 </head> 9 <body> 10 <form action="springmvc/fileUpload" method="POST" enctype="multipart/form-data"> 11 <input type="file" name="img"></br> 12 <input type="submit" value="上傳"> 13 </form> 14 </body> 15
</html>
 1 package com.bjsxt.handlers;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import org.springframework.context.annotation.Scope;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.multipart.MultipartFile;
10 import org.springframework.web.servlet.ModelAndView; 11 12 //後端控制器 13 @Controller //該註解表將當前類交給spring容器管理 14 @Scope("prototype") 15 @RequestMapping("/springmvc") //該註解起到限定範圍的作用 16 public class MyController{ 17 @RequestMapping("/fileUpload") 18 public String fileUpload(MultipartFile img){ 19 String path="d:/"; 20 String fileName = img.getOriginalFilename(); 21 File file = new File(path, fileName ); 22 try { 23 img.transferTo(file); 24 } catch (IllegalStateException e) { 25 e.printStackTrace(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 30 return "welcome"; 31 } 32 33 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context" 
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:aop="http://www.springframework.org/schema/aop"
 8     xsi:schemaLocation="
 9         http://www.springframework.org/schema/beans 
10         http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/tx 
12         http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         http://www.springframework.org/schema/mvc
16         http://www.springframework.org/schema/mvc/spring-mvc.xsd
17         http://www.springframework.org/schema/context 
18         http://www.springframework.org/schema/context/spring-context.xsd">
19     <!-- 註冊組件掃描器 -->
20     <context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
21     <!-- 註冊註解驅動 -->
22     <mvc:annotation-driven/>
23     <!-- 註冊視圖解析器 -->
24     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25         <property name="prefix" value="/jsp/"></property>
26         <property name="suffix" value=".jsp"></property>
27     </bean>
28     <!-- 註冊文件上傳解析器 -->
29     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
30         <property name="defaultEncoding" value="utf-8"></property>
31     </bean>
32         
33     <!-- 靜態資源無法訪問第二種解決方案 -->
34     <!-- <mvc:default-servlet-handler/> -->
35     <!-- 靜態資源無法訪問第三種解決方案 -->
36     <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
37 </beans>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>springmvc-01-primary</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.jsp</welcome-file>
 6   </welcome-file-list>
 7   <!-- 靜態資源無法訪問第一種解決方案 -->
 8   <!-- 
 9   <servlet-mapping>
10       <servlet-name>default</servlet-name>
11       <url-pattern>*.png</url-pattern>
12   </servlet-mapping>
13   <servlet-mapping>
14       <servlet-name>default</servlet-name>
15       <url-pattern>*.js</url-pattern>
16   </servlet-mapping>
17   <servlet-mapping>
18       <servlet-name>default</servlet-name>
19       <url-pattern>*.css</url-pattern>
20   </servlet-mapping>
21    -->
22   <!-- 註冊springmvc前端控制器(中央調度器) -->
23   <servlet>
24       <servlet-name>springmvc</servlet-name>
25       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
26       <!-- 指定springmvc配置文件的路徑以及名稱 -->
27       <init-param>
28           <param-name>contextConfigLocation</param-name>
29           <param-value>classpath:springmvc.xml</param-value>
30       </init-param>
31   </servlet>
32   <servlet-mapping>
33       <servlet-name>springmvc</servlet-name>
34       <url-pattern>/</url-pattern>
35   </servlet-mapping>
36 </web-app>

spring之文件上傳