views:

89

answers:

3

In the java web application need to select the file from server and print to the local printer. how it can be done

Thanks in advance

+2  A: 

You will need an applet, flash, silverlight, javafx - i.e. an embedded app. There:

  • download the file from the server by creating a GET request (in an applet - using URL.openConnection()), obtaining the returned bytes and forming an in-memory document
  • sending that to a printer. If you chose applet - this might help

(I'm not aware whether the same flow can't be achieved with javascript as well)

Bozho
+2  A: 

That's going to be tricky whenever you require a minimum of user interaction (i.e. just click the link and then do the print magic) and it also depends on the type of the file in question. If it is for example a .doc file, then you would basically need to download it to the client environment and open it in the default associated application (MS Word in this case) and then let the application execute the print command. You can't do this from the server side on.

Your best bet is to create an Applet which in turn displays the file tree, downloads the file to the local disk file system on client interaction and makes use of Desktop#print() to print it. E.g.

File file = new File("/temp/file.doc");
// Read file from server using URLConnection, write it to this file and then do:
Desktop.print(file);

But if it are for example plain text files such as text/html, text/xml, etcetera, then you can make use Javascript to load the file into some <div> or <iframe> element and then execute the window.print() method on it, if necessary along with a CSS media rule.

BalusC
A: 

Is it possible using ByteArrayOutputStream and java print Api?