Here i show a simple java example to demonstrate how to let user download a file from website. No matter you are using struts , JSP, Spring or whatever other java framework, the logic is same.

1) First we have to set HttpServletResponse response to tell browser about system going to return an application file instead of normal html page

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=downloadfilename.csv");

we can also specified a download file name in attachment;filename=, above example export a csv file name “downloadfilename.csv” for user download.

2) There have 2 ways to let user download a file from website

Read file from physical location

File file = new File("C:\\temp\\downloadfilename.csv");
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();

Export database data or string directly to InputStream for user download.

StringBuffer sb = new StringBuffer("whatever string you like");
InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();
 
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(in.read(outputByte, 0, 4096) != -1)
{
	out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();

3) Done

Here i show my struts example to demonstrate how to directly write data into InputStream and output it as “temp.cvs” to let user download.

public ActionForward export(ActionMapping mapping, ActionForm form,
	HttpServletRequest request, HttpServletResponse response)
	throws Exception {
 
	//tell browser program going to return an application file 
        //instead of html page
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=temp.csv");
 
	try 
	{
		ServletOutputStream out = response.getOutputStream();
		StringBuffer sb = generateCsvFileBuffer();
 
		InputStream in = 
                    new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
 
		byte[] outputByte = new byte[4096];
		//copy binary contect to output stream
		while(in.read(outputByte, 0, 4096) != -1)
		{
			out.write(outputByte, 0, 4096);
		}
		in.close();
		out.flush();
		out.close();
 
	  }
	  return null;
	}
 
private static StringBuffer generateCsvFileBuffer()
{
	StringBuffer writer = new StringBuffer();
 
	writer.append("DisplayName");
	writer.append(',');
	writer.append("Age");
	writer.append(',');
	writer.append("HandPhone");
	writer.append('\n');
 
        writer.append("mkyong");
	writer.append(',');
	writer.append("26");
	writer.append(',');
	writer.append("0123456789");
	writer.append('\n');
 
	return writer;
}

Here’s a file download example in Servlet code

Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts