www.dextsolution.com
DEXTUPLOAD
JK
menu toggleProduct information > Uploading files(Spring Boot)

Uploading files(Spring Boot)

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.

Spring Boot automatically separates multipart data using StandardServletMultipartResolver. DEXTUploadJK provides the JKMultipartResolver class to conveniently implement business logic by automatically separating data like StandardServletMultipartResolver.

@Bean(name = "multipartResolver")
MultipartResolver createMultipartResolver(ServletContext context) {
    Environment env = new Environment();
    ...		
    JKMultipartResolver rs = new JKMultipartResolver();
    rs.setEnvironment(env);
    return rs;
}
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. Since the Environment object must be set as the environment property of JKMultipartResolver, it must be set at the time of creating the JKMultipartResolver object.

@Bean(name = "multipartResolver")
MultipartResolver createMultipartResolver(ServletContext context) {
    Environment env = new Environment();
    env.setTempRepository("/files/temp");
    env.setDefaultRepository("/files/store");
    env.setLicenseFilePath("/license/dextuploadjk.config");
    env.setAutoMakingDirectory(true);
    env.setMaxFileSize(1024 * 1024 * 20);
    env.setMaxTotalSize(1024 * 1024 * 50);
    env.setWhiteExtensions("jpg,gif,png,doc,xls,ppt,docx,xlsx,pptx,pdf,txt,zip,hwp,mp4");
    	
    JKMultipartResolver rs = new JKMultipartResolver();
    rs.setEnvironment(env);
    return rs;
}
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.

@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.