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

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

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)

Related

Downloading a dynamically created text file from Java, write directly to computer or save on server?

When a user clicks a download button (coded in simple Javascript) on my webpage, it triggers a PHP function that calls to a java file. This Java code connects to the database and writes to a text file.
Currently, the code is in development on my local machine. Here is a snippet of the code is currently creating a local text file and writing the information to it:
StreamFactory sf = StreamFactory.newInstance();
sf.loadResource("mapping.xml");
File file = new File("C:\\MyLocal\\foo.txt");
BeanWriter bw = sf.createWriter("fileExport", file);
// writes beans
bw.write("", "");
...
bw.flush(); // flushes to foo.txt located in C:\MyLocal
My question is is it possible to write this dynamically created text file to the user's computer instead of my local, and if so, is it a good web development practice? The benefit this way is that I don't need to store foo.txt on the server that the code will reside. However, I'm not very familiar with web development practices, and did not see a concrete rule on this subject matter.
If it is better to save it to the server and then implement some download code from there, should this be handled within the PHP of the page or the Java backend functions?
It's not possible to write on user's computer filesystem.
Because of javascript security measures, if it wasn't because of this, a malicious website/webapp could write a huge amount of data on users hard drive, or could write malicious scripts.
The option a user has it's to download a file, but a webpage doesn't have direct access to user filesystem.
There was a proposal API for doing something like this, but it was discontinued
https://www.html5rocks.com/en/tutorials/file/filesystem/
Answering to your question: definitely it's not a good practice to try to write files on users filesystem
You should handle the download with Java on the Backend

File upload with Ajax - not getting complete fileName

It is quite a common question but I can't find an answer to it
I have a simple HTML with an input text box (type=file) and a submit button. On clicking the submit button, I call a js function where I try to get the complete path of the file
var data = $('#fileName').val();
the issue is I am not getting complete file path of the file I am uploading. I know due to security reasons chrome gives me a C:\fakePath\filename and firefox gives me only the fileName. But in case I need a complete path what shall I do?
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
You cannot get the complete path! there is no way to do that!! Even though you are on an intranet and you have enough permissions.
A workaround for this is to have a textarea and ask the user to enter the complete path of the file.
In short you can't have the full name of a file once is loaded on server side, you will just have the file name and its content in a raw byte array (among other attributes). This is not a Java thing nor other server side technologies issue, is related to browser implementation (but it looks that IE6 may contain a flaw about this).
Not directly related to your question but caught my attention
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
Usually, you can't handle a file upload using ajax because it can lead to security holes. Still, there are some browsers (like Chrome and Firefox) that allows you to send a file using XMLHttpRequest but that isn't allowed on some browsers (like IE8-) so you have to use an iframe in order to make the file ajax uploading work.
In order to avoid handling all these problems, I would advice you to use a third-party js library that handles the ajax file upload. An example is blueimp jQuery file upload that also has Java server side examples (DISCLAIMER: I do not work in this project nor I'm associated with blueimp in any way). Note that using this plugin requires that you have a mid knowledge on HTML/JavaScript/jQuery/Java Server Side so if you're a starter it may take you some time to make it work, but once it does is pretty good.
I dont know which technology you are using.. but you can always get file name once it is uploaded on server (Using php or .net )
your steps to upload should be like below:
1) Upload file to the server (e.z. /uploadedFiles/...filename
2) Create a method which will fetch file name from the uploaded path
3) simply insert file name in to the database (this will give you flexibility to change folder name of uploaded docs in future if required)
Generally filenames are not stored as it is . to avoid name conflict in future. So it is a advisable to always rename your filename by adding minutes & seconds after itsname.
If any doubts do ask.
Hope it helps.
Browsers block the filepath access on javascript for securit reasons.
The behavior makes sense, because the server doesn't have to know where the user stores the file on his computer, it is irrelevant to the upload process.

Open MS Word document from network

Here is the case.
User should be able to open MS Word document which is located somewhere in the network (ie. \remote\machine\documents\document_to_edit.docx) with MS Word 2007 by clicking link in the browser. Browser is IE7+.
Edit the document, close it and save it (no "Save as..." just "Save") in the same place in the network (\remote\machine\documents\document_to_edit.docx)
Is that at all possible? If so how should I do that?
Don't think this is relative, but I'm using JAVA + Wicket for my web application.
EDIT:
Any suggestions are welcome.
Main thing is - open file as it was on your computer and save it after edit.
(Read update below)
Short answer: not possible.
Long answer: When you open anything from a browser, even some local file, it will open the given file from the browser cache (or some temporary download directory) but not the original file linked. Therefore, after you save the changes the copied file will be updated.
Added:
Ok, after I thought about it for a while, there could be some ways to do it in intranet application. Here's how: Java applet: run native code from browser?
So, basically, you will serve the applet which will communicate with your javascript (I guess this way would be easiest to implement) (info on how to do it) and send a network path to the applet. Applet will start winword.exe passing it the required parameters.
Or, there's another option with ActiveX: http://codereflex.net/how-to-run-exe-on-webpage/ . The downside is - it works only with IE, but that seems what you need anyway.
IE can open \\server\share\file.docx type links and it opens the original file, not a downloaded version. You may need to add the domain of your http server to trusted sites in the security settings of IE first though. Have tested this and it does work

How to store data for a java applet

I have to create a java applet that needs to access static data which is around 600k in size. This data is exported from an sql database. What is the the best format for that data to be stored in (xml, json, java include file), to get fastest/easiest access to it. I am a complete java noob and this question might be stupid, but is there a way to 'compile' this data in to executable so there are no additional requests to server once the applet is loaded. Thanks in advance!
I do not know what do you mean when you mention 'java include file'.
All the rest is OK. You can use either XML or JSON. It depends on your needs and taste. Just remember that JDK has built-in tools to parse XML and does not have such tools for JSON, so you will have to add external dependency (e.g. GSON). Generally it is not a problem but sometimes code size may be important for applets that are expected to be downloaded from server to client.
The other problems with applets is that unsigned applet cannot write to client's disk. So, whatever format you choose you have to store the information somewhere. You can store it on server, but server has access to DB anyway, so why to create copy?
So, my suggestion is the following. Store data in database. Create server side component (web service) that allows your applet to access the data. Applet should store in browser cookies user id, so next time user runs the applet it enters automatically.
To access browser cookie from applet user live connect and remember that applet tag should have MAYSCRIPT attribute.
If the data is static, just copy in the source tree next to your .java files.
From there, you can access it (from a class in the same package) with:
getClass().getClassLoader().getResourceAsStream("name");

Uploading the contents of a directory in JSP

I'm developing a JSP application, and I want to be able to upload the contents of a directory to the server. So the user will select the directory he wants to upload, and somehow all its contents will be uploaded.
Can't be done with regular HTML/Javascript, you have to use either Java or Flash.
There is no support for this in HTML (only uploading of a single file) so there is nothing you can do in your JSP. If you want to do this in java you will have to have something client side, like a signed Applet or a jar that the user downloads and runs.
Looks like it's time for a Framework! Struts2 can handle this type of jobs well and is easy to start with. Here you can take a look at a File Upload sample.
As said, there is no way to do this in JavaScript/HTML.
If you do not want to use a Java Applet (they are clunky and your users may not have Java installed), you can let people upload a zip file and extract it on the server (there is support for this in the Java standard API). Both Windows and Mac OS allow the clients to zip a folder by right-clicking.

Categories

Resources