aspose file tools*
The moose likes Servlets and the fly likes upload file to client computer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "upload file to client computer" Watch "upload file to client computer" New topic
Author

upload file to client computer

amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
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: 12789
    
    5
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

Joined: Jul 19, 2006
Posts: 129
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5

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

Joined: Jul 19, 2006
Posts: 129
Thanks for ur suggestion .That was a silly mistake .Now my program works properly
Thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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

Joined: Dec 11, 2004
Posts: 13410

Thank you for changing your screen name and Welcome to JavaRanch.
amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
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

Joined: Dec 11, 2004
Posts: 13410

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
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
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
wood burning stoves
 
subject: upload file to client computer