// description of your code here
1
2 /**
3 * Sends a file to the ServletResponse output stream. Typically
4 * you want the browser to receive a different name than the
5 * name the file has been saved in your local database, since
6 * your local names need to be unique.
7 *
8 * @param req The request
9 * @param resp The response
10 * @param filename The name of the file you want to download.
11 * @param original_filename The name the browser should receive.
12 */
13 private void doDownload( HttpServletRequest req, HttpServletResponse resp,
14 String filename, String original_filename )
15 throws IOException
16 {
17 File f = new File(filename);
18 int length = 0;
19 ServletOutputStream op = resp.getOutputStream();
20 ServletContext context = getServletConfig().getServletContext();
21 String mimetype = context.getMimeType( filename );
22
23 //
24 // Set the response and go!
25 //
26 //
27 resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
28 resp.setContentLength( (int)f.length() );
29 resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
30
31 //
32 // Stream to the requester.
33 //
34 byte[] bbuf = new byte[BUFSIZE];
35 DataInputStream in = new DataInputStream(new FileInputStream(f));
36
37 while ((in != null) && ((length = in.read(bbuf)) != -1))
38 {
39 op.write(bbuf,0,length);
40 }
41
42 in.close();
43 op.flush();
44 op.close();
45 }