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

springmvc_文件上传示例

时间:2015-10-15 21:58:38      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

  开始接触springmvc,有点期待,有点兴奋。单今天的一点小事差点让我崩溃了。在这记录下,不是记录什么技术点,纯粹是警告下自己,再小心不为过啊!

  做了个小例子:

  搭好springmvc的架子,导好相关的包(当让做文件上传commons-fileupload.jar这个肯定是必须要的)。

①配置好web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1">
    
    <!-- 配置 SpringMVC 的 DispatcherServlet -->
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <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:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 配置  HiddenHttpMethodFilter :把POST 請求转化为DELETE、PUT 请求-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>    
     <filter-mapping>
         <filter-name>HiddenHttpMethodFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
</web-app>

 

②配置好springmvc.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: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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.springmvc"></context:component-scan>

    <!-- 配置是图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 配置MultipartResover :用于文件上传 -->
    <bean id="multipartResover"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000"></property>
    </bean>
</beans>

 

③然后开始写jsp文件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 id="form" action="testFileUpload" method="Post" enctype="multipart/form-data">
File: <input type="file" name="file" />
Desc: <input type="text" name="desc" />
<
input type="submit" value="Submit" />
</
form>
</body>
</html>

④开始写Java代码:

package com.springmvc.test;

import java.io.IOException;
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 SpringMVCTest {
	
	@RequestMapping("/testFileUpload")
	public String testFileUpload(@RequestParam(value="desc",required=false) String desc,
			@RequestParam(value="file",required=false) MultipartFile file) throws IOException{
		System.out.println("desc: "+ desc);
		System.out.println("originalFilename: " + file.getOriginalFilename());
		System.out.println("InputStream: " +file.getInputStream());
		return "success";
	}
	

}

 相关的文件配好了,有个success.jsp就写了,随便写个页面就行。

开始跑吧,一跑就出错了nullpoint,查原因“System.out.println("originalFilename: " + file.getOriginalFilename());”空指针,就是说file为null,怎么会呢?

相关的配置和包都有啊,一步步看下来发现配置springmvc.xml文件时Id写错了,少了个“l”,id应该是“配置multipartResolver”,悲剧啊……所以写东西一定要小心啊,能copy的就尽量copy吧,不然稍微一不注意就悲催了……

springmvc_文件上传示例

标签:

原文地址:http://www.cnblogs.com/big-xuzhou/p/4883575.html

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