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

springmvc复习笔记----文件上传multipartResolver

时间:2017-04-01 15:22:09      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:eal   round   pos   扫描   alt   lap   etc   bind   height   

结构

技术分享                                            技术分享


web.xml

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>springmvc01</display-name>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>characterEncodingfilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
  <filter-name>characterEncodingfilter</filter-name>
  <url-pattern>*</url-pattern>
  </filter-mapping>
</web-app>
View Code

spring-mvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--   自动扫描加载注解的包 -->
<context:component-scan base-package="com.ij34.bean"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp" ></property>
</bean>
<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="UTF-8"/>  
        <property name="maxUploadSize" value="100000000000000"/>

    </bean>
</beans>

index.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<th colspan="2">上传</th>
</tr>
<tr>
<td>
<input type="file" name="file">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>

success.jsp

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
upload success
</body>
</html>
View Code

 


 

Testhello.java

 

package com.ij34.bean;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class Testhello {

  @RequestMapping("/upload")   //@RequestMapping  请求映射
  public String upload(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception {
    String path=request.getServletContext().getRealPath("/");
    System.out.println(path);
    for(MultipartFile file:files){
        file.transferTo(new File(path+"upload/"+file.getOriginalFilename()));
    }
    return "forward:success.jsp";
  
  }
}

 

 


 

结果

技术分享技术分享

技术分享

技术分享

 

springmvc复习笔记----文件上传multipartResolver

标签:eal   round   pos   扫描   alt   lap   etc   bind   height   

原文地址:http://www.cnblogs.com/tk55/p/6655862.html

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