springMVC文件上传

来源:互联网 发布:遭遇网络贷款诈骗 编辑:程序博客网 时间:2024/06/06 10:40

<%@ 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 method="post" action="fileUpload.do"enctype="multipart/form-data">

 

<input  type="file" name="files">

<input  type="file" name="files">

<input  type="file" name="files">

 

<input type="submit" value="提交">

</form>

Spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans 

    xmlns="http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xmlns:tx="http://www.springframework.org/schema/tx" 

    xmlns:context="http://www.springframework.org/schema/context"   

    xmlns:mvc="http://www.springframework.org/schema/mvc"   

    xsi:schemaLocation="http://www.springframework.org/schema/beans  

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

    http://www.springframework.org/schema/tx  

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

    http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/context/spring-context-3.0.xsd 

    http://www.springframework.org/schema/mvc 

    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   

   

 

 

 

<bean id="internalResourceView"class="org.springframework.web.servlet.view.InternalResourceViewResolver">

   <property name="prefix" value="/"></property>

   <property name="suffix" value=".jsp"></property>

</bean>

 

 

<mvc:annotation-driven/>

 

<context:component-scan base-package="springmvc"/>

 

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> 

    <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

        <property name="defaultEncoding" value="UTF-8"/> 

        <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> 

        <property name="maxUploadSize" value="200000"/> 

    </bean> 

 

 

 

   

</beans>

控制器的编写

@RequestMapping("fileUpload")

   public void fileUpload(HttpServletRequest request,@RequestParam MultipartFile[] files) throwsIllegalStateException, IOException{

     

     

      System.out.println("file uploading begin ...");

     

      for (MultipartFile file : files) {

        System.out.println(file.getName());

        System.out.println(file.getOriginalFilename());

        System.out.println(file.getSize());

        System.out.println(file.getContentType());

       

       

        String path = request.getServletContext().getRealPath("upload");

       

        file.transferTo(newFile(path+File.separator+file.getOriginalFilename()));

       

       

      }

     

     

     

     

      System.out.println("file uploading end ...");

     

     

     

     

   }

  

 

0 0