开发者

In java web application how to print server file from client side

开发者 https://www.devze.com 2022-12-24 03:07 出处:网络
In the java web application need to select th开发者_StackOverflow中文版e file from server and print to the local printer. how it can be done

In the java web application need to select th开发者_StackOverflow中文版e file from server and print to the local printer. how it can be done

Thanks in advance


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消