Reputation: 215
Specifically I am trying to open a file from SharePoint, but it's really just a website, and I have the proper access so that' not an issue. I am trying to use the desktop api in java to open it, but it gives me an error message "File does not exist!" Does desktop only work locally? If it does work for websites, what am I doing wrong?
new code based on stephen c's suggesttions, it still does not work however. What am I missing?
public class ParseURL {
public static void main(String[] args) throws Exception {
try {
URL url = new URL("http://wss/is/sites/itsd/network/Remote%20Access/Soft%20Tokens/Your%20new%20RSA%20Soft%20Token%20for%20Android%20-%20INC%20XXXXXXX.oft");
InputStream is = url.openStream();
is.close();
} catch(IOException err) {
}
}
}
old code
public static void main(){
try {
File oftFile = new File("http://wss/is/sites/itsd/network/Remote%20Access/Soft%20Tokens/Your%20new%20RSA%20Soft%20Token%20for%20Android%20-%20INC%20XXXXXXX.oft
");
if (oftFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(oftFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File does not exist!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Upvotes: 0
Views: 167
Reputation: 718788
The File
class is for representing the names / paths of files that live on a local / locally mounted file system. You've given it a URL, and that is not a file or pathname.
You should be using the URL
or URI
class, and attempting to read it by opening a connection ...
Rather that giving you potted examples to cut and paste, I recommend that you read the relevant parts of the Java Tutorials.
so i read the tutorials but how would that open a file from the url? it's not just that I am trying to read the data but I want to open the oft file in outlook.
Ah ... so by "open" you mean "launch a separate window with a viewer for the file".
In that case, your code is the right approach BUT you can't use File.exists()
to test if a URL exists. If all you have is an "http:", you need to attempt to open the file using URL.openStream()
as per the example code in the tutorial, and interpret the exception as telling whether or not the file exists. (A HTTP 404 response turns into a FileNotFoundException
but other IOException
s are possible too.) Don't forget to close()
the stream or you code will leak file descriptors.
In fact, you are probably better of not trying to test if the URL exists. Just attempt to "open" it and diagnose the exceptions.
Upvotes: 0
Reputation: 6831
Try using HttpURLConnection.
This is a web address and you need to connect to the url and read the contents from the stream you get from the connection.
See Usage here
Upvotes: 1