In Spring MVC (multi) file upload and download

File Upload

Step 1: Set JSP page form of property

The action value of the request address, method request of the representative embodiment, the form file upload request mode must be the POST;

enctype = "multipart / form-data " file upload form required values

<form action="/first/fileupload" method="post" enctype="multipart/form-data"><input type="file" name="file">
    <input type="file" name="file">
    <input type="submit" value="提交">
</form>

Step two: write controller method

The first parameter in the file method for the front submitted, parameter names and forms need to name the same, not the same as on the use of @RequestParam set

@Controller
@RequestMapping("/first")
public class FirstController {
  //文件上传
    @RequestMapping("/fileupload")
    public  String fileUpLoad(@RequestParam MultipartFile[] file, HttpSession session) throws IOException {
        Realpath String = session.getServletContext () the getRealPath ( "/ img." ); // Get the full path to the target directory img directory 
    // retrieve each cycle of a file object
for (a MultipartFile Item: File) {
       // Get original file name String fileName
= item.getOriginalFilename(); File files=new File(realPath,fileName); System.out.println (Files);
        . // write files to the File lowest scheduling disk write () method item.transferTo(files); }
return "welcome"; } }

Step Three: Configure in xml file upload parser

bean id values must be multipartResolver

    <! - File Upload parser -> 
    < bean the above mentioned id = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > 
        <! - Character Set file upload -> 
        < Property name = "defaultEncoding" value = "UTF-. 8" > </ Property > 
        <-! total size of the file -> 
        < Property name = "maxUploadSize" value = "5 billion" > </ Property > 
        <-! single file size -> 
        <property name="maxUploadSizePerFile" value="50000000"></property>
    </bean>

 

document dowload

  // File Download 
    @ RequestMapping ( "/ the FileDownload" )
     public ResponseEntity < byte []> DownLoad () throws IOException {
         // prepare documents 
        File File = new new File ( "full path to the file" );
         // Chinese file name garbled solve (Chinese name settings file)
         // getBytes () needs to throw or catch exceptions 
        String filename = new new String ( "Download file .txt" .getBytes ( "UTF-8"), "ISO8859-1" );
         // setting response header 
        HttpHeaders headers = new new HttpHeaders ();
        headers.setContentDispositionFormData ( "Attachment" , filename);
         // set the content type STREAM stream 
        headers.setContentType (MediaType.APPLICATION_OCTET_STREAM);
         // readFileToByteArray () throws or catch exceptions require 
        return  new new ResponseEntity < byte []> (FileUtils. readFileToByteArray (file), headers, HttpStatus.OK );
    }

 

Guess you like

Origin www.cnblogs.com/yjc1605961523/p/11834375.html