Java Desktop opens files with different extensions - java

I am using Desktop class to open files like .txt files, .pdf files, images, etc; I have a problem that when I open first file, lets say a .jpg file, the images are displayed in the image viewer, then I try to open a .pdf document, the Desktop object tries to open the .pdf file with the image viewer, and it fails.
When I open the .pdf file first, it opens correctly. After that when I try to open a .jpg file, it is opened in the pdf browser. And so it fails...
In short, the first program associated with the first file, is used to open all the other files.
how can I solve it ?

Related

Is there a way to convert a .xlsx file to a .xls file and a .docx file to a .doc file upon clicking a button in java?

I'm working on a java based web application. Which shows list of uploaded files by the logged in user along with hyperlink of the file and delete button. The delete button deletes the file from the database. Clicking on the hyperlink open the file content on the screen with suitable software. But users are not able to open files with following extensions: .xlsx and .docx. So I'm trying to convert the .xlsx file by copying the content to a new .xls file and copying the content of .docx file to a new .doc file and then show those files on screen.
I'm using richfaces with ajax.
<a4j:commandLink id="lnk_view"
action ="#{FileUploadController.view}"
style="display: none"
onclick ="startLoad();"
reRender="info"
oncomplete = "stopLoad();">
<f:param name="selectedFile" value="#{file.serialNo}"/>
</a4j:commandLink>
Expected Result: Clicking on the hyperlink/view button should show the content of the file selected on the screen.
Actual result: "workbook cannot be opened or repaired by microsoft excel because it is corrupted". However, the file is not actually corrupted, because we can open the file from the location it is saved.

HTML pages loading and showing them in .txt file

I'm trying to build a search engine in java where i have a folder "crawler" to store downloads but when I open my project and click on that file my computer gets stuck because it tries to load all the urls and texts parsed from the html pages (100k+) and tries to show them in .txt files in the "crawler" folder.
How can I prevent loading and showing them in the file ?
Store your data files somewhere outside of your project. Use e.g. a properties file to store the location to your crawler folder. You should only include such files in your project like source code. Something you would upload to Git or SVN. Not the data files your application operates on. If you would store this data in a database, you wouldn't include the whole database in your project, but only the SQL files to create the tables and views.

error while opening pdf files from jar folder

I am making a Software in whihch I have to display the pdf files. I have stored the pdf files in my project folder. The software runs perfectly fine.
But when I cleand and build the project and then i run my jar exe file the pdf files doesnt open.
After some experiments I included my pdf files in SRC folder and then clean and build the project. The difference i found is that now the jar file is bigger in size ( it equals to sum of all the pdf files) , I thought that this time it would work . But it didnt work.
Then After more experiments I included all the files in the Dist folder.
Then The jar files can open the pdf files :) , I was happy but not satisfied, Since I only have to create a jar file and seeing all the pdf files in the project folder with one jar files looks really awkard and senseless, is thier anyway i can open the pdf files using only the jar file without copying the pdf files in the folder where my jar file is stored, . This is the code i used to open a file named "aleemullah resume".
try{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "aleemullah resume.pdf");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error");
}
It looks as if you are reading the PDF file from your current working directory (that is, the folder that your program is launched from). By that logic, you should be able to open a PDF stored anywhere by entering it's full path, rather than just the file name. For instance:
String filePath = "C:\Users\you\Desktop\aleemullah resume.pdf"
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filePath);
Change the filePath variable to wherever you are storing the PDF file.
You might also consider using a JFileChooser to select the file if you want the user to choose the PDF during runtime. Check out this example of how to open a dialog box from Java to pick a PDF file and retrieve its path.

Display files that have been uploaded to directory (servlet)

So, I followed this tutorial to upload files to a servlet:
http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api
It creates a folder (if it doesn't exist) and uploads a file to that folder.
Once the upload in complete it displays a page saying that the upload was successful. I've seen other tutorials, such as this one:
http://www.javacodegeeks.com/2013/08/servlet-upload-file-and-download-file-example.html
That display a download link, but only for the file that was just uploaded.
I want to create a page that uploaded a file, then displays links to download all files that have been uploaded (or a button to direct one to such a page).
Suppose you're uploading all files to X folder
Then, Simply iterate over X folder to get all files
for(File f:new File("X").listFiles()){
//Iterate here to create download links for each file
}
You may use http://www.javacodegeeks.com/2013/08/servlet-upload-file-and-download-file-example.html for each of the file to create a download link

How close a file opened by java.awt.Desktop

How to close an opened excel file?
Open excel code is:
File file = new File("e:\\aaa.xlsx");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
What would be the close code?
just using java open and close Windows application like use mouse.
Forget it. Open here means that the registered application, Excel, takes over and opens a Window. After that you have no control, but watching. Excel will close.
File is a class that represents a file system path. Itself it has no state of associated reader/writer. Java 7 now parallel introduces a more evolved class Path. Besides (obviously) the path on the file system, it also stores what file system. With Path one can have more than one FileSystem, like a ZipFileSystem. That allows you to copy and rename files in a zip.
That was just an elaboration.

Categories