where to keep an XML that should be editable and downloadable? - java

So I have a web app (using apache tomcat server,servlets,eclipse IDE). I wrote code to allow user to edit the XML file via UI. So I used the following code to access the XML through java
String fileName = "/MyXML.xml";
String path = this.getClass().getResource(fileName).toString();
This works fine. I am able to edit the file through UI.
Now I want to let the client download the file. But I am not able to access the file while I am trying to download.
However, if I keep the file inside webapps folder, then I can access the file using the following
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/MyXML.xml");
(Thanku Mr.MK Yong- http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/)
But then If i keep it inside webapp folder, how do I access the file for editing the XML ?
So basically I am either able to edit the file, or I am able to download the file(from webapp folder), or I am able to do both on two different copies of the file. I want to edit the XMl file and be able to download the same. So where do I keep the file and how do I access it?

You should store it in the local resource folder as it is essentially a dynamic resource.
The other thing i recommend is if you know the parameters that will be changed then have a template in resource folder and store the changes in database.
Personally i have it the second way.
e.g.
/yourapp/resource/config_file/xmltemplate.xml
Parameters that can change:
userLocation
folderLocation
colorBase
Stored them in the table:
Table: UserCongifStorage
Columns: userLocation, folderLocation, colorBase
So when i need to use the data from row 1 the logic is:
read in the xml file into a string, replace the variables with data retrieved from database, output it as xml to resource folder.
Then you read for usage.
Hope that helps

If the file is in your 'webapp' folder (I think you mean your application root), then it is already accessiable to everyone by calling hxxp://domainname/appname/MyXML.xml. I would suggest you not to store files that can be edited, inside your app folders, since they will be overwritten if you redeploy your application.
Put them in an external directory and load the contents like you would do with all other files. Doing this you can take control over file permissions easily, too.

Related

Create a file link on the fly in wildfly

I have some questions about Wildfly deployment
1.An ear, when deployed in wildfly, is extracted within standalone/tmp/vfs/deployment/ directory. Can I place a file there manually and still access it from web. (I can check it, but as of now I do not have any machine to test it).
Can I create a file and place it there via some program. The reason I am asking this question is that I need to generate some files based on user input and provide the user with a link to that file. One way to do this is to statically link a directory in JBOSS and create the file there(access it using file handlers see this). I just want to know if it can be done at all using something like VFS.
If you need to persist to a file you'd want to create a new file handler, like the link you provided describes, and write the file to that folder. You don't want to try to use that temporary deployment directory. The content is not exploded by default so writing to it would likely fail.
If you don't need to persist to a file you can just use an output stream of some sort and the user will be able to download the file.

Where to create folder for uploaded files in a Spring project?

I am making a Spring (not Boot!) project right now, with Maven. The user can upload a file, then I store it in the file system. When I start the application, I create a folder with a method annotated with #PostConstruct. I have already tried two ways: create the folder in the same level as the src and target folder, or create it in the target/tomcat folder (I used the ${catalina.base} property). I want to do some conversion on the uploaded file, basically convert it to JSON, and also store the JSON file. Then I want to use these two files inside JSP pages: when the user executes a GET request, my application will return the path of the files, and I want to use the content of the JSON with JavaScript.
So my question is basically: what is the best place to store these files, if I want to use them later for any purpose, and I dont want to use absolute paths. I saw some example when the code creates a folder like: "C:\folder", but that looks weird for me, relative is much better in my opinion.

Java: Universally accessing resource files regardless of application server

