Spring framework learning (8) spring mvc upload and download

The content comes from: spring mvc upload and download

 

The following example: 
page: 
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">

  <servlet>
    <servlet-name>etoak</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>etoak</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

etoakk-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- 
        request parser
            Character Request Parser HandlerMapping
            Byte request resolver MultipartResolver
            CommonsMultipartResolver
                This parser is used when the server side uses commons-fileupload to handle upload requests
            StandardServletMultipartResolver
                This parser is used when the server segment uses smartupload to handle upload requests

    Note: When registering an upload request resolver, the resolver's name (id) value must be: multipartResolver
            Interface name - first letter lowercase
     -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

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

</beans>

FileController.java:

@Controller
public class FileController {

    @RequestMapping("/upload")
    public String upload(User user , 
            HttpServletRequest request)throws Exception{

        MultipartFile myfile = user.getMyfile();

        String filename = myfile.getOriginalFilename();
        String contentType = myfile.getContentType();
        long size = myfile.getSize();

        // Get an input stream from the uploaded file 
        InputStream is = myfile.getInputStream();

        // 定位到file目录  request.session.ServletContext.getRealPath("/file")
        String path = request.getSession().getServletContext().getRealPath("/file");
        String newFilename = new UUIDGenerator().generate().toString()+
            filename.substring(filename.lastIndexOf("."));
        File file = new File(path+"/"+newFilename);

        OutputStream os = new FileOutputStream(file);

        int len;
        byte[] data = new byte[1024];
        while((len=is.read(data))!=-1)
            os.write(data, 0, len);
        is.close();
        os.close();

        return "redirect:success.jsp";

    }

    @RequestMapping("/download")
    public void download(String filename,HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println( "filename" + filename);
        String path = request.getSession().getServletContext().getRealPath("/file");
        File file = new File(path+"/"+filename);

        response.setContentType("multipart/form-data");
        response.setHeader("content-Disposition", "attachment;filename="+filename);
        InputStream is = new FileInputStream(file);
        OutputStream os = response.getOutputStream();
        int len;
        byte[] data = new byte[1024];
        while((len=is.read(data))!=-1)
            os.write(data, 0, len);
        is.close();
        os.close();
    }
}

User.java:

package com.etoak.bean;

import org.springframework.web.multipart.MultipartFile;

public class User {

    /** myfile file
     * How spring-mvc encapsulates file type objects
     *
     * struts1 - FormFile
     * struts2 - File String String
     * spring-mvc  -  MultipartFile接口 、 CommonsMultipartFile
     */
    private MultipartFile myfile;

    public MultipartFile getMyfile() {
        return myfile;
    }

    public void setMyfile(MultipartFile myfile) {
        this.myfile = myfile;
    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073438&siteId=291194637