File upload and download files to achieve

A: file upload    

  Most of the documents are submitted to the backend server upload through the form form, therefore, to achieve upload function, you need to provide a file upload form, and the form must meet the following three conditions:

1.form form method attribute is set to POST;

2.form property to form enctype multipart / form-data;

3. Provide <input type = "file" name = "filename" /> file upload input box.

 

File upload form examples are as follows: (multiple HTML5 attribute is a new attribute, enabling multi-file upload)

<from action "uploadUrl" method = "post" enctype = "multipart/form-data">
   <input type = "file" name = "filename" multiple = "multiple"/>
   <input type = "submit" value = "文件上传"/>
   </from>

     When the form of the form attribute enctype multipart / form-data, the browser will use the binary stream processed form data, the server will upload the file analysis processing request. Spring MVC achieve file upload functionality through MultipartResolver. MultipartResolver achieve file upload function. MultipartResolver when an interface object, you need to complete the file upload work through its implementation class CommonsMultipartResolver.

      By <property> encoding formats and arranged to allow upload file size, a file can be configured as follows CommonsMultipartResolver parser class attributes <property> element.

       1.maxUploadSize: The maximum length of the uploaded file (in bytes);

       2.maxInMemorySize: the maximum size of the cache;

       3.defaultEncoding: The default encoding format;

       4.resolveLazily: postpone document analysis, in order to capture file an exception in the Controller.

Note: Because the class that implements internal CommonsMultipartResolver MultipartResolver interface is a reference to a string multipartResolver get the complete implementation class object and file parsing, you must specify the id Bean when configuring CommonsMultipartResolver to multipartResolver.

       Since CommonsMultipartResolver is inside Spring MVC by Apache Commons FileUpload technology, so Spring MVC file upload component also relies on Apache Commons FileUpload, that need to import the relevant JAR package supports file uploads.

 

Two: implementation file download

       File download is to download the file to the file server on the local machine, in Spring MVC environment, file downloading can be divided into the following two steps:

1. On the client file download pages using a hyperlink, href attribute of the link to download the file to specify the background of the method and the file name (you need to add a name "1.jpg" file in the file download directory)

<a href = "${pageContext.request.contextPath}/downloads?filename = 1.jpg">
文件下载
</a>

      Use the type of object in the background ResponseEntity Spring MVC provides a complete file download, using it can easily define a return HttpHeaders HttpStatus objects and object, these two objects are set to complete the required configuration information when downloading files.

 

@RequestMapping("/download")
public ResponseEntity<byte[]>fileDownload(HttpServletRequest request,String filename)throws Exception{
	String path = request.getServletContext().getRealPath("/upload/");
	File file = new File(path + File.separator + filename);
	HttpHeaders headers = new HttpHeaders();
	headers.setContentDispositionFormData("attachment", filename);
	headers.setContentType(Media Type.APPLICATION_OCTET_STREAM);
	return new ResponseEntity<byte[]>(fileUtils.readFileToByteArray(file),headers,HttpStatus.OK);
}

      In response to the header information represents the Interner Media Type Media Type (i.e., media type), also known as a MIME type, Media Type.APPLICATION_OCTET_STREAM value of application / octet-stream, i.e., the download data in a binary stream.

      Is a state representative of the type of HttpStatus Http protocol, examples HttpStatus.OK representation 200, i.e., the server has successfully processed the request.

发布了376 篇原创文章 · 获赞 172 · 访问量 9万+

Guess you like

Origin blog.csdn.net/Eider1998/article/details/104217328