File Upload: Commonsmultipartresolver

Source: Internet
Author: User

I. INTRODUCTION

Commonsmultipartresolver is based on the Apache Commons fileupload to implement the file upload function, the main role is to configure file upload some properties.

Two. Configuration

1) rely on Apache Commons FileUpload, so maven Pom file added dependency;

<dependency><groupid>commons-fileupload</groupid><artifactid>commons-fileupload</ Artifactid><version>1.3.3</version></dependency>

2) applicationcontext.xml Configuration bean

    <!--file Upload parser--    <bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">        <property name=" Maxuploadsize " Value= "209715200"/>        <property name= "maxinmemorysize" value= "40960"/>    </bean>

3) The source of the ID, view SPRINGMVC's core controller dispatcherservlet, there are some bean name constants defined

Lic class Dispatcherservlet extends Frameworkservlet {public    static final String multipart_resolver_bean_name = "
   
    multipartresolver";    public static final String locale_resolver_bean_name = "Localeresolver";    public static final String theme_resolver_bean_name = "Themeresolver";    public static final String handler_mapping_bean_name = "handlermapping";    public static final String handler_adapter_bean_name = "Handleradapter";    public static final String handler_exception_resolver_bean_name = "Handlerexceptionresolver";    public static final String request_to_view_name_translator_bean_name = "Viewnametranslator";    public static final String view_resolver_bean_name = "Viewresolver";    public static final String flash_map_manager_bean_name = "Flashmapmanager";
   
Three. Use

1) Process:

The client request must be multiparthttpservletrequest (a subclass of HttpServletRequest) Request---> Spring's Dispatcherservlet calls Method Checkmultipart () Validation---> Multipartresolver (some properties that are configured in the configuration file are verified: for example, the maximum value of a file)

2) After the test, the code to the file processing example

@RequestMapping ("/fileupload")
Public ajaxlist fileUpload (httpservletrequest request,httpservletresponse response) {
Multiparthttpservletrequest multirequest = (multiparthttpservletrequest) request;
Multipartfile file = Multirequest.getfile ("FileName");
if (file!=null) {
File LocalFile = new file ("d:/" +file.getoriginalfilename ());
try {
File.transferto (LocalFile); Writes the uploaded file to the file specified on the server
} catch (Exception e) {
E.printstacktrace ();
Return Ajaxlist.createerror ("server Exception");

}
return ajaxlist.createsuccess ("Upload succeeded");
}

 

  

File Upload: Commonsmultipartresolver

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.