Java download file from URL with unknown filename - java

I have URL that gets called from my java app that returns the save/open dialog for an Excel file.
At the moment, it opens in a new tab but I want to return the dialog box in the same window. I do not know the file name as it is dynamic and changes based on the parameters passed in.
I also do not want to save it somewhere as I want the user to have the choice of opening or saving.
Is there anyway of doing this in Java? I've only seen examples where the file name is known.
EDIT
The URL is hitting a CGI Script

Create a File object in your app from the url, store it in a temporary location then ask Excel to open it for you.

Related

Open file with java application

I am using Ubuntu. I would like to have a user be able to double-click (or whatever they have set to open a file) on the save file for a java game i'm making, and have the game start and automatically load the save. How could I make the game launch instead of opening the file, and then pass the save's file name or location as an argument? Would i have to have the game create a new mime type the first time it ran, or something similar, so that Ubuntu knows what to do with the save when the user tries to open it?

How to read & open multiple file in one request using struts?

I have a requirement that, User will see the list of files in the page which is the list from directory. From there they will select multiple files and they click on view button. Then we need to read the corresponding files from Drive and need to open all the files which are all user has selected. I need to implement this using Struts2/servlet.
From what I userstand the user sees the content of a directory which is located on the server, right?
Further, you display a list of files along with some selection method (checkboxes ?) which the user uses to select files and then clicks the view button.
If I am correct, you could use javascript to open a new tab/window or dialog (e.g. a jQuery dialog) which reads the file or its contents from the server. Just iterate over the selected entries in the list and pass them one by one to the JavaScript function that opens the tab/window/dialog.
It is not clear if you do want to
open each file in the client's browser, or
download each file to the client's file system, or
download an archive containing all the files to the client's file system;
In 1. or 2. you need to create multiple requests, targeting multiple windows, using target="_blank", or by AJAX, etc .
By manipulating HttpResponse Header objects, you can instruct the browser when to open or download a file: Content-Disposition: inline; will try to open it inside the browser, while Content-Disposition: attachment; will perform the default operation (open with default desktop application, download, or ask if none of the previous was marked as default by the user)
Keep in mind that you may encounter several possible problems by opening or downloading multiple files at once; it could overload/crash the client, overload/crash the server, cause problem of bandwidth etc; what if the user choose 100 files ? You would open 100 tab, or 100 windows, or ask 100 times where to save each file, etc...
If you want to create a dynamic ZIP instead, you can find a kick-off example on how to do it in Struts2 here, (using an Action like a Servlet, and returning NONE);
My files were from database, yours are from file system, but the code is the same.

How to open latest downloaded file

I have a jsp form with a download button.Whenever user clicks that button it will download a pdf file and popups that.I'm using chrome browser. My download location is
C:\Users\gt\Downloads
For this I have used the following code to open from that location.
String filePath=System.getProperty("user.home")+"\\Downloads\\"+pdfname;
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +filePath);
Now when user clicks the download button the pdf file will get save in that location by the name like F90CR0010-HBR-C-4.pdf and my code will open this correctly.
When the user clicks the download button again for second time it will save as like F90CR0010-HBR-C-4 (1).pdf But my code will open the old previous file only.Because the name is that only.I need to open the latest downloaded file.
Any Idea of how to open the latest downloaded file??I have a idea that I can add the current time to my pdfname and so it will be unique.But my requirement is that pdf name should be like this only.
If you know what the base file name would be (in your case F90CR0010-HBR-C-4.pdf ) then you can use a simple regex to check for strings of the form F90CR0010-HBR-C-4\(\d+\).pdf . If you find one then just figure out which one has the biggest number and open that one.
Its not really pretty, but it works. Also, technically this can be "hacked" by making your own file called F90CR0010-HBR-C-4(999999).pdf, but I would guess you dont really care.

JSP: save a file on client without storing it on server

In my jsp someone wants to export a query result to csv. Can't use frameworks. At the moment, here is the process:
press button, go to servlet
prepare data for csv
make csv and save to server
back to jsp and let them download the fresh-made file from an anchor tag.
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. So, is there a way, putting for example an OutputStream object in session, to achieve this result?
Thank you all!
A servlet can generate any type of content. So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. You'll need to set the Content-type header to "text/csv" (and making sure that you set encoding properly). You don't need to set the Content-Length header; the browser can deal with that.
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application.
Yes. You don't need to save the file. Just hold it in the session and send it in-memory to the OutputStream. I would trash it after a given time or after delivery in order to save memory.
Set the content type and the content disposition appropriately. This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
See this SO answer for more details.

Pressing save button in JFileChooser opens a folder instead of saving a file

I'm having the following problem with a JFileChooser: I create a save dialog and then when I'm trying to save a file, I'm entering a file name into the field and click "Save". Suppose that the directory, which I try to save my file to, contains a subdirectory, which is named exactly like the entered name. In this case JFileChooser opens it instead of saving my file, and I don't know how to make it save a file instead of opening the directory with the same name.
I tried to do something with the file filters, with approveSelection() and changed UI from Windows to default - nothing helps. How to make JFileChooser stop opening a folder and save the file? Is there some way to modify the behavior of the button or am I just doing something wrong?
Update:
To be clear: I'm not trying to save a file with the same name, I'm just trying to make JFileChooser get the path so I could append an extension to it. However, I checked Word and Photoshop, neither of them is able to do what I request, so I guess I wish to make everything too perfect. But if there's a way to do what I want, I'll still be glad to hear the solution.
Neither Windows nor Linux allow file names and directory names to be the same in the same directory as far as I know. Under Linux of course they can be the same with different case lettering :)

Categories

Resources