how to open a pdf file from java? - java

I want to open a PDF file from a jsp. The jsp and the PDF are in the same directory.
I am using the following piece of code:
if (Desktop.isSupported()) {
try {
File myFile = new File("<file name>.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
However, I get the error that the file is not found.
Verified user.dir and it points to my tomcat/bin.
How can I refer to the pdf to open it?

You need to specify the absolute file path. Assuming that there's a filename.pdf in the root of the public webcontent, this should do:
File myFile = new File(getServletContext().getRealPath("/filename.pdf"));
However, this construct won't work the way you'd expect. It will show the PDF file in webserver machine, not in webbrowser machine! Only when you happen to run both the webserver and webbrowser at physically the same machine, this will "work". But this does obviously not happen in real world when you publish your webapp into the internet where the webserver and webbrowser runs at physically different machines.
Instead, you just need to link to the PDF file directly.
View PDF
and let the browser handle the display.

Have you tried this? I just got this from google, so I dunno if it will work.
Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:\\Java- Interview.pdf");
p.waitFor();

Related

How can I find image file within my Tomcat 8 java app?

In my Java tomcat application, I'm facing problems trying to get the correct file path.
Two code snippets from my Java app I need:
try {
InputStream in = getClass().getResourceAsStream("StaticContent/images/image.png");
image = ImageIO.read(in);
} catch (Exception e) {
System.out.print(e.getStackTrace());
}
Second snippet:
DataSource fds_image = new FileDataSource
("StaticContent/images/image.png");
I have put the image into the folder:
src/main/webapp/StaticContent/images/image.png
Folder structure
I have tried several ways to make sure the file path is correct, but nothing worked. Also, I put the image into the ressources folder and tried different paths but to no avail.
I assume that I choose the wrong (relative) path, but I don't know how to handle it.
Can anybody help me with this?

How to download file using dropbox

I'm trying to build an app that gives my users right to download file from my dropbox storage.
So I've got next quesions:
1) I was following the dropbox tutorial and it says:
If the Dropbox app is installed, the SDK will switch to it so the user
doesn't have to sign in, and it will fallback to the browser if not.
If I run my app, it will open a browser with dropbox web site. Is there any way to avoid that?
2) Is it possible to download file by file name only? Can I just set url for my storage and file name without randomized url?
You can use the dropbox public folder for downloading the file that you want. You just have to put the file in that folder and then copy the url.
Then on you java you just have to put the code to download from a URL.
This is how I have done in my program and it doesn't open any browser.
A good thing about the URL in Public folder, is that it doesn't change if the filename doesn't change, so you can update your file and the URL will be the same
You can simply use client to download file by name
DbxEntry.File md;
File file = new File("destination.file");
OutputStream out = new FileOutputStream(file);
try {
md = client.getFile("/path/to/target.file", null, out);
} finally {
out.close();
}
Here null indicates that you want to receive the latest revision of file. And "/path/to/target.file" is path to file on your dropbox, like "/Public/001.jpg".
Also md can be used to retrieve some metadata about this file, like its size, name, revision, etc.

Open a file using Desktop(java.awt)

Am using the following code snippet to open a file,
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
File myFile = new File(fileName);
desktop.open(myFile);
} catch (IOException ex) {}
}
if the file i try to open is of normal .txt or .pdf any such file types i can able to open the file.
But in some cases,
As shown above the Type of file is File , in this scenario the file is not opened.
if i manually open this File type file i get open with window and using a text editor i am able to open it.
How should i handle this in java? Please help..
Windows says the type of the file is "file" when there is no extension. This is why there is no default opener, and that is why Java doesn't open the file properly.
You can either rename the file and give it an extension, or use Java to directly execute the program you want to open the file with.
#open method finds the specified default app to open the specified file but in .file type there is no default app to open the file.You can only specify manually to open it in specific app.So if you set the default application that should open the .file and you have to set the default application and choose default program to open the file and than run the program.

Can't find folder or file created with Android App with windows file exlorer

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.

java- jar file cannot find resources

I 'm working on a java application and in the program I use some files such as images, a local database and an .htm file (used as help file).
I have tried to make the access to these resources, location independent. So I've made in the package that contains my source files, a subfolder named resources and I've put there all these images,etc. I access them through .getResource() method, and not by using paths. For example that's how i access an image resource:
lang_icon=new javax.swing.ImageIcon(getClass().getResource("/myeditor/resources/checked.gif"));
The problem is that when the .jar file is built, it doesn't work properly. It loads the images successfully but cannot connect to the local database or open the .htm file.
Here is the code I use to access the .htm file (it works fine when I run the application through Netbeans)
URL helpFile= getClass().getResource("/myeditor/resources/help/help.htm");
try {
BrowserLauncher launcher = new BrowserLauncher();
launcher.openURLinBrowser(helpFile.toString());
} catch (BrowserLaunchingInitializingException ex) {
Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedOperatingSystemException ex) {
Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
}
and here is the code I use to access my local database
URL url = getClass().getResource("/myeditor/resources/icddb");
database = new File(url.getFile());
try
{
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:" + database+"\\icddb", "sa", "");
When I try to open the .htm file through .jar file it shows this error "This file does not have a program associated with it for performing this action. Create an association in the Folder Options in control panel".
When I try to connect to my local database it says "Severe could not reopen database".
All ideas appreciated!
Thank you and sorry for my english!
Can you print out the URL you're asking the browser to open ? That should give a clearer indication as to what's going on.
I suspect the URL you're getting from getResource() is a URL pointing to within your .jar file. As such the browser is going to have difficulty opening that. I would (perhaps) extract that file to a temporary directory and have the browser open that.

Categories