springMVC文件上传

参考的地址:http://www.tuicool.com/articles/nMVjaiF

1.需要使用的jar、

commons-fileupload.jar与commons-io-1.4.jar二个文件 

其中commons-io已经被当成依赖包导进去了,不需要另外导,所用的pom.xml完整内容为:

<!-- 这个配置只能在tomcat7上运行,tomcat6和tomcat8会报错 -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tansun</groupId>
    <artifactId>hadoopClientWeb</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>hadoopClientWeb Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <junit.version>3.8.1</junit.version>
        <cdh.hadoop.version>2.6.0</cdh.hadoop.version>
        <cdh.hbase.version>1.0.0</cdh.hbase.version>
        <elasticsearch.version>1.5.0</elasticsearch.version>
        <springmvc.version>4.0.5.RELEASE</springmvc.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

     

        <!-- springMVC的jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springmvc.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springmvc.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springmvc.version}</version>
        </dependency>
        <!-- jsp编译的jar包 -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <!-- 文件上传到后台的包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>hadoopClientWeb</finalName>
    </build>
</project>

2、springmvc配置

增加以下内容,

    <!-- 文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为10MB -->
        <property name="maxUploadSize">
            <value>10000000</value>
        </property>
    </bean>

spring-servlet.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">

    <!-- 把标记了@Controller注解的类转换为bean -->
    <context:component-scan base-package="action"></context:component-scan>
    <!-- 文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为10MB -->
        <property name="maxUploadSize">
            <value>10000000</value>
        </property>
    </bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>





</beans>

3、上传页面

在form里要加上 enctype="multipart/form-data",form里面file的input,即如下:

<form id="form1" method="post" enctype="multipart/form-data">

<input id="file" name="file" type="file" class="inputclass"></input> 
<input type="button" class="btn_public" value="上传文件" onclick="uploadFile()"></input> </form>

上面的uploadFile()为:

<script type="text/javascript">
    function uploadFile() {
            var fileSelect = document.getElementById("file");
            var localName = fileSelect.value;
            if (localName == "") {
                alert("请先选择要上传的文件");
            } else {
                var form = document.getElementById("form1");
                form.action = "hdfsUploadFile.do"
                form.submit();
            }
        }
</script>

后台的java为:

@RequestMapping("hdfsUploadFile.do")
    public String upload(@RequestParam("file") MultipartFile file, Model model,
            HttpServletRequest request) {
String name = file.getOriginalFilename();
        String view = "toHdfsTable";
      try {
            // 获取文件 存储位置
            String realPath = request.getSession().getServletContext().getRealPath("/uploadFile");
            uploadServer(realPath,file);
            
        }  catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         
        }
        
        
        return view;
}

    private void uploadServer(String realPath,MultipartFile file) throws IllegalStateException, IOException{
        File pathFile = new File(realPath);

        if (!pathFile.exists()) {
            // 文件夹不存 创建文件
            pathFile.mkdirs();
        }

//        System.out.println("文件类型:" + file.getContentType());
//        System.out.println("文件名称:" + file.getOriginalFilename());
//        System.out.println("文件大小:" + file.getSize());
//        System.out.println(".................................................");
        // 将文件copy上传到服务器
        file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
    }

 

你可能感兴趣的:(springMVC文件上传)