Data in the form of Multipart in SpringMVC (file upload)

tags: Java advanced code example  java  maven  intellij-idea

1. The jar package required for file upload

Raw servlet Processing uploaded data data, springmvc ⼜ ⼜ ⼜ serlvet Packaging,
RequiredjarBag
<!-⽂ ⽂ Upload JAR coordinates->
<dependency>
 <groupId>commons-fileupload</groupId>
 <artifactId>commons-fileupload</artifactId>
 <version>1.3.1</version>
</dependency>

two. Configuration piece upload parser

<!-Configure   <uploaded parser, ID is a fixed MultipartResolver->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!-Set up upload  , unit bytes->
 <property name="maxUploadSize" value="1000000000"/>
</bean>

three.front endForm

<%--
 1 method="post"
 2 enctype="multipart/form-data"
 3 type="file"
--%> <form method="post" enctype="multipart/form-data" action="/demo/upload">
 <input type="file" name="uploadFile"/>
   <input type = "submit" value = "upload"/>
</form>

Four. Background receivingHandler

 /**
           * File Upload 
     * @return
     */
    @RequestMapping(value = "/upload")
    public ModelAndView upload(MultipartFile uploadFile,HttpSession session) throws IOException {

                 // Process upload files
                 // Renamed, formerly known as 123.jpg, get the suffix
                 String OriginalFilename = UploadFile.getOriginalFilename (); // The original name
                 // Extended name jpg
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        String newName = UUID.randomUUID().toString() + "." + ext;

                 // Storage, to store to the specified folder,/uploads/yyyy-mm-dd, consider the situation of too many files according to the date, generate a subfolder folder
                 // Get the real disk path of "/Uploads" and store in the RealPath string variable
        String realPath = session.getServletContext().getRealPath("/uploads");
                 // Get the current system time and generate in the format of "yyyy-mm-dd", and store in the Datepath string variable
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File folder = new File(realPath + "/" + datePath);

        if(!folder.exists()) {
            folder.mkdirs();
        }


                 // Storage file to the directory
        uploadFile.transferTo(new File(folder,newName));


                 // Todo File Disk Disk to update to the database field

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

Intelligent Recommendation

JSP file upload enctype="multipart/form-data"

problem When uploading files, the form needs to be set enctype="multipart/form-data", that is: At this time, if there are other types of content in the form in addition to the file, the requ...

Upload file multipart/form-data description

Upload file multipart form-data boundary description Meaning ENCTYPE="multipart/form-data" Description: Upload files through http protocol rfc1867 protocol overview, the client sends content...

Multipart/form-data and httpclient file upload

Written in front: The content discussed in this article is based on the Java technology stack. File upload is a relatively common functional requirement whether it is developed in the traditional HTML...

GO transit file Upload Multipart/Form-Data;

A slightly large project may have a transit project. Today I will say that the files encountered when uploading the transfer of the transit. If you use the post request directly as a transit, you will...

More Recommendation

Boost-based Multipart/Form-Data upload file

Warehouse code: https://gitee.com/liudegui/http_upload Based on Boost.asio packaging HTTP upload library, supports ordinary Post and Multipart/Form-Data Need to use C ++ 11 to compile head File...

multipart/form-data; Boundary upload file

Prospects: need to make data in batches at work, and this data is uploaded by uploading XLSX file. As shown in the figure below, I want to automatically call to generate batch data with python   ...

JMeter file upload interface multipart/form-data

Common content-types for http requests are divided into three types: application/json, x-www-form-urlencoded, multipart/form-data. multipart/form-data is mainly used in scenarios where files need to b...

Multipart Form File Upload

Why can't 80% of the code farmers can't do architects? >>>   Major class: Test code: Funded in: https: //my.oschina.net/gxchan/blog/464849...

FormData form file upload form set enctype="multipart/form-data"

The role of enctype=multipart/form-data in form Enctype="multipart/form-data" is used to upload images in the form. such as: The meaning of enctype="multipart/form-data" in the for...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top