One of the more common features when developing Web applications is to allow users to upload local files to the server using multipart requests, which is one of the advantages of Grails's rugged cornerstone--spring MVC. Spring extends the HttpServletRequest interface of the servlet API so that it can handle file uploads well. The expanded interface is named Org.springframework.web.multipart.MultipartHttpServletRequest, and its contents are shown in Listing 7-31.
Listing 7-31 Org.springframework.web.multipart.MultipartHttpServletRequest Interface
Interface Multiparthttpservletrequest extends HttpServletRequest {
Public Multipartfile getFile (String name);
Public Map getfilemap ();
Public Iterator getfilenames ();
}
As the listing shows, the Multiparthttpservletrequest interface simply expands the default HttpServletRequest interface and provides some way to handle the request file.
7.10.1 using multipart requests
In fact, if a multipart request is found, it indicates that there is a request object in the controller instance that implements the multipart HttpServletRequest interface. We can use the method shown in listing 7-31 to access the upload file in the multipart request, but before processing the upload file, take a look at the contents of the upload form, as shown in Listing 7-32.
Listing 7-32 Sample Upload Form
The bold display is the part that needs attention, in fact an upload form only needs to meet the following two points.
The property value of the L Enctype property is set to Multipart/form-data.
The property value of the Type property of input is set to file.
In the previous example, the value of the attribute type in the label is file, and the value of the Name property is MyFile, and the Name property value is required. is because the value of the Name property is required when using the GetFile method of the interface multiparthttpservletrequest. For example, in Listing 7-33, the upload operation in the code reads the upload file from the request.
Listing 7-33 reading the upload file
def upload = {
def file = Request.getfile (' MyFile ')
Process the file
}
Note The GetFile method does not return an instance of Java.io.File, but instead returns Org.springframework.web. Multipart. For an example of Multipartfile, refer to listing 7-34 for more information about Org.springframework.web.multipart.MultipartFile. If the file is not found in the request, the GetFile method returns NULL.
Listing 7-34 Org.springframework.web.multipart.MultipartFile Interface
Interface Multipartfile {
Public byte[] GetBytes ();
Public String getcontenttype ();
Public Java.io.InputStream getInputStream ();
Public String getName ();
Public String getoriginalfilename ();
Public long getsize ();
public boolean isEmpty ();
public void TransferTo (Java.io.File dest);
}
A number of useful methods are defined in the Multipartfile interface.
L Use the GetSize () method to get the file length to determine the size of the file that is allowed to be uploaded.
L Use the IsEmpty () method to determine whether the uploaded file is an empty file, to determine whether to reject the empty file.
L Use the getInputStream () method to read the file as a Java.io.InputStream stream object.
L Use the getContentType () method to obtain the file type, which determines which file types are allowed to be uploaded.
L Use the Transferto (dest) method to write the uploaded file to the file specified on the server.
For example, if the uploaded file is not empty and the size is not less than 1024 bytes, then it can be implemented in the code in Listing 7-35.
Listing 7-35 File Upload example
def upload = {
def file = Request.getfile (' MyFile ')
if (file &&!file.empty && file.size < 1024) {
File.transferto (New Java.io.File ("/local/server/path/${file.name}"))
}
}
Processing file uploads using multipart requests