This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 7 years ago.
Working on some import process where i need to first upload the file at some location on server and than later on i need to pick the file from this location to import it in to the system.
i am just wondering what might be the best place to store the uploaded file. i have few option
1) Can create a folder in the root of tomcat and than can place the upload files there and later on can pick the file for the import process.
File dir = new File(System.getProperty("catalina.base"), "uploads");
is this a good option and weather the above code will work equally in all enviornment
2) i can create an uploads folder undermy application and can access it for file upload and later on for import by using the following code
ServletActionContext.getServletContext().getRealPath("uploads");
your valuable suggestions are needed the only work i need to do is to upload the file and den run the import process for the uploaded files(s) and once import is successfull remove files from this folder to other like processed etc.
File dir = new File(System.getProperty("catalina.base"), "uploads");
It won't work on environments where catalina.base property is absent. So you need to either document it properly in the installation manual of the webapp to ensure that this property is been set on the server machine in question, or to look for an alternative approach.
ServletActionContext.getServletContext().getRealPath("uploads");
This is not a good choice as permanent storage. Everything will get lost whenever you redeploy the WAR.
Rather store it in a known and fixed path outside your webapp and add its path as <Context> in Tomcat's /conf/server.xml so that it's available online as well.
If you don't want to alter the Tomcat's /conf/server.xml for some reason, then you need to create a servlet which reads the file from disk using FileInputStream and writes it to the OutputStream of the response. You can find here a basic example.
Related questions:
Simplest way to serve static files from outside application server
Related
So, I need to download 2 csv files which include pricing details for some AWS services.
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AWSStorageGateway/current/index.csv
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonS3/current/index.csv
I need to download the files from the URLs and store it in separate folders to avoid conflicts. Such as, the first index.csv file will be downloaded to a folder AWSStorageGateway and the second one to folder AmazonS3.
Or another approach could be to store those files in 1 folder by changing filenames like AWSStorageGateway.csv and AmazonS3.csv. If the files already exist, it needs to be replaced with the new one.
The project uses Java 8 and Spring MVC. I don't want to use any external libraries. Is NIO package enough to handle this?
I need to modify html file that is placed at server folder from my servlet.
No other way than read it by FileInputStream to byte[], convert to String[] splitting lines by "\n", change what I need and then rewrite it.
I don't see.
This is not possible by design. Your server might just have to serve a .WAR file. If the server is not configured to unzip it, your server will have to read all files directly from this archive. You can now guess that you cannot write at this location.
You would need to create some kind of working directory and also serve files from there, too. You can always use this directory as working directory:
File workingDir = (File)servletContext.getAttribute(ServletContext.TEMPDIR);
This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 5 years ago.
I have a java web application. This app reads 2 excel file and process them and display the result on the screen. I am using Eclipse and tomcat v9 to implement and run the application locally. I have put the excel files in the root of the application. I have a function in ProcessFileUtility.java class that takes the file name of two excel files and process them. I give the absolute path of those two excel files to that function and it works on my local. But when I export the web application as .war file and deploy it on the tomcat server, I don't see my data displayed and my immediate guess is that the file path provided is an absolute path and should be some thing else. I know this question has been asked already but after reading many of them I am still struggling what to do.
Can some please help me with this problem. I have also put an screen shot of the structure of my application.
Here is the path to excel files :
ArrayDataModel<Record> records = this.processDataSources("C:\\EEworkspace\\PVvalidation\\ppmsOrigin.xlsx", "C:\\EEworkspace\\PVvalidation\\product_database-reverse-column.xlsx");
The problem is actually the path you are using. Don't expect the server to have that path and file unless you create it.
Please refer to this answer and see how the file is inside the src folder. That way your application will be able to find the file inside it's folder.
This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 7 years ago.
The following code returns the path to the war folder (i.e. /ROOT/):
getServletContext().getRealPath("/");
I need to go one folder up from this point, how do I go about doing that?
I have tried the following:
final String path = getServletContext().getRealPath("./");
final String path2 = getServletContext().getRealPath("../");
final String path3 = getServletContext().getRealPath(".../");
I need so save files outside of the ROOT folder, but only one level up, so that everytime I update my website, it doesn't replace the physically uploaded files within the ROOT folder, and rather only touch the web site files.
Thanks
We don't even know if your WAR files are unpacked: Tomcat can well deploy them without unzipping, which leaves you with a non-filesystem-path.
As discussed in the comments, it's bad practice to do this, you should rather figure out where to store the data - for example: database, external storage, somewhere in the file system. Then "just" provide a separate download option for those files - individually or zipped - through your web application.
Another solution you can use is a symbolic link to an external folder.
You can create a folder out side your project let's say for the example at the same level of the webapps folder in your tomcat path.
We will name uploads for the example.
Now in your deploy script (assuming you have one), you can easily create symbolic link from the uploads folder to a folder inside your project.
It will allow you to get the files from inside of your project, and when you re-deploy, the files are not delete.
For example:
ln -s /var/lib/tomcat6/uploads /var/lib/tomcat6/webapps/myWebApp/uploads
The example above is for my distro of tomcat6 but you can change the path to work with your setup.
This answer uses 3 assumptions:
Your WAR file is unpacked.
You have a deployment script and if not you can easily create a bash script
to do so.
The solution you need is for a non distributed setup (each server holds its own files).
Liron
You have to use the File's API over war's location.
File root = new File(getServletContext().getRealPath("./"));
String parent = root.getParent();
String parentParent = new File(parent).getParent();
I'm new to Cloudbees. I just opened an account and trying to upload a .JAR file which basically downloads a file to the location mentioned by user (through java command line arguments). So far I have run the .JAR in my local. So far, I was referring to my local file system to save the file. If I deploy my .JAR file to Cloudbees SDK, where can I save the downloaded file (and then process it).
NOTE: I know this is not a new requirement in java if we deploy the jar in UNIX/WINDOWS OS where we can refer the file system w.r.t to home directory.
Edit#1:
I've seen couple of discussions about the same topic.
link#1
link#2
Everywhere they are talking about the ephemeral (temporary) file system which we can access through System.getProperty("java.io.tempDir"). But I'm getting null when I access java.io.tempDir. Did anyone manage to save files temporarily using this tempDir?
You can upload a jar with the java stack specifying the class and classpath (http://developer.cloudbees.com/bin/view/RUN/Java+Container)
Our filesystem however is not persistent, so if you are talking about saving a file from within your application, you could save it in this path
System.getProperty("java.io.tmpdir")
but it will be gone when your application hibernates, scales-up/down or is redeployed.
If you want a persistent way to store file/images, you can use AmazonS3 from your cloudbees application: uploading your files there will ensure their persistence.
You can find an example of how to do that in this clickstart:
http://developer-blog.cloudbees.com/search?q=amazon+s3
More information here: https://wiki.cloudbees.com/bin/view/RUN/File+system+access