springmvc--implementation file upload download

Source: Internet
Author: User

Reference:
http://blog.csdn.net/u012706811/article/details/51059419
http://blog.csdn.net/u012660464/article/details/53434331 One, file upload 1. Introduction of dependency Packs

Add the following to the Pom.xml file, introducing the commons-fileupload and Commons-io two packages.

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId> commons-fileupload</artifactid>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactid>commons-io</ artifactid>
    <version>2.3</version>
</dependency>
2.spring-mvc.xml
<!--file Upload-->
<bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">
    <!--maximum upload size (bytes)-->
    < Property Name= "Maxuploadsize" value= "1048576"/>
</bean>
3.fileupload.jsp

The form adds the enctype= "Multipart/form-data" attribute, which indicates that the file one or two stream is transmitted, otherwise the error is not correct.

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
4.success.jsp
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
5.index.jsp
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

6.FileController

    /**
     * To File upload page
     *
     * @return
    /@RequestMapping (value = "FileUpload", method = Requestmethod.get) Public
    String Tofileupload () {return
        "/file/fileupload";
    }

    /**
     * Single File Upload
     * *
     @param file
     * @return
    /@RequestMapping (value = "Upload", method = requestmethod.post) Public
    String FileUpload (@RequestParam ("file") Multipartfile file) {
        if (!file.isempty ()) {
            try {
                //save in F:\temp directory
                fileutils.copyinputstreamtofile (File.getinputstream (), New file ("f:\\ Temp ",
                        system.currenttimemillis () + file.getoriginalfilename ());
            } catch (IOException e) {
                e.printstacktrace ();
            }
        }
        Upload success, jump to Success page return
        "File/success";
    }

7. Multiple file uploads

    /** * to multiple File upload page * * @return/@RequestMapping (value = "Multifileupload", method = Requestmeth Od.
    Get) public String Tomultifileupload () {return "/file/multifileupload"; /** * Multiple File Upload * * @param multirequest * @return/@RequestMapping (value = "Multiupload ', method = Requestmethod.post ' public String multifileupload (Multiparthttpservletrequest multirequest) throws Ioexcep tion {iterator<string> filesnames = Multirequest.getfilenames ();//Get all file names while (Filesnames.hasnex
            T ()) {//iteration, operation of a single file String fileName =filesnames.next ();
            Multipartfile file = Multirequest.getfile (fileName);
                        if (!file.isempty ()) {Fileutils.copyinputstreamtofile (File.getinputstream ()), New file ("F:\\temp",
            System.currenttimemillis () + file.getoriginalfilename ()); }//upload succeeded, jump to Success page return "File/suCcess "; }

Second, File download 1.index.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
2.FileController

Now the realization of the function is to click "Download the specified file" is downloaded in the controller designated good files, has not implemented the uploaded file into the database, nor the implementation of the download database to save the file, the future slowly improve.

    /** *
     Designated File Download * *
     @return
     * @throws ioexception
    /@RequestMapping (value = "Download", produces = "application/octet-stream;charset=utf-8") public
    responseentity<byte[]> Download () throws IOException {
///                Specify file, must be absolute path file
        = new file ("F:\\temp\\1513489657107psb.jpg");                Download Browser response file name
        String dfilename = "1.jpg";                The following starts to set up httpheaders, make browser response download
        httpheaders headers = new Httpheaders ();                Set the response mode
        headers.setcontenttype (mediatype.application_octet_stream);                Set Response file
        headers.setcontentdispositionformdata ("attachment", dfilename);                write back the file in binary form return to
        new Responseentity<byte[]> (Fileutils.readfiletobytearray), headers, httpstatus.created);
    }

Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.