1

I want to download a file from one of the EBS volumes I created on Amazon Elastic block storage. Mostly it is advisable to used ServletContext#getResource() and its counterpart ServletContext#getResourceAsStream() as well advised here.

But in this case is the following code advisable

InputStream in = new FileInputStream(new File(FOLDER_PATH_ON_AMAZON_EBS + "/" + folder + "/" + fileName));

Community
  • 1
  • 1
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166

2 Answers2

1

It's hard to tell what the question is here.

If you are asking whether it is better to use getServletContext() or new File(PATH_TO_EBS...) then it simply depends on what you are running. If you are running a standalone java application and requesting files via sockets, then you would use the latter (a FileInputStream over a file you know where to look). If you are running a web server (eg Tomcat) and will be using a web client to download the file, then you would typically use the getServletContext() since that is part of the web-server infrastructure.

Both ways let you get a handle on the file, but getServletContext() is going to refer to a location for your application under Tomcat's working area. Are you going to mount your EBS volume somewhere where you can easily reach it starting from Tomcat's working area.

If you are running a web server and it is allowing you to reach a file directly in your EBS volume with new FileInputStream(new File(MY_EBS_LOCATION + "/" + ...)) then use it by all means - clear and easy.

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19
0

Having the file path in the properties file, and using the absolute path is better due the following reasons,

  • There are chances that the mountpoint of the ec2 volume changes, having that in the classpath and modifying that is comparatively harder than modifying the entry in the properties file.
  • Normally resources like property files are got using getResource(), if there is going to be other IO like storing photos, office docs then its wise to use absolute path for access.

It also depends on the usecase if the files(resource) is frequently used by the application, and the application only, then u can have that in classpath and pack it along with the archive.

Muthukannan Kanniappan
  • 2,080
  • 1
  • 16
  • 18