• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

upload file to client computer

 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){

}
Thanks
d17may
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

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.

You can change it here


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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That appears to create a new byte[] (initialized to zeros) and write it - where is the part where you read the local file into the byte[]?
Bill
 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur suggestion .That was a silly mistake .Now my program works properly
Thanks
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"dmay dmay",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

Please note that this is your third and final warning.

thanks,
Dave
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for changing your screen name and Welcome to JavaRanch.
 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic