Uploading files(Servlet/JSP)
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.
However, separating and storing file and string elements from the data provided by InputStream is a very complicated task, so DEXTUploadJK provides the FileUpload class to make it convenient to implement business logic by separating data from InputStream.
// dextuploadjk.support.common.FileUpload class is a class that processes uploaded files.
FileUpload fileUpload = new FileUpload(request, environment);
// Save all file data parts from the data transmitted from the client as a temporary file.
// The temporary storage location is set with the Environment#setTempRepository method.
fileUpload.prepare();
// name Obtains a FormItem object with information about the form passed by name.
FormItem formItem = fileUpload.getFormItem("name");
// Obtain a FileItem object with temporary file information named file1.
FileItem fileItem = fileUpload.getFileItem("file1");
// Save the file to the default directory path (consistent with moving temporary files to the destination).
fileItem.save();
The FileUpload object is created by receiving the HttpServletRequest object, and uses the prepare method to internally separate the file and other form elements. After the prepare method call is completed, string and file elements can be handled using the getFormItem or getFileItem method.
- 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 prepare method 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.
If you want to change the information about the directory where files are written, you can register the directory information in the Environment object and pass it along when creating the FileUpload object.
Environment env = new Environment(); // Location where temporary files will be stored env.setTempRepository("/home/user/temp"); // Location where the file should be ultimately saved env.setDefaultRepository("/home/user/files"); ... // Provide option values using the second parameter. FileUpload fileUpload = new FileUpload(request, env); // Alternatively, you can set it using the setEnvironment method. FileUpload fileUpload = new FileUpload(request); dextnj.setEnvironment(env); - 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 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.
- Get client file elements
-
DEXTUploadJK supports various methods to obtain file field elements of forms defined on the client.
<form action="..." method="post" enctype="multipart/form-data"> ... <!-- When individual names are given --> <input type="file" name="file1"/> <input type="file" name="file2"/> <input type="file" name="file3"/> ... <!-- If identical names are given --> <input type="file" name="files"/> <input type="file" name="files"/> <input type="file" name="files"/> </form>
FileUpload fileUpload = new FileUpload(request, environment); fileUpload.prepare(); // Get the first file element. FileItem item = fileUpload.getFileItem(0); // Get an object with the name file1. FileItem item = fileUpload.getFileItem("file1"); // Get an object with the name file2. FileItem item = fileUpload.getFileItem("file2"); // Get the object with the name file3. FileItem item = fileUpload.getFileItem("file3"); // Get the first element with the name files. FileItem item = fileUpload.getFileItem("files"); // Get all objects (files, files, files) with the name files. List<FileItem> items = fileUpload.getFileItems("files"); // Get all file elements (file1, file2, file3, files, files, files). List<FileItem> items = fileUpload.getFileItems();