In my application i have to generate reports .Earlier we show reports only in jsp pages .now i try that user can also download file to his /her computer .i generate reports in excel format now i have to upload it to client machine . how to do this Thanks d17may
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
i have to upload it to client machine
Whats wrong with providing a clickable link on an HTML page just like you would use for letting the client download a physical file? The link would lead to a servlet that generates the data stream, sets the response headers, etc just like a file download.
I write the code and i also able to download file but problem is that on opening the file i get error file is not recognizable format This is my code ServletOutputStream out = null; response.setContentType("text"); response.setHeader("Content-Disposition","attachment; filename=a.txt"); FileInputStream f=new FileInputStream("c:\\a.txt"); FileChannel fc=f.getChannel(); //String s="QQQQ";
try { byte[] data = new byte[(int)fc.size()];
out = response.getOutputStream(); out.write(data); out.flush(); out.close(); } catch(IOException e){
We're pleased to have you here with us in the Servlets forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
As to your question. If you would like an example of a servlet that streams binary files to the client, I have one on my site. http://simple.souther.us See SimpleStream. [ July 21, 2006: Message edited by: Ben Souther ]
Thank you for changing your screen name and Welcome to JavaRanch.
amit sharma
Ranch Hand
Joined: Jul 19, 2006
Posts: 129
posted
0
When client click on link browser prompt it to either save or open the file . When client click on save in the filename textbox shows the name of servlet . I want to give some pathname (at runtime).My code is ServletOutputStream out = null; response.setContentType("application/vnd.ms-excel");response.setHeader("Content-Disposition","attachment; filename=c:\\"+file_name); FileInputStream f=new FileInputStream("c:\\"+file_name); FileChannel fc=f.getChannel(); //String s="QQQQ"; int length=(int)fc.size(); try { byte[] data = new byte[length]; int offset = 0; int numRead = 0; while (offset < length && (numRead=f.read(data, offset, data.length-offset)) >= 0) { offset += numRead; } out = response.getOutputStream(); out.write(data); out.flush(); out.close(); } catch(Exception e) {
Originally posted by dmay chug: When client click on link browser prompt it to either save or open the file . When client click on save in the filename textbox shows the name of servlet . I want to give some pathname (at runtime).My code is ServletOutputStream out = null; response.setContentType("application/vnd.ms-excel");response.setHeader("Content-Disposition","attachment; filename=c:\\"+file_name); FileInputStream f=new FileInputStream("c:\\"+file_name); FileChannel fc=f.getChannel(); //String s="QQQQ"; int length=(int)fc.size(); try { byte[] data = new byte[length]; int offset = 0; int numRead = 0; while (offset < length && (numRead=f.read(data, offset, data.length-offset)) >= 0) { offset += numRead; } out = response.getOutputStream(); out.write(data); out.flush(); out.close(); } catch(Exception e) {
} ....................... Thanks
I don't see a question here.
amit sharma
Ranch Hand
Joined: Jul 19, 2006
Posts: 129
posted
0
I am sorry if you didn't understand my question .What i want that is that when user click on save option then a dialog box appears which by default in file name textbox has the value of servletname . I want to override that value .For e.g. My servlet name is s1 user see s1 in filename textbox (in dialog box after clicking on save option). I generate excel report and user give me fromdate and todate .I want to give default name as client_name_fromdate_to_todate
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
Originally posted by dmay dmay: upload it to client machine
Watch your use of "upload" and "download", or confusion will result. Of course, download and upload are somewhat dependent on the viewpoint (client or server), but there are established norms when discussing Web applications.
In this case, as other posters have already said, what you seem to need is for the Web client to initiate a transfer of a resource from the Web server. That's usually called a download.
In a Web application, upload is where the Web client initiates a transfer of a client-side file to the Web server.
In Web applications, using HTTP, it is generally not possible for the Web server to initiate transfers.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Originally posted by dmay chug: I am sorry if you didn't understand my question .What i want that is that when user click on save option then a dialog box appears which by default in file name textbox has the value of servletname . I want to override that value .For e.g. My servlet name is s1 user see s1 in filename textbox (in dialog box after clicking on save option). I generate excel report and user give me fromdate and todate .I want to give default name as client_name_fromdate_to_todate
Take a look at the example code in the JSP FAQ -> Excel. In it, the JSP generates some text and sets the content-type and content-dispostion headers.
The content-disposition header is where you would set a filename.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.