How to read data from local XML file in Flex - java

I have a java web application which contains a flash part.Currently the .swf file is reading the xml file from project src folder. I want to access the xml file from local file system(in C:/ drive ).How to access XML data from C:/ drive.Currently my java web application is accessing the same xml file from C:/ drive.Can i pass the xml data through javascript to .swf file.
Is it a best practice.Which is the best practice for accessing XML file from local file system in Flex.
Any help is appreciated.

If your swf is in a web application, you can't access freely the hard drive. There are nevertheless two options:
FileReference
With the command FileReference.browse(), you can allow the user to choose a file from the local system and load it, or upload it.
If you choose to load it, you'll have access to its data as a ByteArray.
ExternalInterface
ExternalInterface allows you to communicate via javascript with the browser. You can set a responder with ExternalInterface.addCallback(). Check the reference for some example.

You say this is a Java web application. So one way would be to put the XML file on server and then make a service call to read it in Flex.
var httpService:HTTPService = new HTTPService();
httpService.url = path; //The URL of the XML file goes here
httpService.resultFormat = "e4x";
httpService.addEventListener(FaultEvent.FAULT, onFaultHttpService);
httpService.addEventListener(ResultEvent.RESULT, onResultHttpService);
httpService.send();
You will have to define the onFaultHttpService and onResultHttpService methods.
AFAIK, there is no way to access the filesystem directly in Flex, unless you are using AIR.

Related

How to read a file from inside Openwhisk Java Action

I'm trying to use openwhisk cloud functions to leverage some existing java code. The code needs to read a local file. Is this supported in a Java Action on IBM Cloud?
Though the file is included in the jar file - the application is not able to reach the file .
OpenWhisk actions can read from the file system. Changes to the runtime filesystem are not persisted across invocations.
If you want to include a file within the JAR, you will need to need to find the location of the JAR within the filesystem, unzip it and read the file contents.
The Java runtime stores the user-provided JAR in the system-wide temporary directory as useraction.jar.
Another approach would be to store the file on an object storage service and retrieve it dynamically during invocations.

Getting the path to current dynamic web project JAVA

How can I access an XML file while my application is already deployed?
I'm running a Dynamic Web Application with several classes and a simple rest service, but I don't have any actual servlets, so accessing the ServletContext is not possible, (as far as I know) so using getRealPath() won't work.
An example:
I have a class DBcon which connects to a database, but has to load the properties from an XML file, which are located at /xml/db/oracle-properties.xml
In a normal Java project you can simply use a file input stream, but it won't work for a dynamic web application.
How can I still load the xml file?
If the file is in the classpath, you can get it as input stream with something like this:
InputStream in = this.getClass().getResourceAsStream("xml/db/oracle-properties.xml");
I figured out by reading this: Where to place and how to read configuration resource files in servlet based application?
I've put the xml files in WEB-INF/classes and then used this code to load it:
InputStream xmlFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
prop.loadFromXML(xmlFile);

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

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.

User Downloads In Java Spring

I need to make a link in my Java Spring application that will allow the user to download a CSV file . I have the CSV file in my project's root directory and I have an tag in my view that runs a Javascript function when it's clicked. Is there any way I can use either Javascript or the Java Spring framework to allow the user to download the CSV file? I'm a noob Spring user/developer so any help is appreciated. :)
Put the file in your WebContent (or whatever you call the web resources directory) and link it directly. Everything below WebContent (but not in WEB-INF) is accessible in your context root.
So file WebContent/test.csv is accesible with relative url /test.csv.

In java web application how to print server file from client side

In the java web application need to select the file from server and print to the local printer. how it can be done
Thanks in advance
That's going to be tricky whenever you require a minimum of user interaction (i.e. just click the link and then do the print magic) and it also depends on the type of the file in question. If it is for example a .doc file, then you would basically need to download it to the client environment and open it in the default associated application (MS Word in this case) and then let the application execute the print command. You can't do this from the server side on.
Your best bet is to create an Applet which in turn displays the file tree, downloads the file to the local disk file system on client interaction and makes use of Desktop#print() to print it. E.g.
File file = new File("/temp/file.doc");
// Read file from server using URLConnection, write it to this file and then do:
Desktop.print(file);
But if it are for example plain text files such as text/html, text/xml, etcetera, then you can make use Javascript to load the file into some <div> or <iframe> element and then execute the window.print() method on it, if necessary along with a CSS media rule.
You will need an applet, flash, silverlight, javafx - i.e. an embedded app. There:
download the file from the server by creating a GET request (in an applet - using URL.openConnection()), obtaining the returned bytes and forming an in-memory document
sending that to a printer. If you chose applet - this might help
(I'm not aware whether the same flow can't be achieved with javascript as well)

Categories

Resources