Uploading files (Spring Web Framework)
DEXTUploadNJ is a server component that handles multi-part (multipart/form-data) data that is sent to the server under HTTP protocol based RFC 1867 protocol.
Generally, in order to transfer a file in HTML format, set the enctype attribute of the form element to "multipart/form-data" and use the input element whose type attribute is "file".
<form action="..." method="post" enctype="multipart/form-data"> ... <input type="text" name="name" value="DEXTUploadNJ"/> ... <input type="file" name="file1"/> ... </form>
After the Web application server(WAS) receives the transferred data, it is wrapped in the HttpServletRequest object, and the data can be accessed by using the getInputStream method.
Unlike the general JSP/Servlet environment, the Spring Web Framework automatically separates multipart data using the bean of the CommonMultipartResovler class. DEXTUploadNJ, like the CommonMultipartResovler class, automatically separates data and provides the DEXTUploadNJMultipartResolver class for convenience in implementing business logic.
<bean id="multipartResolver" class="devpia.dextuploadnj.support.spring.DEXTUploadNJMultipartResolver"> <property name="environment" ref="environment"/> </bean>
Since DEXTUploadNJMultipartResolveris exchanging an existing CommonMultipartResolver, you should not declare duplicately on the same URL mapping. If the dispatcher servlet is mapped to the *.do path and the dispatcher setting DEXTUploadNJMultipartResolver is declared, you should not declare CommonMultipartResovler to work together with the *.do mapping.
- Temporary directory and default directory
-
DEXTUploadNJ records the information of the uploaded file in a temporary directory rather than recording it in memory. This process is performed by executing the resolveMultipart method of the DEXTUploadNJMultipartResolver object at the time of execution and recording(moving or copying) the file to the location where the actual save should be done by the method of the mapped controller using the save, saveAs method of the FileItem interface.
DEXTUploadNJMultipartResolver do configuration with a Environment object. Since the Environment object needs to be set to the environment attribute of DEXTUploadNJMultipartResolver, all settings are determined by setting of the dispatcher servlet (generally XML).
<bean id="environment" class="devpia.dextuploadnj.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="devpia.dextuploadnj.support.spring.DEXTUploadNJMultipartResolver"> <property name="environment" ref="environment"/> </bean>
- Format of method mapped to controller
-
DEXTUploadNJMultipartResolver object processes the role of separating the form data and generating a temporary file. After this, the work to move to the place where the temporary file is actually saved and copy it depends on the service to be implemented, so it must be handled with the method mapped to the controller.
DEXTUploadNJMultipartResolver obtains file information using the org.springframework.web.multipart.MultipartFile parameter object in the controller mapping method as well as the file upload processing method using CommonsMultipartResolver. MultipartFile is implemented by the existing Spring Framework MVC by the org.springframework.web.multipart.commons.CommonsMultipartFile class, but using DEXTUploadNJMultipartResolver will implement DEXTUploadNJMultipartFile instead of CommonsMultipartFile.
@RequestMapping(value = "/common.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 DEXTUploadNJMultipartFile class. DEXTUploadNJMultipartFile item = (DEXTUploadNJMultipartFile)file1; ... return "view name and redirection processing"; } // If multiple files are received at once, you can use an array. @RequestMapping(value = "/common.do", method = RequestMethod.POST) public String upload(@RequestParam(value = "files") MultipartFile[] files) throws IOException { ... } - Change the file storage location and name
-
By using the save and saveAs method of the FileItem interface, the file is recorded in the default position. If you save the file in another path, you can use the targetDirectoryPath parameter and targetFilename parameter of the saveAs method to change the position or change the file name.
// When the file is finally saved at "/home/user/files" and the file name is "photo.jpg", // record to the "/home/user/files/photo.jpg" path. fileItem.save(); // record to the "/home/user/other/files/photo.jpg" path. fileItem.save("/home/user/other/files"); // record to the "/home/user/files/photo_new.jpg" path. fileItem.saveAs("photo_new.jpg"); // record to the "/home/user/other/files/photo_new.jpg" path. fileItem.saveAs("/home/user/other/files", "photo_new.jpg");If there is a file with the same name in the place to record, a specific suffix(file number) is added after the file name.