0

i am saving some records in an excel file on click of a button
currently i am hardcoding the path like

FileOutputStream fileOut = new FileOutputStream("c:\\test.xls");

but i want to take the path from user,i.e.,something like 'save as' option
how can i do it?
please help.
Thanks in advance.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by tapas.chand
0

The statement that you have given above will write the while on the server. If you want to have a option for the client to save the file than you will have to send the file on the server through the servlet response.

//First set HttpServletResponse response to tell browser about system going to return an application file instead of normal html page

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=test.xls");

//Now Read file from physical location and output it to the servlet's output stream

File file = new File("C:\\test.xls");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
 
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
	out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();
0

Thanks a lot for your precious instructions.
Now my problem got solved.

Edited by peter_budo: Keep It Organized - Avoid using an excessive amount of [bbcode] to alter font styles or to draw more attention to your post.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.