I'm looking for the 'best practice' way to access resource files (for example a bunch of .xml files) as well as the folder structure in which they reside, regardless of the application server used.
Right now I'm using Wildfly 8 server and I access all src/main/resources/xxxx by getting the application real path then using Paths.get(resourcePath) as well as Files.walk(Paths.get(folderPath)) if I want to access a folder's files.
However, I faced a problem when I tried to deploy to Weblogic 12c, because this app server actually takes everything under WEB-INF/classes and creates a .jar file and adds it to WEB-INF/lib. I can still access singular resources using classLoader.getResource(resourcePath) but for some reason when I try to create a new File(Paths.get(resourcePath) or use Files.walk(Paths.get(folderPath)) it doesn't seem to be working. It throws an exception saying to file doesn't exist which I'm guessing is because it is not accessible since it is packaged inside a jar file.
I could potentially use classLoader.getResource(resourcePath) to access all my resources but unfortunately in my case I cannot know what resources will be available at compile time. I specifically NEED to be able to go through a selected folder's files and subfolders but I haven't found a common way to do it on both app servers, or ALL app servers for that matter.
Bonus points if the solution uses the new File api instead of creating a bunch of FileReaders but I'm ok with that too.
You could place the XML files in a folder /WEB-INF/xml and then use the ServletContext to obtain a File or Path for that location.
Variant 1:
call servletContext.getResourceUrl("/WEB-INF/xml") to obtain a URL and convert this URL to a File or Path. But depending on the server this might return a non-file resource URL, e.g. a jndi:/ url.
Variant 2:
call servletContext.getRealPath("/WEB-INF/xml") to obtain a file string and convert this URL to a File or Path.

Where to save uploaded files in server?

We have developed a web application using JSP and Servlet. The server we use is Tomcat 7. We have hired a host (Daily Razor) with "private JVM" to launch the application in production level.
Now, in our application, user can visit a particular form, browse for a file in his PC and upload it to the server. But I have a question there; what is the best place to store these files? Mainly there are 3 types of files so we would like to categorize them into, "Office', "Home" and "Other" and create 3 folders for them. But inside which main folder these 3 folders should be made?
The main important this is that these files should not be accessed via a URL (because then anyone can get them ), but a Servlet can. Apart from that, the location (String) should be saved in our MySQL database so the file can be accessed again without an issue.
We have developed the application using Netbeans IDE so the folder structure is like below.
I look forward for your answers.
Use a java property to specify the directory where the files should be stored, and pass it on to tomcat during start up.
Also, it might be a good idea to separate the files per user.
Possible Solution:
-Duser.data.export.dir=D:\users_export\directory
In your java code, read the property
String property = System.getProperty("user.data.export.dir");
Now lets say for user 'A'
String userName = getCurrentUser();
Path userDirHome = Paths.get(property, userName, "Home");<br/>
Now use userDirHome to store the data.
Suggestion: When you store the file location in the DB, ensure that you do not store the complete path, only store the relative path, like "Home/myFile.txt".
This will help you at a later point in time when, there is any change in the directory where the file are stored.

Creating a js file to be accessed by jsp

Alright heres the scenario. I'm creating a dynamic website with Java\JSP\JS. I'm using the Java to access a database and query a table to get information. Instead of passing that query to the JSP, I decided to create a JS file with a class in it with all of that information. (I'm basically building a store locator that will update itself with new stores\locations whenever the user presses a button. Obviously they need to update the table first but you get the point)
And now onto my problem. I'm creating the file by opening a stream. (But first I check to see if the file exists and if it does delete it, because I don't want to keep writing to that same file every time a user clicks the button.)
FileWriter fstream = new FileWriter("test.js");
The location of the file goes to my Eclipse folder directory, obviously. Even doing this,
FileWriter fstream = new FileWriter("./test.js");
put it there. Granted I didn't think it would change but I was testing it out.
The location of the JSP file when the project isn't 'deployed', or testing, its in the workspace location. But when it is 'deployed'
\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BoomBaby\index.jsp
When I get the location of the JS file its:
/C:/Program%20Files/Apache/lib/
So my question, after all of this, is: How do I save the JS file in the same directory as the JSP file while deployed so I can access it from a JSP file by:
<script type="text/javascript" src="test.js"></script>
You need to build a WAR file. It is basically a zip file with a standardized layout. You put all your jsps in one place, and all your js files in another. Containers like tomcat understand the layout of the war and will run your application automatically. Popular build tools like ant and maven will construct wars for you with little effort.
You can't when deployed as a WAR file. The way this is usually done is, instead of generating a file and referring to it, generate the JavaScript source into the page directly.

Categories

Resources