Uploading files(Spring MVC)
DEXTUploadJK is a server component that processes multipart (multipart/form-data) data transmitted to the server according to RFC1867 specifications based on the HTTP protocol.
Generally, to transmit a file in HTML, set the enctype attribute of the form element to 'multipart/form-data' and use an input element with the type attribute 'file' inside the form element.
<form action="..." method="post" enctype="multipart/form-data"> <input type="text" name="name" value="DEXTUploadJK"/> <input type="file" name="file1"/> </form>
After the transmitted data is received by the web application server (WAS), it is wrapped and delivered as an HttpServletRequest object, and the data can be accessed using the getInputStream method.
Unlike the Servlet/JSP environment, Spring MVC automatically separates multipart data using the bean of the CommonMultipartResovler class. DEXTUploadJK provides the JKMultipartResolver class to conveniently implement business logic by automatically separating data like the CommonMultipartResovler class.
<bean id="multipartResolver" class="dextuploadjk.support.spring.JKMultipartResolver"> <property name="environment" ref="environment"/> </bean>
Since JKMultipartResolver replaces the existing CommonMultipartResovler, two bean objects should not be declared redundantly on the same URL mapping line. If DispatcherServlet is mapped to the *.do path and JKMultipartResolver is declared in the corresponding context settings, CommonMultipartResolver should not be declared to operate together with *.do mapping.
- Temporary directory and default directory
-
DEXTUploadJK does not record uploaded file information in memory, but in a temporary directory. This process is performed when the resolveMultipart method of the JKMultipartResolver object is executed, and the task of recording (moving or copying) the file to the actual location where it should be saved is handled by calling the save and saveAs methods of the FileItem interface in the method of the mapped controller.
JKMultipartResolver can perform preset operations with Environment objects. Because the Environment object must be set as the environment property of JKMultipartResolver, all settings are determined in the context servlet configuration (usually XML).
<bean id="environment" class="dextuploadjk.engine.Environment"> <property name="tempRepository" value="/file/temp"/> <property name="defaultRepository" value="/file/attach"/> <property name="autoMakingDirectory" value="true"/> <property name="licenseFilePath" value="..."/> </bean> <bean id="multipartResolver" class="dextuploadjk.support.spring.JKMultipartResolver"> <property name="environment" ref="environment"/> </bean> - Method type mapped to controller
-
The JKMultipartResolver object only analyzes multipart data and creates temporary files. Afterwards, the task of moving or copying the temporary file to the actual storage location varies depending on the service to be implemented, so it must be handled in a method mapped to the controller.
JKMultipartResolver obtains file information through the org.springframework.web.multipart.MultipartFile parameter object in the controller's mapping method in the same way as the file upload processing method using CommonsMultipartResolver. MultipartFile is implemented by the org.springframework.web.multipart.commons.CommonsMultipartFile class in existing Spring MVC, but when JKMultipartResolver is used, JKMultipartFile, not CommonsMultipartFile, replaces it.
@RequestMapping(value = "/upload.do", method = RequestMethod.POST) public String upload(@RequestParam(value = "file1") MultipartFile file1) throws IOException { // Cast the file1 parameter object to the FileItem interface, FileItem item = (FileItem)file1; // Or cast to the JKMultipartFile class. JKMultipartFile item = (JKMultipartFile)file1; ... } // When receiving multiple files at once, you can use an array. @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public String upload(@RequestParam(value = "files") MultipartFile[] files) throws IOException { ... } - Change the location and name of a file
-
When using the save and saveAs methods of the FileItem interface, the file is written to the default location. If you want to record a file in a different path, you can change the location or change the file name using the targetDirectoryPath and targetFilename parameters of the save and saveAs methods.
// When the final location where the file will be saved is '/home/user/files' and the file name is 'photo.jpg', // Record in the path '/home/user/files/photo.jpg'. fileItem.save(); // Record in the path ‘/home/user/other/files/photo.jpg’. FileSaveOption option = new FileSaveOption(); option.setTargetDirectoryPath("/home/user/other/files"); fileItem.save(option); // Record in the path ‘/home/user/files/photo_new.jpg’. FileSaveOption option = new FileSaveOption(); option.setTargetFilename("photo_new.jpg"); fileItem.saveAs(option);If a file with the same name exists in the recording location, a (number) is added to the end of the file name.