码迷,mamicode.com
首页 > 编程语言 > 详细

spring之文件上传

时间:2018-12-29 23:24:16      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:扫描   common   tran   list   try   default   tac   oca   div   

 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之文件上传

标签:扫描   common   tran   list   try   default   tac   oca   div   

原文地址:https://www.cnblogs.com/wq-9/p/10198002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!