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.

Recommended Answers

All 2 Replies

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();

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.