Posted In: Apache, Server, Tomcat, Tomcat

How to save uploaded files in a enterprise application

Before writing about how to upload file developer needs to consider how to choose location to upload the file.

 

1. Not clustered environment – Basic file system

Case 1 – Uploading files to Web root

This is easiest solution to implement. Developer will be tempted to do this. E.G. Application is deployed at location /deployment/app/web/ then just create another folder under web and upload your files there. Side effect from this approach is as files are uploaded inside Web root, developer can easily provide even links to download the files without any extra work.
Example – Uploaded file to /upload/abc.pdf then even download link /upload/abc.pdf will also work.

There are some problems with this approach

1. When developer deploys new WAR/EAR file to server all uploaded files will get deleted.
2. In enterprise application environment Server disk space is much more restricted and costly. So application may face space issues.
3. As download link is directly available secure access rules cannot be added.
4. Approach will not work in clustered server environment.

Case 2 – Uploading files to WEB-INF

Application is deployed at location /deployment/app/WEB-INF/ then just create another folder under WEB-INF and upload your files there. In case application needs to give also download access then another download Servlet is required and will stream file data in HTTP response.

There are some problems with this approach

1. When developer deploys new WAR/EAR file to server all uploaded files will get deleted.
2. In enterprise application environment Server disk space is much more restricted and costly. So application may face space issues.
3. Approach will not work in clustered server environment.

Case 3 – Uploading files to outside deployment. Always use this for a basic application.

Files are uploaded to totally another folder on application server. Folder is non-mounted folder. In case application needs to give also download access then another download Servlet is required and will stream file data in HTTP response.

Points to consider in this approach

1. When developer deploys new WAR/EAR file to server none of the uploaded files will be affected.
2. In enterprise application environment Server disk space is much more restricted and costly. So application may face space issues.
3. Approach will not work in clustered server environment as files are uploaded to server file system.

 

2. Clustered environment – Mounted file system

Mount specialized file server folder to your application server. As all servers share same mounted folder this approach will work in clustered environment.


All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /.  These files can be spread out over several devices.  The mount command serves to attach the filesystem found on some device to the big file tree.  Conversely, the umount(8)
command will detach it again. 
The standard form of the mount command is: 
mount -t type device dir

 

3. Clustered environment – Cloud based storage

Upload and download services shall be provided by the service provider. These are fast and reliable services and removes burden to maintain images/docs on enterprise network. These are secure services.


Example
Amazon Simple Storage Service (Amazon S3) makes it simple and practical to collect, store, and analyze data - regardless of format – all at massive scale. S3 is object storage built to store and retrieve any amount of data from anywhere – web sites and mobile apps, corporate applications, and data from IoT sensors or devices. It is designed to deliver 99.999999999% durability, and has many customers each storing billions of objects and exabytes of data.

 

4. Clustered environment – Database server

Developer can create file table with BLOB column type to store date. Files should not be too big for performance reasons. Following JDBC is needed to upload file


Write
File image = new File("e:\\abc.pdf");
FileInputStream   fis = new FileInputStream(image);
stmt.setBinaryStream(3, fis, (int) image.length());

Read
File image = new File("e:\\abc.pdf");
FileOutputStream fos = new FileOutputStream(image);
byte[] buffer = new byte[1];
InputStream is = resultSet.getBinaryStream(3);
while (is.read(buffer) > 0) 
       fos.write(buffer);
}

MySQL 
A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.

ORACLE
A BLOB (binary large object) is a varying-length binary string that can be up to 2,147,483,647 characters long.

 

Simple servlet upload code using Apache Commons Upload

JSP
<form method="post" action="uploadFile" enctype="multipart/form-data">
	Select file to upload:
	<input type="file" name="uploadFile" />
	<input type="submit" value="Upload" />
</form>

Upload Servlet using apache upload commons
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

// configures upload settings
DiskFileItemFactory factory = new DiskFileItemFactory();
// sets memory threshold - beyond which files are stored in disk
factory.setSizeThreshold(MEMORY_THRESHOLD);
// sets temporary location to store files
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);
 
// sets maximum size of upload file
upload.setFileSizeMax(MAX_FILE_SIZE);
 
// sets maximum size of request (include file + form data)
upload.setSizeMax(MAX_REQUEST_SIZE);

List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
	// iterates over form's fields
	for (FileItem item : formItems) {
		// processes only fields that are not form fields
		if (!item.isFormField()) {
			String fileName = new File(item.getName()).getName();
			String filePath = uploadPath + File.separator + fileName;
			File storeFile = new File(filePath);

			// saves the file on disk
			item.write(storeFile);
		}
	}
}

by , on August 25th, 2017

  • Categories