spring:文件上传

spring-sevlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="gb2312"/>
    <property name="maxUploadSize" value="500000"/>
    <property name="uploadTempDir" value="upload/tempfile"/>
</bean>
<bean id="uploadController" class="com.mxl.upload.UploadController">
     <property name="commandClass" value="com.mxl.upload.UploadBean"/>
    <property name="formView" value="upload"/>
    <property name="successView" value="success"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/upload.do">uploadController</prop>
        </props>
    </property>
</bean>
  <!-- 配置视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix"><!-- 前缀 -->
           <value>/</value>
        </property>
        <property name="suffix"><!-- 后缀 -->
           <value>.jsp</value>
        </property>
    </bean>
</beans>

web.mxl

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
                   
    <servlet>
    <servlet-name>spring</servlet-name>
    <!-- 配置DispatcherServlet -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup><!-- Servlet自动启动顺序号 -->
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>     <!-- 拦截所有以do结尾的请求 -->
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-servlet.xml
    </param-value>
</context-param>
                   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

UploadBean.java

package com.mxl.upload;
import org.springframework.web.multipart.MultipartFile;
public class UploadBean {
    private MultipartFile file;
    private String filename;
    public MultipartFile getFile() {
        return file;
    }
    public void setFile(MultipartFile file) {
        this.file = file;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
              
}

UploadController.java

package com.mxl.upload;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.util.WebUtils;
public class UploadController extends SimpleFormController {
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response,Object command,BindException errors) throws Exception{
        String path=WebUtils.getRealPath(request.getSession().getServletContext(),"/");
        UploadBean upBean=(UploadBean) command;
        upBean.setFilename(upBean.getFile().getOriginalFilename());
        upBean.getFile().transferTo(new File(path+"/upload/"+upBean.getFile().getOriginalFilename()));
        return new ModelAndView("success","bean",upBean);
    }
}

wKioL1L11OnDy_i8AAECQUhhqE0709.jpg

你可能感兴趣的:(spring)