I have a program (GUI = JavaFX), which scans my file system for specific XML report files and lists them in a table. These XML reports are rendered in a web browser by XSLT. Now I want to be able to click on such a report in my Java application and have it displayed in the browser. I already wrote the handler and the correct URL is determined. On my Windows system this is
file://localhost/C:/report.xml
The XML is not the problem. If I open it manually in my browser everything works fine. However if I use Google and ask how to open files in a browser it always gives me this:
java.awt.Desktop.getDesktop().
browse(new java.net.URI("file://localhost/C:/report.xml"));
As this is a good solution for http URLs (web sites), it always opens my XML file in my default text editor (e.g. Notepad++). So the browse method of the Desktop doesn't really force the browse, but merely falls back to a default open operation.
So the question is: How can I force Java to open the XML in the browser similar to the Windows function "Open with >"?
Here an sscce (which should try to access the file in a browser, even though it doesn't exist):
public class XMLOpener {
public static void main(String[] args)
{
String fileURL = "file://localhost/C:/report.xml";
try {
java.awt.Desktop.getDesktop().browse(new java.net.URI(fileURL));
} catch (Exception e) {}
}
}
The JavaFX replacement for the awt Desktop.browse method would be HostServices.showDocument. You could try that, but it will likely have the same the same effect as Desktop.browse.
Another alternative is to load the XML and perform the XSL transform in Java, then display the resulting document in a JavaFX WebView using webview.getEngine.loadContent(contentString, contentType) or just display the resultant document in a Label or custom JavaFX control. Note that, as of JavaFX 2.2, the JavaFX WebView does not yet have a viewer for pretty printing an xml content type, so to get pretty printed xml in the webview you may need to parse and format the xml as an html document using javascript/css/html, similar to the method demonstrated in this post for displaying formatted java source in a WebView.
For me, although it's more development work, this alternate approach of handling the display with JavaFX is nice because the display of the resultant document can be encapsulated and controlled in the JavaFX application itself and you don't have a reliance on whatever browser and configuration may or may not be installed in the host environment.
If the browser can't handle the URI, browse launches "the application registered for handling URIs of the specified type is invoked".
Check your favourite browser exists (Chrome, Fx, O, IE, etc) or get it by some other method, and then execute a custom command. If you know the OS you're running on (windows) then you only need to consider the exec line for that.
Related
I am using Selenium and I need to upload an image to instagram. But at the time the windows file selector opens I could not get it to search for the image and upload it automatically
Up to this point my application opens well, when it goes to the point of selecting the file it does not do it automatically
I tried using the following codes without success
driver.findElement(By.className(name)).send_keys('C:\\path-to-file\\file.jpg')
But it gives me back the value of the second photo, that is, it generates a file selector from the main folder and not the one that I pass through as a parameter
I am using the following versions:
Java: 8
Selenium: 3.14
ChromeDriver: 76
Normal browsers don't allow JavaScript any access to the local disk. This is basic security. Imagine what would happen if you visit some site and it reads your local password file or any other important data without asking you.
If you want to upload files, take some standalone application or write a small utility that will do that. For instance, try instapy, you can find it pypi.org or on github. It is a matter of taste, the other can prefer other tools. Just try several tools and see which fits your need better.
The window opened for file explorer is not part of the browser and therefore it can not be interacted with via selenium. The solution to this is a software called AutoIt. Use the following link for a guide on how to use AutoIt for file uploads : Link
Here is a little snippet of what I need to do:
if (Desktop.isDesktopSupported())
{
final Desktop desktop = Desktop.getDesktop();
try{
desktop.open(new File(path));
}
catch (final IOException e)
{
MessageBox.alert("Log View", "Error finding file", null);
}
}
I need to open a file in the logged in users default desktop application for that file type. Maven is giving me issues on build saying it can't find java.io.File and java.awt.Desktop.
Kind of a 2 part question, first, does GWT have some sort of similar functionality to java.awt.Desktop and how do I fix the project so maven finds the io and awt files?
To get the same effect as a JButton calling Desktop in HTML, you would provide an HTML button that links to a download of the file and ensure it has the correct content-type provided by the server.
Specifically to the GWT part of the question.
Yes you can do this. You would likely use either use a Grid, FlexTable, HTMLTable or event just HTML to display. You would embed that widget into a panel, and display that panel on the root panel.
The trick will be to get the file data from the server to the client. You can do that using either GWT RPC, or some kind of REST server. If you look at the default app that is produced when you start a GWT application, it will send back string data based on a string. You can use that as a pattern to bring down your model object. The default app uses GWT-RPC, and is pretty easy to follow.
The reason why I was vague on the type of widget is it will depend on how you want to present this data to your user. You can use an HTML to just render HTML, but I would think you would want to use a better widget to truly render this data.
No, you can't do this with GWT.
Browsers typically have no access to the file system. The best you can do is have the users install some plugin that gives you file access (will require a permission check) or have them upload a file to your server (your web app can download the file from there to inspect it).
I am using JNA with Java to find some properties about open windows on a Windows machine desktop. I am trying to find a way to get the file being accessed by an arbitrary windowed application. For instance, say I get information regarding the window of an open pdf document in adobe. I want to be able to get the filepath of the pdf document displayed in the window.
I know about the GetWindowModuleFileName() method, however this gets you the filepath of the executable of the application, i.e. 'javaw.exe'. If you have 'my.pdf' open in adobe, I'd like to get the filepath of this document, i.e. 'C:\...\my.pdf'.
I've done some searching around (on this site and others) and haven't found anything yet on this in particular.
thank you for your time, -Kevin
If you know the process ID, you can get the list of all files currently opened by the process using Handle utility. However, it depends if adobe reader continues to keep the file open or closes it after reading it completely.
I need to show the first slide of a PowerPoint presentation in my jsf app. It must look something like that:
user upload the .ppt file to jsf app
the app take the first slide and converts it to Flash
user sees the converted slide in the separate (not as part of the another page)
I suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
Also I'm thinkinig about Flash, not HTML, because the presentation can be dynamic.
Any ideas? Can I do this task or it's impossible and I need to think in another way (maybe restriction for end users - to save presentation as png, but I think that my boss won't like this decision).
Maybe I need to look at .xslt format, maybe it would help?
suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
You can truly use external tools since your Java/JSF code runs at the webserver, not at the webclient (webbrowser). All the webbrowser get is just the Java/JSF-generated HTML/CSS/JS code. Open a page in webbrowser, rightclick and View Source and see it yourself. You just install the external tool at the webserver and execute it there.
To upload a file in JSF, you'll need to grab a 3rd party component library since the standard implementation doesn't have an upload component, for example Tomahawk's t:inputFileUpload or the one of whatever component library you're currently already using.
To convert PPT to Flash, execute the appropriate action using external tool in the managed bean action method and store the Flash file somewhere in the local disk file system of the webserver. You can store it in the public webcontent so that it's directly accessible by URL, but those files will be lost whenever you redeploy the webapp. If this shouldn't happen, then store it outside the public webcontent.
I don't have hands on experience with PPT-Flash converters, so I can't recommend a specific one, but Google learns me that there's pretty a lot of choice. There seems to be a Java solution of iSpring.
To display the Flash file, use the HTML <object> element which points to the URL of the Flash file. If the Flash file is stored outside the public webcontent, then you'll need to create a Servlet which gets an InputStream of the file from the local disk file system and writes it to the OutputStream of the response along a correct set of HTTP headers and then let the URL of the <object> element point to that instead.
I want to open a webpage (whose URL is given as the commandline argument) and then want to save the content of that webpage as a .txt file.
Remember, I need the .txt file and not the source of the webpage.
I tried my hand with selenium and it works fine. But now I want something that doesn't open the real browser as opening the browser and loading a page in it is a time consuming task.
I want to do it in java.
By content, I mean the text (without markups) which we get when we save a webpage in IE by going to "Save As" and then selecting ".txt" as the output format of the file.
If I understand correctly your question, you want to render the page and copy the rendered text without using a navigator.
For this, you'll need a headless browser. HTMLUnit would be a good choice.
To get the text content, you could do it like this (not tested) :
WebClient c = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
TextPage tp = c.getPage("yoururl");
String content = tp.getContent();
(see Javadoc)
Hmm, I'd even code that from scratch, does not seem as a complex thing and might not be even worth adding a dependency on another library to your project:
Open a URLConnection to that URL
Get a stream from the connection, apply regex to strip out all the HTML to the data. If the page is not expected to be too large for you memory requirements :) read the page into a String then apply the regex. Alternatively, give a shoot to what's described here (I have no experience with the way described there though).
Save output to a txt.