Uploading files
DEXTUploadNJ is a server component that handles multipart(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.
However, separating and storing the elements of files and strings from the data provided by InputStream is a very complicated task, and DEXTUploadNJ separates data from InputStream and is convenient for implementing business logic by using the FileUploadclass.
// devpia.dextuploadnj.support.common.FileUpload class is a class that handles uploaded files.
FileUpload dextnj = new FileUpload(request, environment);
// Save the file part from the data sent from the client as a temporary file.
// The temporary storage location is set by use the setTempRepository method of the Environment object.
dextnj.prepare();
// Get a FormItem object with the delivered form name.
FormItem formItem = dextnj.getFormItem("name");
// Get a FileItem object with information on a temporary file named "file1".
FileItem fileItem = dextnj.getFileItem("file1");
// Save the file to the path of the basically set directory(This is same job to move the temporary file to the destination).
fileItem.save();
FileUpload object receives the current HttpServletRequest object and uses the prepare method to internally separate files and other form elements. After calling prepare method is completed, you can handle string and file elements using getFormItem or getFileItem method.
- 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 executed when the prepare method is executed at the time of execution, and recording(moving or copying) the file to the place where actual storage should be performed is processed by calling the save, saveAs method of the FileItem interface.
If you want to change the information of the directory that records the file, register the directory information to the Environment object and pass this object together when creating the FileUpload object.
Environment env = new Environment(); // set a position where temporary files are stored env.setTempRepository("/home/user/temp"); // set a position where files are stored finally env.setDefaultRepository("/home/user/files"); ... // Use the Environment object as a second argument. FileUpload dextnj = new FileUpload(request, env); // Or you can set it using the setEnvironment method. FileUpload dextnj = new FileUpload(request); dextnj.setEnvironment(env); - 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.
- Get client file element
-
DEXTUploadNJ supports various methods to retrieve the form elements of the file defined to the client.
<form action="..." method="post" enctype="multipart/form-data"> ... <!-- if an individual name is given --> <input type="file" name="file1"/> <input type="file" name="file2"/> <input type="file" name="file3"/> ... <!-- If the same name is given --> <input type="file" name="files"/> <input type="file" name="files"/> <input type="file" name="files"/> </form>
FileUpload dextnj = new FileUpload(request, environment); dextnj.prepare(); // get the first file element. FileItem item = dextnj.getFileItem(0); // Get the first file element with the name of "file1". FileItem item = dextnj.getFileItem("file1"); // Get the first file element with the name of "file2". FileItem item = dextnj.getFileItem("file2"); // Get the first file element with the name of "file3". FileItem item = dextnj.getFileItem("file3"); // Get the first file element with the name of "files". FileItem item = dextnj.getFileItem("files"); // Get all file elements with the name of "files". List<FileItem> items = dextnj.getFileItems("files"); // Get all file elements("file1, file2, file3, files, files, files"). List<FileItem> items = dextnj.getFileItems();