Spring Uploading Files

Source: Internet
Author: User

1. Add code in Servlet-dispatcher.xml

< Bean id="Multipartresolver"class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "/>


You can also add related properties to your needs< Property name="Maxuploadsize" value="2097152"></ Property>   
2, adding dependent jar files

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId>< version>2.4</version></dependency><dependency><groupid>commons-fileupload</ groupid><artifactid>commons-fileupload</artifactid><version>1.3.1</version></ Dependency>

3, Write Uploadcontroller
    @RequestMapping (value= "/upload", method=requestmethod.post) public      String processupload (@RequestParam ("name") String name,     @RequestParam ("file") Multipartfile file) throws IOException {        log.info ("file '" + File.getoriginalfilename () + "' uploaded successfully");      if (!file.isempty ()) {            try {                byte[] bytes = File.getbytes ();                Bufferedoutputstream stream =                        new Bufferedoutputstream (new FileOutputStream (name));                Stream.Write (bytes);                Stream.Close ();                                                                Return "You successfully uploaded" + name + "!";            } catch (Exception e) {                return "failed to upload" + name + "= +" + e.getmessage ();            }        } else {            return ' failed to upload ' + name + ' because the file was empty. ';        }    }  
4, using restclient test upload
Background prompt:

[2015-08-18 11/:36/:12] INFO Com.kaishuhezi.api.hardware.log.controller.LogController (line/:40)-file ' 78a0f7dcjw1e1bvyuzt1jj.jpg ' Uploaded successfully


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Uploading Files

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.