SpringMVC uploading and downloading

File Upload

       Overview: Most of the documents are uploaded through a form submitted to the backend server form, therefore, to achieve file upload function, you need to provide a file upload form, but the form requires the following three conditions:

       1, form method attribute is set to form post

       2, enctype property sheet to form multipart / form-data

       3, provided <input type = "file" name = "filename" /> input box file upload

       Example code:

              <form action="后台路径" method="post" enctype="multipart/form-data" method="post">

                     <input type="file" name="fileName" />

                     <input type="submit" value="提交"/>

              </form>

      

SpringMVC in to provide two jar package upload:

              commons-fileupload-1.3.2.jar

              commons-io.2.5.jar

 

       Example code:

      

 

       jsp Code:

 

Add the following code in the configuration file:

 

Controller write the code:

@RequestMapping("/fileUpload")

       public String handlerFormUpload(@RequestParam("name") String name,

                     @RequestParam("uploadfile") List<MultipartFile> uploadfile,

                     HttpServletRequest request) {

 

              // determine whether the uploaded file exists

              if (!uploadfile.isEmpty() && uploadfile.size() > 0) {

                     // loop output file upload

                     for (MultipartFile file : uploadfile) {

                            // get the original name of the uploaded file

                            String yuanshiFileName = file.getOriginalFilename();

                            // set the address directory to save the uploaded file

                            String driPath = request.getServletContext().getRealPath(

                                          "/upload/");

                            File filePath = new File(driPath);

                            // If the address to save the file does not exist, create a directory

                            if (!filePath.exists()) {

                                   filePath.mkdirs();

                            }

                            // use the file name to rename uploaded UUID

                            String newFilename = name + "_" + UUID.randomUUID() + "_"

                                          + YuanshiFileName;

                            try {

                                   file.transferTo(new File(driPath + newFilename));

                            } catch (Exception e) {

                                   // TODO Auto-generated catch block

                                   e.printStackTrace ();

                                   return "error";

                            }

                     }

                     return "success";

              } else {

                     return "error";

              }

 

       }

 

document dowload

 

 

Published 40 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sj_1993/article/details/105351047