of the java spring mvc file upload

Directory structure is as follows:

 

Note that the configuration files in src says the following configuration file, usually value that mvc.xml. If web.xml, then just say web.xml

1. File upload Precautions

Post form must be submitted, enctype must be set to "multipart / form-data",

Use commons-fileupload submission of documents, you need to add commons-fileupload and commons-io jar package.

2.Jsp page

<form action="file/upload.do" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/><input type="submit" value="上传"/>
</form>
</body>

3.Controller class

@Controller
//窄化 
@RequestMapping("/file")
public class UploadController {
    @RequestMapping("/upload.do")
    public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws Exception{
        String path=req.getServletContext().getRealPath("/upload");
        //获取文件名
        String fileName=file.getOriginalFilename();
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,fileName));
        byte[] buffer = new byte[400];
        int len=0;
        while((len=is.read(buffer))!=-1){
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
        return "redirect:/index.jsp";
    }
}

4. Add the configuration file multipartResolver

<!-- 文件上传配置 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"/>
    </bean>

 

 appendix:

Annex I, where the contents of attached files 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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <! - Development adapter annotation -> 
    <the bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
    <! - resolver configuration view -> 
    <the bean ID = "the viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver"> 
        <Property name = "the viewClass" value = "org.springframework.web.servlet.view.JstlView" /> 
           <-! name for the view in response plus prefix -> 
        <Property name = "prefix" value = "/ the WEB-INF / JSP /" /> 
        <-! response to the view name suffix -> 
        <Property name = "suffix" value =. " JSP "/> 
    </ bean> 
    <-! file uploads configuration ->
    <bean id="multipartResolver"class
        
        ="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000"/>
    </bean>
    <!-- 扫描注解类 -->
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>

 

 Here then attach the contents of web.xml file WebContent / WEB-INF / under

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>01springmvc_helloworld</display-name>
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 改变springmvc配置文件的路径及名称 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11278749.html
Recommended