I'm trying to code in a download in my java application. Basically, the user inputs their personal information, which is sent through file i/o to a text document. Then the next step is to have a series of buttons or links that corresponds to a certain download. For instance, button1=file1, button2=file2, and so on. I need it to download a picture. Is this possible through a dialog box? I'm running the application through a website. Any help is appreciated!
You can download a file from a URL using URLConnection. You can have your dialog box display the choices then choose a URL depending on what button they pressed, and use the URLConnection (via .connect() and .getInputStream()) to read the data, which you can then write out to a file or do whatever you want with.
You say you are running it through a web site, so I assume this is an applet. Bear in mind that if it is an unsigned applet you can only download files from the server that the applet is on. If you need to download from anywhere, you will need to sign your applet, or use something that isn't an applet.
Also, since you are running it through a web site, if it's appropriate perhaps a different approach is to just redirect the user to the URL of the file and let their browser take care of the download.
Related
I am trying to write a java program that uses selenium to perform web actions. In the case of uploading a video there is a frame where the user clicks that brings up the File Explorer for Windows. I have not been able to find anything that will help me in getting the focus to the File Explorer to select a file. There is no "browse" on the page with a textbox for me to just send a filename to.
Here is what the page looks like:
I would try using Selenide because it has a pre-canned upload utility that might save you some time/effort. See the command .uploadFromClasspath in the docs. One of the project videos/pages shows a little more detail on how to do it.
I've not actually tried it, but if this works for you and you figure it out, please share.
What I have done in the past is to use the "Apache HttpUtils" class to simulate the upload action from my test. All you need is the upload URL, and the HTML page usually provides that. That might also work for you.
I asked a question on here once before and got some great help and advice from a lot of knowledgeable people so I thought I'd try this again with another programming issue I'm having.
Here is the basic problem. I've created a small Java applet that takes four images and combines them into one large image. To be more specific, I combine four images into one large image that will fold up to be a greeting card when printed out. Everything works just fine, but I'm going to be embedding the applet on my website and so I'll have users select the four images they want from a list of images on my website, and then with their selections I'll create the large image.
Now here's the trouble... when I create the large image I want the user to be able to download it. Is it necessary to create a signed applet? And if so, how do I go about signing my applet? Or is it somehow possible for the applet to just display the image in such a way so the user can just right-click the image and save it to their file system. Any advice/suggestions would be much appreciated!!
There is no need to sign the applet if the image is loaded from the same location that the applet class itself is downloaded. There would be a problem however, saving the image locally from an unsigned applet. For that functionality, you would need to sign the applet.
Alternatively, for applets running in a web browser, the image could be displayed in a separate browser frame using:
applet.getAppletContext().showDocument(imageURL, frameTarget);
From this page/frame, the image could then be saved locally using Javascript.
Depending on Java and browser version, I see 3 options:
Digitally sign the Jar and encourage the end user to trust the digitally signed code.
Jar the app., deploy it using Java Web Start and use the JNLP API services to save the resulting image. This can be done with a free floating applet since Java 1.2, or an embedded applet since 1.6.0_10+. Here is a demo. of the file services. GIFanim also offers an embedded applet that uses the services for saving animated GIFs.
Encode the image as a Base64 URL to display it direct in a web page. This requires a more recent browser, older browsers put a 32Kb limit on the image size. J2SE only had a method added recently, that could do the conversion for us. See DatatypeConverter.printBase64Binary(byte[])
To write files to a file system, signing an applet is necessary.
However it is quite easy to self sign applets and unless your users are extremely cautious, self signing would work.
Follow these intructions to sign an applet. P.S these instructions are for a linux setup. You might need to set up environment variables in windows.
Jar signing link
I am writing a java client-server application where some clients have the right to put html files to serve as login pages on the server and the server will provide those pages to the rest of the users when they try to log. To this purpose I have added a preview button , that allows the admin set of clients to visualize the page when they upload it or afterwords . The problem I need to figure it out is how to open the browser to preview the file when the resource is not locally (it was uploaded by another admin and therefore it is remote).
Any suggestion and reference will be greatly appreciated !
Depends on the client; my first choice would be to use the Desktop class to make things simple, if you can guarantee Java 6.
If not, the Bare Bones Browser Launch plays some games on JRE < 6.
See this SO post for a bit more info.
If neither of those suits your purposes, you may need to provide some more info.
I am working on a web application developed in Java with struts running on Tomcat. I have a requirement in the web application as follows:
One JSP page having a list of PDF files each associated with a checkbox. The JSP page has one Button. Once the user selects the PDF documents he wants to print by selecting the associated checkboxes, user clicks on the button.
Then all the selected PDF docs should be sent to a specific local Printer( i.e. printer connected to the client machine where from user is accessing the web application). The selected PDF files should not be opened either in acrobat reader or in browser. The PDF docs should not be visible to the user while being sent to printer.
Could anyone please help me in implementing this requirement?
It is possible with Internet Explorer and ActiveX. Search about "auto print" and "silent printing" with Google. You may have to lower security settings in Internet Explorer for that.
We're using it here for some Intranets from our Customers.
For Firefox you may be able to trigger the print dialog via javascript. (http://stackoverflow.com/questions/975652/silent-print-a-embedded-pdf)
If this is the case you can disable print dialog in FF with setting print.always_print_silent=true in about:config.
Never did it on my own, so I cannot say for sure if it works like the ActiveX thing does.
For Chrome there is an issue requesting same feature like FF has. See https://code.google.com/p/chromium/issues/detail?id=31395.
You will most likely have to use a Java Applet for this. The browser have no inherent capability to print a pdf document. You may be able to access the "standard" adobe plugin, but to my knowledge it is not exposed to the standard scripting environment.
You can look here: Can a Java Applet use the printer?
Note: it is recommended to have the applet signed to prevent security restrictions and/or annoying questions to the user to allow access to printer.
You'll also need some form of PDF renderer in your applet (to render to printer). Something like http://java.net/projects/pdf-renderer/, read more here: http://juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/
That is not possible with JavaScript. It is possible with either Flash or a Java applet. In either case, you will need to be able to use both the Printer drivers and a custom PDF reading toolkit. There are adequate plugins for both for PDF reading, and both come with a printing API.
If I may say so, this is a bad requirement -- it is not good to force a user to print a document without reading it -- and should be re-negotiated, especially since it is trivially simple to have those documents print to PDF anyway.
I am looking for a way to save a couple of files created with JavaScript on the clients computer. I want him to be able to choose a folder (similar to open/save folder dialog), and then write the files there. This obviously requires three things:
Ability to popup such a dialog.
Ability to write on the client's computer (signed applets?)
Some way to interact with JavaScript because it knows the filenames and contents to write.
Is this possible or should I just stop dreaming?
It sure is possible.
One way is to use a signed applet, but if your users are willing to upgrade to Java 1.6.0_10+, a signed applet is not needed. Since 1.6.0_10 (the Next Generation Java Plug-In), JNLP API services are available to embedded applets. Here is a demo. of the JNLP file services.
I see the other reply has already covered the applet/JS interaction.
It is possible with a signed applet.
I'd probably do it the other way around, let the javascript code pass all the data to the applet and the applet should open a FileDialog and write the file.
Calling javascript from java can be achieved via `JSObject, calling Java from Javascript is explained here
If your applet is signed you can do everything that you can do in regular application. You can open "File dialog" but it will be the Java's dialog, not the same that is opened when user pushes "browse" button of tag <intput type"file"/>. Obviously signed applet can perform read and write operations with user's disk.
All applets (either signed or unsigned) may call javascript using JSObject. You just have to say MAYSCRIPT="true" into applet tag.
I think that you cannot open the browser's "file open" dialog applet. Actually you even cannot open it using regular javascript. (I'd be happy to know that I am wrong at this point but I spent some time trying to do this and failed).